Reflection.Emit: How to convert MethodBuilder to RuntimeMethodInfo reliably?

Posted by Qwertie on Stack Overflow See other posts from Stack Overflow or by Qwertie
Published on 2010-06-13T05:19:49Z Indexed on 2010/06/13 5:32 UTC
Read the original article Hit count: 335

Filed under:
|
|
|

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?

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET