充分利用MethodInfo的委托 [英] Getting a delegate from methodinfo

查看:141
本文介绍了充分利用MethodInfo的委托的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个下拉列表是通过检查一类的方法和包括那些符合特定签名填充。问题是,从列表中服用所选择的项目和获得委托来调用类的方法。第一种方法的工作原理,但我想不出第二个的一部分。



例如,

 公共委托无效MyDelegate(MyState状态); 

公共静态MyDelegate GetMyDelegateFromString(字符串方法名)
{
开关(方法名)
{
案CallMethodOne:
返回MyFunctionsClass。 CallMethodOne;
案CallMethodTwo:
返回MyFunctionsClass.CallMethodTwo;
默认:
返回MyFunctionsClass.CallMethodOne;
}
}

公共静态MyDelegate GetMyDelegateFromStringReflection(字符串方法名)
{
MyDelegate功能= MyFunctionsClass.CallMethodOne;

型的inf = typeof运算(MyFunctionsClass);
的foreach(在inf.GetMethods VAR方法())
{
如果(method.Name ==方法名)
{
//功能=方法;
//我怎么要调用的函数?
}
}

返回功能;
}



我如何获得第二个方法的注释部分工作?我怎么投的的MethodInfo 进入委托?



谢谢!



编辑:这里是工作的解决方案

 公共静态MyDelegate GetMyDelegateFromStringReflection(串方法名)
{
MyDelegate功能= MyFunctionsClass.CallMethodOne;

型的inf = typeof运算(MyFunctionsClass);
的foreach(在inf.GetMethods VAR方法())
{
如果(method.Name ==方法名)
{
功能=(MyDelegate)Delegate.CreateDelegate (typeof运算(MyDelegate),法);
}
}

返回功能;
}


解决方案

您将需要调用某种形式的 Delegate.CreateDelegate(),取决于问题的方法是否是静态或实例方法。


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

解决方案

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

这篇关于充分利用MethodInfo的委托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆