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