【问题标题】:Reflection.Emit: property with function call in getter/setterReflection.Emit:在 getter/setter 中具有函数调用的属性
【发布时间】:2014-02-03 15:50:16
【问题描述】:

我正在尝试使用 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;
    }
}

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

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

看起来很简单。

我想出了以下 (这是有效的答案)

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);

我确实将其与基于硬编码示例的编译器生成的 IL(使用 ildasm)进行了比较,但没有发现差异。

这是我为测试上述代码是否有效所做的:

var inst = Activator.CreateInstance(myType);

var p = inst.GetType().GetProperty("NewProp");
p.GetValue(inst, null);
p.SetValue(inst, 1, null);

作为参考,以下是 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

这里是“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

在问题的原始版本中,对 GetValue 的调用引发了 TargetInvocationException,其 InnerException 是 InvalidProgramException,显示“公共语言运行时检测到无效程序”。这是由于拼写错误(已更正);哦!

【问题讨论】:

  • 定义“不起作用”
  • 查看我的编辑。 (使用新属性时出现 InvalidProgramException。)
  • @Andreas 我没有得到InvalidProgramException...我只是没有看到“set”被调用(“get”工作正常)
  • 您是否尝试过在发出的程序集上运行 PEVerify?
  • 啊哈!找到了;查看编辑后的答案

标签: c# .net reflection reflection.emit


【解决方案1】:

你会踢自己的:

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

仔细观察。更近了。更近了。还是没有?

第 2 行与 NewPropGetIL 对话

如果这仍然不起作用,请确保您在TypeBuilder 中声明了基类型,并且Test 类是public,而不是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));

还有下面的脚:

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

地点:

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();
}

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

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;
    }
}

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

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

有了这个,很明显set有问题:

ManualTest

GetVal:1
0
SetVal:1
GetVal:1
123

SomeType

GetVal:1
0
GetVal:1
0

【讨论】:

  • 确实如此。发布到 stackoverflow 以供其他人查找拼写错误.. :) 我应该删除这个问题,但我会保留它,以供将来参考,如果有人想尝试类似的事情。
  • @Andreas 好吧,在这种情况下,也许我应该带回我之前的部分答案(参见编辑历史),它更多地关注我是如何找到它的...
  • 好主意。你怎么没有收到 InvalidProgramException?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-13
  • 1970-01-01
  • 2014-07-04
  • 2017-06-27
  • 2018-07-16
  • 1970-01-01
相关资源
最近更新 更多