Failed to run simple test for ExpandoObject. Can anyone explain why?

Error message first

Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'System.Collections.Generic.List' does not contain a definition for 'First' in CallSite.Target (Closure, CallSite, Object) in System.Dynamic.UpdateDelegates.UpdateAndExecute1 (CallSite site, T0 arg0) in ClaySharp.Tests.ToPropertyDictionaryTests.TestExpando () in ToPropertyDictionaryTests.cs: line 91

Test:

[Test]

public void TestExpando()
{
    dynamic root = new ExpandoObject();
    root.Name = "Name";

    var result = GetExpandos();

    root.Child = result;

    var first = root.Child.First();

    Assert.That(first.Name, Is.EqualTo("Obj1"));
}

private IEnumerable<dynamic> GetExpandos()
{
    var toReturn = new List<dynamic>();

    dynamic obj1 = new ExpandoObject();
    toReturn.Add(obj1);
    obj1.Name = "Obj1";

    dynamic obj2 = new ExpandoObject();
    toReturn.Add(obj2);
    obj2.Name = "Obj2";

    return toReturn;
}

The interesting part is that if the “root” is removed from the image and the test is run against the “result”, how does it work perfectly.

And now for the very strange part. Debugging - setpoint before returning toReturn. Take a look at this, it works

? ToReturn.GetType (). Fullname

"System.Collections.Generic.List`1 [[System.Object, mscorlib, Version = 4.0.0.0, Culture = neutral, PublicKeyToken = b77a5c561934e089]]"

? ToReturn

Count = 2   [0]: {System.Dynamic.ExpandoObject}   [1]: {System.Dynamic.ExpandoObject}

? ToReturn.First()

{System.Dynamic.ExpandoObject}

, "root",

?

Count = 2   [0]: {System.Dynamic.ExpandoObject}   [1]: {System.Dynamic.ExpandoObject}

? Result.First()

{System.Dynamic.ExpandoObject}

, root,

? Root.Child

{System.Collections.Generic.List}   [0]: {System.Dynamic.ExpandoObject}   [1]: {System.Dynamic.ExpandoObject}

? Root.Child.First()

+5
1

dynamic ; "" LINQ to Objects First , "" . :

7.6.5.2

... , . expr , .

, # 4 ?

:

var first = root.Child.First();

:

var first = Enumerable.First(root.Child);

:

var first = root.Child[0];

EDIT:

, "root" , "", .

result IEnumerable<dynamic>; (). , result.First(), Enumerable.First . result dynamic, .

+6

All Articles