• <bdo id='Q0xqN'></bdo><ul id='Q0xqN'></ul>
  • <tfoot id='Q0xqN'></tfoot>
    <i id='Q0xqN'><tr id='Q0xqN'><dt id='Q0xqN'><q id='Q0xqN'><span id='Q0xqN'><b id='Q0xqN'><form id='Q0xqN'><ins id='Q0xqN'></ins><ul id='Q0xqN'></ul><sub id='Q0xqN'></sub></form><legend id='Q0xqN'></legend><bdo id='Q0xqN'><pre id='Q0xqN'><center id='Q0xqN'></center></pre></bdo></b><th id='Q0xqN'></th></span></q></dt></tr></i><div id='Q0xqN'><tfoot id='Q0xqN'></tfoot><dl id='Q0xqN'><fieldset id='Q0xqN'></fieldset></dl></div>

    <small id='Q0xqN'></small><noframes id='Q0xqN'>

      <legend id='Q0xqN'><style id='Q0xqN'><dir id='Q0xqN'><q id='Q0xqN'></q></dir></style></legend>

      1. 从 methodinfo 获取委托

        Getting a delegate from methodinfo(从 methodinfo 获取委托)
        <i id='w26F5'><tr id='w26F5'><dt id='w26F5'><q id='w26F5'><span id='w26F5'><b id='w26F5'><form id='w26F5'><ins id='w26F5'></ins><ul id='w26F5'></ul><sub id='w26F5'></sub></form><legend id='w26F5'></legend><bdo id='w26F5'><pre id='w26F5'><center id='w26F5'></center></pre></bdo></b><th id='w26F5'></th></span></q></dt></tr></i><div id='w26F5'><tfoot id='w26F5'></tfoot><dl id='w26F5'><fieldset id='w26F5'></fieldset></dl></div>
          • <bdo id='w26F5'></bdo><ul id='w26F5'></ul>
            <legend id='w26F5'><style id='w26F5'><dir id='w26F5'><q id='w26F5'></q></dir></style></legend>

              <small id='w26F5'></small><noframes id='w26F5'>

                  <tbody id='w26F5'></tbody>

                  <tfoot id='w26F5'></tfoot>
                  本文介绍了从 methodinfo 获取委托的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个下拉列表,它通过检查类的方法并包括与特定签名匹配的方法来填充.问题在于从列表中获取所选项目并让委托在类中调用该方法.第一种方法有效,但我无法弄清楚第二种方法的一部分.

                  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.

                  例如,

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

                  如何使第二种方法的注释掉部分起作用?如何将 MethodInfo 转换为委托?

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

                  谢谢!

                  这是可行的解决方案.

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

                  推荐答案

                  您需要调用某种形式的 Delegate.CreateDelegate(),取决于所讨论的方法是静态方法还是实例方法.

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

                  这篇关于从 methodinfo 获取委托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)
                  Parameter count mismatch with Invoke?(参数计数与调用不匹配?)
                  How to store delegates in a List(如何将代表存储在列表中)
                  How delegates work (in the background)?(代表如何工作(在后台)?)
                  C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)
                  Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)

                      <tbody id='lEd0s'></tbody>

                  1. <i id='lEd0s'><tr id='lEd0s'><dt id='lEd0s'><q id='lEd0s'><span id='lEd0s'><b id='lEd0s'><form id='lEd0s'><ins id='lEd0s'></ins><ul id='lEd0s'></ul><sub id='lEd0s'></sub></form><legend id='lEd0s'></legend><bdo id='lEd0s'><pre id='lEd0s'><center id='lEd0s'></center></pre></bdo></b><th id='lEd0s'></th></span></q></dt></tr></i><div id='lEd0s'><tfoot id='lEd0s'></tfoot><dl id='lEd0s'><fieldset id='lEd0s'></fieldset></dl></div>
                      <tfoot id='lEd0s'></tfoot>

                          <bdo id='lEd0s'></bdo><ul id='lEd0s'></ul>

                          <small id='lEd0s'></small><noframes id='lEd0s'>

                            <legend id='lEd0s'><style id='lEd0s'><dir id='lEd0s'><q id='lEd0s'></q></dir></style></legend>