我正在尝试使用 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,表示“公共(public)语言运行时检测到无效程序”。这是由于打字错误(已更正);哦!
最佳答案
你会踢自己:
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
关于c# - Reflection.Emit:在 getter/setter 中调用函数的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21531700/