5

check the code below please:

static void Main(string[] args) {
    IList<dynamic> items = new List<dynamic>();
    items.Add(3);
    items.Add("solid");
    dynamic i = new ExpandoObject();
    items.Add(i); //System.Collections.Generic.IList<object>' does not contain a definition for 'Add'
    Console.WriteLine();
}

is this a bug in "dynamic" mechanism?

Inside
  • 53
  • 5
  • +1 wish I knew more about the complexity of the dynamic binding. I just tried this and it appears that `items.Add(new ExpandoObject());` is fine, but `items.Add((dynamic)new ExpandoObject());` is not. – Daniel Earwicker Nov 03 '11 at 14:30

2 Answers2

3

Looks like a bug (or is it a feature request?):

https://connect.microsoft.com/VisualStudio/feedback/details/534288/ilist-dynamic-cannot-call-a-method-add-without-casting

Daniel Earwicker
  • 114,894
  • 38
  • 205
  • 284
3

This should do the trick:

static void Main(string[] args) {
    IList<dynamic> items = new List<dynamic>();
    items.Add(3);
    items.Add("solid");
    dynamic i = new ExpandoObject();
    items.Add((object) i); // type-cast dynamic object
    Console.WriteLine();
}
JoBot
  • 131
  • 1
  • 7