Search code examples
c#reflectionreflection.emitil

Reflection Emit to create class instance


I want to use Reflection Emit to create an instance of a class with arbitrary constructor parameters. This is how my code looks like:

public delegate object ObjectActivator(params object[] args);
static void Main(string[] args)
{
    var ao = new { ID = 10000, FName = "Sample", SName = "Name" };
    var t = ao.GetType();
    var info = t.GetConstructor(new Type[] { typeof(int), typeof(string), typeof(string) });
    var objActivatorEmit = GetActivatorEmit(info);
    var obj = createdActivatorEmit(4, "Foo", "Bar");
}
public static ObjectActivator GetActivatorEmit(ConstructorInfo ctor)
{
    ParameterInfo[] paramsInfo = ctor.GetParameters();
    DynamicMethod method = new DynamicMethod("CreateInstance", typeof(object), new Type[] { typeof(object[]) });
    ILGenerator gen = method.GetILGenerator();
    for (int i = 0; i < paramsInfo.Length; i++)
    {
        Type t = paramsInfo[i].ParameterType;
        gen.Emit(OpCodes.Ldarg_0); // Push array (method argument)
        gen.Emit(OpCodes.Ldc_I4, i); // Push i
        gen.Emit(OpCodes.Ldelem_Ref); // Pop array and i and push array[i]
        if( t.IsValueType )
        {
            gen.Emit( OpCodes.Unbox_Any, t ); // Cast to Type t
        }
        else
        {
            gen.Emit( OpCodes.Castclass, t ); //Cast to Type t
        }
    }
    gen.Emit(OpCodes.Newobj, ctor);
    gen.Emit(OpCodes.Ret);
    return (ObjectActivator)method.CreateDelegate(typeof(ObjectActivator));
}

The code fails with an MethodAccessException with the error message Attempt by method 'DynamicClass.CreateInstance(System.Object[])' to access method '<>f__AnonymousType1'3<System.Int32,System.__Canon,System.__Canon>..ctor(Int32, System.__Canon, System.__Canon)' failed..

What is going wrong?


Solution

  • The error message indicates that the anonymous type's constructor isn't public. I think anonymous type constructors are always internal, so you'll need to skip visibility checks by using a different DynamicMethod constructor:

    DynamicMethod method = new DynamicMethod("CreateInstance", typeof(object), new Type[] { typeof(object[]) }, true);
    

    Note that this not work in partial trust scenarios.