目标:在运行时生成这样的方法:
public void InsertOnSubmit<T>(IQueryable<T> q, T o) where T : class, new()
{
(q as Table<T>).InsertOnSubmit(o);
}
我当前的代码是:
var tb = mb.DefineType("DatabaseDataRepository");
// define & implement other methods, etc
/* Define InsertOnSubmit<> method */
var insertOnSubmitMethod = tb.DefineMethod("InsertOnSubmit",
MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.Virtual |
MethodAttributes.NewSlot);
var genericInput = insertOnSubmitMethod.DefineGenericParameters("T")[0];
genericInput.SetGenericParameterAttributes(GenericParameterAttributes.ReferenceTypeConstraint | GenericParameterAttributes.DefaultConstructorConstraint);
insertOnSubmitMethod.SetParameters(typeof(IQueryable<>).MakeGenericType(genericInput), genericInput);
insertOnSubmitMethod.SetReturnType(null);
/* Implement InsertOnSubmit<> method */
var saveMethodGen = insertOnSubmitMethod.GetILGenerator();
saveMethodGen.Emit(OpCodes.Ldarg_1); // push first argument (collection)
saveMethodGen.Emit(OpCodes.Isinst, typeof(Table<>).MakeGenericType(genericInput)); // cast first argument to Table<>
saveMethodGen.Emit(OpCodes.Ldarg_2); // push second argument (element)
saveMethodGen.Emit(OpCodes.Callvirt, typeof(Table<>).GetMethod("InsertOnSubmit")); // insert second argument to table
saveMethodGen.Emit(OpCodes.Ret); // return from InsertOnSubmit method
但是在生成的实例上运行此方法,我得到:
An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)
,堆栈为at DatabaseDataRepository.InsertOnSubmit[T](IQueryable`1 , T )
我怀疑此行saveMethodGen.Emit(OpCodes.Callvirt, typeof(Table<>).GetMethod("InsertOnSubmit"));
中有问题-确实应该是typeof(Table<>).MakeGenericType(genericInput).GetMethod("InsertOnSubmit")
之类的东西-但这会抛出NotSupportedException
有什么办法解决吗? 谢谢。
答案 0 :(得分:1)
您必须使用静态System.Reflection.Emit.Typebuilder.GetMethod
方法来创建正确键入的MethodInfo
。
msdn指出:
返回与通用类型定义的指定方法相对应的指定构造通用类型的方法。
在您的情况下,将是:
Typebuilder.GetMethod(typeof(Table<>).MakeGenericType(genericInput), typeof(Table<>).GetMethod("InsertOnSubmit"))