Reflection.Emit:在getter/setter中调用函数的属性

And*_*eas 4 .net c# reflection reflection.emit

我试图使用Reflection.Emit创建一个继承自给定类型的动态类型,并添加一个新的属性,其getter/setter调用基类型的方法.

假设我的基本类型如下所示:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

我想创建一个具有新属性的子类型,定义如下:

public int NewProp { get { return GetVal(1); } set { SetVal(1, value); } }
Run Code Online (Sandbox Code Playgroud)

看起来很简单.

我想出了以下内容(这是工作答案):

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);
Run Code Online (Sandbox Code Playgroud)

我确实将它与基于硬编码样本的编译器生成的IL(使用ildasm)进行了比较,并且无法发现差异.

以下是我测试上述代码是否有效的方法:

var inst = Activator.CreateInstance(myType);

var p = inst.GetType().GetProperty("NewProp");
p.GetValue(inst, null);
p.SetValue(inst, 1, null);
Run Code Online (Sandbox Code Playgroud)

作为参考,这里是ildasm关于"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
Run Code Online (Sandbox Code Playgroud)

在这里"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
Run Code Online (Sandbox Code Playgroud)

在问题的原始版本中,对GetValue的调用抛出了一个TargetInvocationException,其InnerException是一个InvalidProgramException,表示"公共语言运行时检测到无效程序".这是由于一个错字(自纠正以来); D'哦!

Mar*_*ell 8

你会踢自己:

NewPropSetIL.Emit(OpCodes.Ldarg_1);       
NewPropGetIL.Emit(OpCodes.Callvirt, typeof(Test).GetMethod("SetVal"));
NewPropSetIL.Emit(OpCodes.Ret);
Run Code Online (Sandbox Code Playgroud)

仔细看.言归正传.言归正传.还没有吗?

2号线交谈 NewPropGetIL

如果仍然不起作用,请确保在类中声明了基类型TypeBuilder,并且该类Testpublic,而不是internal在问题中显示.它现在适合我.

我是如何调查它的

我首先添加以下序言:

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));
Run Code Online (Sandbox Code Playgroud)

和下面的脚:

RunTest(typeof(ManualTest));
RunTest(tb.CreateType());
Run Code Online (Sandbox Code Playgroud)

哪里:

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();
}
Run Code Online (Sandbox Code Playgroud)

我还在基本方法中添加了一些日志记录:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

和手动测试比较(预期结果):

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

有了这个,显然有一个问题set:

ManualTest

GetVal:1
0
SetVal:1
GetVal:1
123

SomeType

GetVal:1
0
GetVal:1
0
Run Code Online (Sandbox Code Playgroud)