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.
But as soon as im trying to instanciate my new object, im receiving a BadImageFormatException
This seems to be a similar issue, but unresolved Is there any way to instrument 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
.
Bonus-Question: Why is there an BadImageFormatException
?
Additional Info:
- .Net-Framework 4.6.1
- Compiler-target is x86
- Never ever did
Reflection.Emit
before
Aucun commentaire:
Enregistrer un commentaire