【发布时间】: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