Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't understand the Exception when using dynamic with generic collection in .net4

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?

like image 636
Inside Avatar asked Nov 03 '11 14:11

Inside


2 Answers

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

like image 136
Daniel Earwicker Avatar answered Sep 23 '22 23:09

Daniel Earwicker


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();
}
like image 24
JoBot Avatar answered Sep 23 '22 23:09

JoBot