Take the 2-minute tour ×

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;
share|improve this question
It should work in all cases as long as CreateType has been called on the containing type(builder). –  leppie Jan 19 '11 at 14:24
add comment

2 Answers

up vote 3 down vote accepted

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.GetParameters().Select(p => p.ParameterType).ToArray());
share|improve this answer
I rather thought somebody would suggest this kind of lookup. While fairly reliable it's not 100%, I think, bacause what if you have two overloads, the only difference between them being one is 'out' and one is 'ref'? –  Qwertie Jun 13 '10 at 16:06
Ah, the C# compiler won't allow two overloads that differ only by 'ref' and 'out'. Perhaps your method really will work 100% reliably, I guess I will go for it. It's a slight pain though, as I'm using .NET 2.0 - no LINQ. –  Qwertie Jun 13 '10 at 19:15
One problem though, GetMethod only gets public methods. I should use a different overload to be safe. –  Qwertie Jun 13 '10 at 19:19
Uh oh! This is not 100% reliable - in CIL, methods can be overloaded on return type (or so I read at simple-talk.com/community/blogs/simonc/archive/2010/07/16/…) AND based on custom modifiers (simple-talk.com/community/blogs/simonc/archive/2010/11/26/…) –  Qwertie Apr 12 '11 at 1:26
@Qwertie: If that affects you then I suppose you'll need to call GetMethods and then filter the returned MethodInfo[] array using parameters, return type, modifiers etc until you get down to a single method. –  LukeH Apr 12 '11 at 1:48
add comment

You need to use the GetMethod overload that takes an array of types. Method overloads are selected for binding by the parameters passed to them. Since you are not actually calling a method when getting their metadata via reflection, you have to provide an ordered array of Type objects to get the right MethodInfo object:

Type.GetMethod Method (String, Type[])

Used like:

MethodInfo mi = loadedType.GetMethod(

Assiming methodBuilder.Name is "MyMethod", that would bind the following:

public <returnType> MyMethod(string param1, int param2);

Remember that the return type is not a part of a method signature, only the method name and its parameter types are.

share|improve this answer
add comment

Your Answer


