[英]Reflection.Emit throws BadImageFormatException
Im trying to generate a new class/object at runtime. 我试图在运行时生成一个新的类/对象。
After reading How to create a private property using PropertyBuilder , i've managed to get everyting implemented and everything is like i need it. 在阅读了如何使用PropertyBuilder创建私有财产之后 ,我设法实现了一切,一切都像我需要的。
But as soon as im trying to instanciate my new object, im receiving a BadImageFormatException
但是,一旦我试图实例化我的新对象,我就会收到
BadImageFormatException
This seems to be a similar issue, but unresolved Is there any way to instrument System.Reflection.Emit? 这似乎是一个类似的问题,但尚未解决是否有任何方法可以检测System.Reflection.Emit?
Here my code: 这是我的代码:
Field: 领域:
internal class Field {
public string FieldName;
public Type FieldType;
public string Value;
}
Generator-Code: 发电机码:
var xx = new List<Field>(new[] { new Field { FieldName = "Name", FieldType = typeof(string), Value = "Hello World" },
new Field { FieldName = "Id", FieldType = typeof(int), Value = "1" } });
this.DoVodoo(xx);
The Magic 魔术
private dynamic DoVodoo(IEnumerable<Field> fields) {
var aName = new AssemblyName("DynamicAssemblyExample");
var ab = AppDomain.CurrentDomain.DefineDynamicAssembly(aName, AssemblyBuilderAccess.RunAndSave);
var mb = ab.DefineDynamicModule(aName.Name, aName.Name + ".dll");
// Create class with all needed Properties
var tb = mb.DefineType("ParamRow", TypeAttributes.Public, typeof(object));
foreach (var field in fields) {
var pb = tb.DefineProperty(field.FieldName, PropertyAttributes.None, CallingConventions.HasThis, field.FieldType, null);
var getSetAttr = MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig;
// Define the "get" accessor method for the Property.
var custNameGetPropMthdBldr = tb.DefineMethod($"get_{field.FieldName}", getSetAttr, typeof(string), Type.EmptyTypes);
var custNameGetIL = custNameGetPropMthdBldr.GetILGenerator();
custNameGetIL.Emit(OpCodes.Ldarg_0);
custNameGetIL.Emit(OpCodes.Ldfld, custNameGetPropMthdBldr);
custNameGetIL.Emit(OpCodes.Ret);
// Define the "set" accessor method for CustomerName.
var custNameSetPropMthdBldr = tb.DefineMethod($"set_{field.FieldName}", getSetAttr, null, new[] { typeof(string) });
var custNameSetIL = custNameSetPropMthdBldr.GetILGenerator();
custNameSetIL.Emit(OpCodes.Ldarg_0);
custNameSetIL.Emit(OpCodes.Ldarg_1);
//custNameSetIL.Emit(OpCodes.Stfld, custNameGetPropMthdBldr);
custNameSetIL.Emit(OpCodes.Stfld, custNameSetPropMthdBldr);
custNameSetIL.Emit(OpCodes.Ret);
// Last, we must map the two methods created above to our PropertyBuilder to
// their corresponding behaviors, "get" and "set" respectively.
pb.SetGetMethod(custNameGetPropMthdBldr);
pb.SetSetMethod(custNameSetPropMthdBldr);
}
var finalType = tb.CreateType();
var result = new List<object>();
foreach (var field in fields) {
var inst = ab.CreateInstance(finalType.Name);
finalType.GetProperty(field.FieldName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).SetValue(inst, field.Value); //<-- Here comes the trouble
result.Add(inst);
}
return result;}
Any help is appreciated on how to instanciate my newly created Type ParamRow
. 感谢您提供有关如何实例化我新创建的Type
ParamRow
。
Bonus-Question: Why is there an BadImageFormatException
? 奖励问题:为什么会有
BadImageFormatException
?
Additional Info: 附加信息:
Reflection.Emit
before Reflection.Emit
。 The .Message
of the exception you get is the important bit: 重要的是您收到的异常的
.Message
:
Field token out of range.
字段令牌超出范围。
This tells you that it isn't understanding what field you want to use in ldfld
/ stfld
- which is because you're passing it the method token ( custNameGetPropMthdBldr
/ custNameSetPropMthdBldr
) instead of a field token. 这表明您不了解要在
ldfld
/ stfld
使用哪个字段-这是因为要向其传递方法令牌( custNameGetPropMthdBldr
/ custNameSetPropMthdBldr
)而不是字段令牌。
You need to define and use a field: 您需要定义和使用一个字段:
var fb = tb.DefineField("__" + field.FieldName, field.FieldType, FieldAttributes.Private);
// ...
custNameGetIL.Emit(OpCodes.Ldarg_0);
custNameGetIL.Emit(OpCodes.Ldfld, fb);
custNameGetIL.Emit(OpCodes.Ret);
// ...
custNameSetIL.Emit(OpCodes.Ldarg_0);
custNameSetIL.Emit(OpCodes.Ldarg_1);
custNameSetIL.Emit(OpCodes.Stfld, fb);
custNameSetIL.Emit(OpCodes.Ret);
Note also that it is more efficient to use the Type
than the name when creating objects via reflection; 还要注意,通过反射创建对象时,使用
Type
比使用名称更有效。 this works fine: 这工作正常:
var inst = Activator.CreateInstance(finalType);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.