Inside
Inside

Reputation: 53

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?

Upvotes: 5

Views: 1336

Answers (2)

JoBot
JoBot

Reputation: 131

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();
}

Upvotes: 3

Related Questions