Search code examples
c#dynamicreflectionexpandoobjectfastmember

FastMember and ExpandoObject error


I'm not sure if its supported but I'm trying to get fastmember to give me type information for a dynamic ExpandoObject (using the code below) and I get the error:

System.NotSupportedException: Specified method is not supported. at FastMember.TypeAccessor.GetMembers() at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0)

The second test for an anonymous dynamic type works fine, but for some reason it doesn't work with ExpandoObject. Is this supposed to be possible? I can think of an alternative solution by using the ExpandoObject dictionary implementation if not it would be nice to use the same code for everything though.

public class FastMemberTest
{
    [Fact]
    public void GetsMembersOfExpandoObject()
    {
        dynamic dyn = new ExpandoObject();
        dyn.Item1 = 1;
        dyn.Item2 = "Testing";
        var typeAccessor = TypeAccessor.Create(dyn.GetType());

        var members = typeAccessor.GetMembers();

        members.Should().NotBeNull();
    }

    [Fact]
    public void GetsMembersOfDynamicType()
    {
        dynamic dyn = new
        {
            Item1 = 1,
            Item2 = "Testing"
        };
        var typeAccessor = TypeAccessor.Create(dyn.GetType());

        var members = typeAccessor.GetMembers();

    }
}

[Test is Xunit2]


Solution

  • I ended up writing a class which mimicked the fast member behaviour but cast the expandoobject to an IDictionary and using that. Then using this only when an expandoobject was passed otherwise using fastmember. Works fine and the alternative implementation is actually pretty simple.

    The abstraction and isolation of fastmember is good for the code base too.