Reflection.Emit: property with function call in getter/setter

.net, c#, reflection, reflection.emit

Solution

You'll kick yourself:

NewPropSetIL.Emit(OpCodes.Ldarg_1);       
NewPropGetIL.Emit(OpCodes.Callvirt, typeof(Test).GetMethod("SetVal"));
NewPropSetIL.Emit(OpCodes.Ret);

Look closely. Closer. Closer. Still not there?

Line 2 talks to `NewPropGetIL`

If that still doesn't work, make sure that you declared the base-type in the `TypeBuilder`, and that the class `Test` is `public`, and not `internal` as shown in the question. It works fine for me now.

How I investigated it

I started by adding the following preamble:

AssemblyName aName = new AssemblyName("SomeAssembly");
AssemblyBuilder ab =
    AppDomain.CurrentDomain.DefineDynamicAssembly(
        aName,
        AssemblyBuilderAccess.RunAndSave);

// For a single-module assembly, the module name is usually 
// the assembly name plus an extension.
ModuleBuilder mb =
    ab.DefineDynamicModule(aName.Name, aName.Name + ".dll");

TypeBuilder tb = mb.DefineType(
    "SomeType",
     TypeAttributes.Public, typeof(Test));

and the following foot:

RunTest(typeof(ManualTest));
RunTest(tb.CreateType());

where:

private static void RunTest(Type type)
{
    Console.WriteLine(type.Name);
    Console.WriteLine();
    dynamic obj = Activator.CreateInstance(type);

    int i = obj.NewProp;
    Console.WriteLine(i);
    obj.NewProp = 123;
    i = obj.NewProp;
    Console.WriteLine(i);

    Console.WriteLine();
}

I also added some logging to the base methods:

public class Test
{
    private int _val1;

    public int GetVal(int fld)
    {
        Console.WriteLine("GetVal:" + fld);
        if (fld == 1) return _val1;
        return 0;
    }

    public void SetVal(int fld, int val)
    {
        Console.WriteLine("SetVal:" + fld);
        if (fld == 1) _val1 = val;
    }
}

and a manual test for comparison (the expected result):

class ManualTest : Test
{
    public int NewProp { get { return GetVal(1); } set { SetVal(1, value); } }
}

With this in place, it was clear that there is a problem in the `set`:

ManualTest

GetVal:1
0
SetVal:1
GetVal:1
123

SomeType

GetVal:1
0
GetVal:1
0

Problem

I am trying to use Reflection.Emit to create a dynamic type which inherits from a given type, and adds a new property whose getter/setter call methods of the base type. Suppose my base type looks as follows: ``` class Test { private int _val1; public int GetVal(int fld) { if (fld == 1) return _val1; return 0; } public void SetVal(int fld, int val) { if (fld == 1) _val1 = val; } } ``` I want to create a subtype which has a new property, defined as follows: ``` public int NewProp { get { return GetVal(1); } set { SetVal(1, value); } } ``` Seems simple enough. I came up with the following (which is the working answer): ``` PropertyBuilder pbNewProp = tb.DefineProperty("NewProp", PropertyAttributes.HasDefault, typeof(int), null); MethodAttributes getSetAttr = MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig; // Define the "get" accessor method MethodBuilder mbNewPropGetAccessor = tb.DefineMethod( "get_NewProp", getSetAttr, typeof(int), Type.EmptyTypes); ILGenerator NewPropGetIL = mbNewPropGetAccessor.GetILGenerator(); NewPropGetIL.Emit(OpCodes.Ldarg_0); NewPropGetIL.Emit(OpCodes.Ldc_I4_1); NewPropGetIL.Emit(OpCodes.Call, typeof(Test).GetMethod("GetVal")); NewPropGetIL.Emit(OpCodes.Ret); // Define the "set" accessor method MethodBuilder mbNewPropSetAccessor = tb.DefineMethod( "set_NewProp", getSetAttr, null, new Type[] { typeof(int) }); ILGenerator NewPropSetIL = mbNewPropSetAccessor.GetILGenerator(); NewPropSetIL.Emit(OpCodes.Ldarg_0); NewPropSetIL.Emit(OpCodes.Ldc_I4_1); NewPropSetIL.Emit(OpCodes.Ldarg_1); NewPropSetIL.Emit(OpCodes.Call, typeof(Test).GetMethod("SetVal")); NewPropSetIL.Emit(OpCodes.Ret); // Map the accessor methods pbNewProp.SetGetMethod(mbNewPropGetAccessor); pbNewProp.SetSetMethod(mbNewPropSetAccessor); ``` I did compare this to the compiler generated IL (using ildasm) based on a hardcoded sample and can't spot a difference. Here is what I did to test whether above code would work: ``` var inst = Activator.CreateInstance(myType); var p = inst.GetType().GetProperty("NewProp"); p.GetValue(inst, null); p.SetValue(inst, 1, null); ``` For reference, here is what ildasm says about "set_NewProp": ``` .method public hidebysig specialname instance void set_NewProp(int32 'value') cil managed { // Code size 9 (0x9) .maxstack 8 IL_0000: ldarg.0 IL_0001: ldc.i4.1 IL_0002: ldarg.1 IL_0003: call instance void ConsoleApplication2.Test::SetVal(int32, int32) IL_0008: ret } // end of method TestSub::set_NewProp ``` And here "get_NewProp": ``` .method public hidebysig specialname instance int32 get_NewProp() cil managed { // Code size 8 (0x8) .maxstack 8 IL_0000: ldarg.0 IL_0001: ldc.i4.1 IL_0002: call instance int32 ConsoleApplication2.Test::GetVal(int32) IL_0007: ret } // end of method TestSub::get_NewProp ``` In the original version of the question, the call to GetValue threw a TargetInvocationException, whose InnerException was an InvalidProgramException that says "Common Language Runtime detected an invalid program." It was due to a typo (since corrected); d'oh!

Original source