我正在尝试使用reflection.emit创建一种动态类型,该类型从给定的类型继承,并添加了一个新属性,其基本类型的Getter/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);
i确实将其与基于硬编码样本的编译器生成的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抛出了TargetInvocation Exception,其Innerexception是无效的Programexception,上面写着"常见语言运行时检测到无效的程序"。这是由于错别字(自更正)造成的;D'OH!
你会踢自己:
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