Reflection.Emit的:如何转换MethodBuilder可靠RuntimeMethodInfo? [英] Reflection.Emit: How to convert MethodBuilder to RuntimeMethodInfo reliably?
问题描述
动态生成类型并调用TypeBuilder.CreateType后,我想创建一个委托指向新类型的方法。但是,如果我用code像
loadedType = typeBuilder.CreateType();
myDelegate =(MyDelegate)Delegate.CreateDelegate(
typeof运算(MyDelegate),methodBuilder);
重用methodBuilder作为MethodInfo的,我得到的异常的MethodInfo必须是一个RuntimeMethodInfo。现在通常我可以重新获得的MethodInfo与
MethodInfo的MI = loadedType.GetMethod(methodBuilder.Name);
myDelegate =(MyDelegate)Delegate.CreateDelegate(typeof运算(MyDelegate),MI);
不过,我的课可能包含多个同名的重载方法。我如何确保我得到正确的? do方法有一定持续性的标识符,我可以看看在loadedType?
更新:好吧,这里是我用什么来重新获得的MethodInfo。我只是希望我可以肯定它适用于所有的情况。
私有静态的MethodInfo ReacquireMethod(类型类型,MethodInfo的方法)
{
的BindingFlags标志= BindingFlags.DeclaredOnly;
旗帜| =(?method.IsPublic BindingFlags.Public:BindingFlags.NonPublic可);
旗帜| =(?method.IsStatic BindingFlags.Static:BindingFlags.Instance);
MethodInfo的M = type.GetMethod(method.Name,标志,空,
ParameterTypes(方法),NULL);
Debug.Assert的(M!= NULL);
返回米;
}
据我所知没有永久共享标识符。
方法的重载通过其参数列表区分开来,所以我的猜测是,你需要调用<一个href="http://msdn.microsoft.com/en-us/library/system.reflection.emit.methodbuilder.getparameters.aspx"相对=nofollow> GetParameters
对 methodBuilder
然后再翻译回来信息参数[]
阵列成键入[]
数组传递给了的相应的 GetMethod的
超载:
MethodInfo的MI = loadedType.GetMethod(
methodBuilder.Name,
。methodBuilder.GetParameters()选择(P =&GT; p.ParameterType).ToArray());
After generating a type dynamically and calling TypeBuilder.CreateType, I want to create a delegate that points to a method in the new type. But if I use code like
loadedType = typeBuilder.CreateType();
myDelegate = (MyDelegate)Delegate.CreateDelegate(
typeof(MyDelegate), methodBuilder);
Reusing the methodBuilder as a methodInfo, I get the exception "MethodInfo must be a RuntimeMethodInfo". Now normally I can re-acquire the MethodInfo with
MethodInfo mi = loadedType.GetMethod(methodBuilder.Name);
myDelegate = (MyDelegate)Delegate.CreateDelegate(typeof(MyDelegate), mi);
But my class may contain several overloaded methods with the same name. How do I make sure I get the right one? Do methods have some persistent identifier I could look up in loadedType?
Update: okay, here's what I'm using to re-acquire the MethodInfo. I just wish I could be sure it works in all cases.
private static MethodInfo ReacquireMethod(Type type, MethodInfo method)
{
BindingFlags flags = BindingFlags.DeclaredOnly;
flags |= (method.IsPublic ? BindingFlags.Public : BindingFlags.NonPublic);
flags |= (method.IsStatic ? BindingFlags.Static : BindingFlags.Instance);
MethodInfo m = type.GetMethod(method.Name, flags, null,
ParameterTypes(method), null);
Debug.Assert(m != null);
return m;
}
As far as I'm aware there's no persistent shared identifier.
Overloads of a method are distinguished by their parameter lists, so my guess is that you'll need to call GetParameters
on methodBuilder
and then translate the returned ParameterInfo[]
array into a Type[]
array to pass to the the appropriate GetMethod
overload:
MethodInfo mi = loadedType.GetMethod(
methodBuilder.Name,
methodBuilder.GetParameters().Select(p => p.ParameterType).ToArray());
这篇关于Reflection.Emit的:如何转换MethodBuilder可靠RuntimeMethodInfo?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!