Getting a delegate from methodinfo

2019-01-23 22:54发布

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;
}

2条回答
我命由我不由天
2楼-- · 2019-01-23 23:46
private 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);
}
查看更多
何必那么认真
3楼-- · 2019-01-23 23:48

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

查看更多
登录 后发表回答