51

I have a drop down list that is populated by inspecting a class's methods and including those that match a specific signature. The problem is in taking the selected item from the list and getting the delegate to call that method in the class. The first method works, but I cannot figure out part of the second.

For example,

public delegate void MyDelegate(MyState state);

public static MyDelegate GetMyDelegateFromString(string methodName)
{
    switch (methodName)
    {
        case "CallMethodOne":
            return MyFunctionsClass.CallMethodOne;
        case "CallMethodTwo":
            return MyFunctionsClass.CallMethodTwo;
        default:
            return MyFunctionsClass.CallMethodOne;
    }
}

public static MyDelegate GetMyDelegateFromStringReflection(string methodName)
{
    MyDelegate function = MyFunctionsClass.CallMethodOne;

    Type inf = typeof(MyFunctionsClass);
    foreach (var method in inf.GetMethods())
    {
        if (method.Name == methodName)
        {
            //function = method;
            //how do I get the function to call?
        }
    }

    return function;
}

How do I get the commented out section of the second method to work? How do I cast the MethodInfo into the delegate?

Thanks!

Edit: Here is the working solution.

public static MyDelegate GetMyDelegateFromStringReflection(string methodName)
{
    MyDelegate function = MyFunctionsClass.CallMethodOne;

    Type inf = typeof(MyFunctionsClass);
    foreach (var method in inf.GetMethods())
    {
        if (method.Name == methodName)
        {
            function = (MyDelegate)Delegate.CreateDelegate(typeof(MyDelegate), method);
        }
    }

    return function;
}
nawfal
  • 70,104
  • 56
  • 326
  • 368
Ty.
  • 3,888
  • 3
  • 24
  • 31

3 Answers3

34
public static Delegate CreateDelegate(this MethodInfo methodInfo, object target) {
    Func<Type[], Type> getType;
    var isAction = methodInfo.ReturnType.Equals((typeof(void)));
    var types = methodInfo.GetParameters().Select(p => p.ParameterType);

    if (isAction) {
        getType = Expression.GetActionType;
    }
    else {
        getType = Expression.GetFuncType;
        types = types.Concat(new[] { methodInfo.ReturnType });
    }

    if (methodInfo.IsStatic) {
        return Delegate.CreateDelegate(getType(types.ToArray()), methodInfo);
    }

    return Delegate.CreateDelegate(getType(types.ToArray()), target, methodInfo.Name);
}
Sagi
  • 8,972
  • 3
  • 33
  • 41
  • 2
    @Graviton Depends on whether you happen to have the method signature as a `Delegate` type or not. In this case, the OP seems to indicate that he can pre-assume `MyDelegate`, in which case Nate and the solution the OP incorporated are best. This one, on the other hand, is an excellent answer for the *other* question, which is what to do if you have no access to an appropriate `Delegate` type (i.e. typically, you obtained a `MethodInfo` out-of-the blue by name alone)... but need precisely such a delegate type in order to create a delegate (a somewhat notorious `.NET` chicken and egg problem). – Glenn Slayden Jul 15 '17 at 12:24
  • 2
    This solution is not applicable for methods with out / ref argument types. – MaMazav Aug 31 '18 at 12:04
  • I've noted the method is marked as `private`. Unless you want actually to use this as an extension method, you should declare it as `public`. – Arsen Khachaturyan Jan 20 '20 at 20:59
  • 1
    @ArsenKhachaturyan you are right, probably a copy/paste issue. – Sagi Jan 21 '20 at 09:24
25

You'll need to call some form of Delegate.CreateDelegate(), depending on whether the method in question is a static or instance method.

Sachin Joseph
  • 18,928
  • 4
  • 42
  • 62
Nate Kohari
  • 2,216
  • 17
  • 13
1

Here is a simpler version of Sagi's solution using Expression.GetDelegateType() instead of manually detecting whether an Action or Func is needed:

public static Delegate CreateDelegate(this MethodInfo methodInfo, object target)
{
    var parmTypes = methodInfo.GetParameters().Select(parm => parm.ParameterType);
    var parmAndReturnTypes = parmTypes.Append(methodInfo.ReturnType).ToArray();
    var delegateType = Expression.GetDelegateType(parmAndReturnTypes);

    if (methodInfo.IsStatic)
        return methodInfo.CreateDelegate(delegateType);
    return methodInfo.CreateDelegate(delegateType, target);
}
Anders Carstensen
  • 2,949
  • 23
  • 23
  • So short! +1 I just run into issue when a method has a nullable value type parameter (like int?) with a default value. But it seems that it is rather a bug in ASP.NET Core, where I send this delegate to register routing, it's probably not related to this helper method. – Al Kepp Jan 04 '23 at 14:40