• <tfoot id='IkWvC'></tfoot>
      • <bdo id='IkWvC'></bdo><ul id='IkWvC'></ul>

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

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

        针对委托检查 MethodInfo

        Checking a MethodInfo against a delegate(针对委托检查 MethodInfo)
          <i id='Mdy20'><tr id='Mdy20'><dt id='Mdy20'><q id='Mdy20'><span id='Mdy20'><b id='Mdy20'><form id='Mdy20'><ins id='Mdy20'></ins><ul id='Mdy20'></ul><sub id='Mdy20'></sub></form><legend id='Mdy20'></legend><bdo id='Mdy20'><pre id='Mdy20'><center id='Mdy20'></center></pre></bdo></b><th id='Mdy20'></th></span></q></dt></tr></i><div id='Mdy20'><tfoot id='Mdy20'></tfoot><dl id='Mdy20'><fieldset id='Mdy20'></fieldset></dl></div>
          <legend id='Mdy20'><style id='Mdy20'><dir id='Mdy20'><q id='Mdy20'></q></dir></style></legend>
            <tbody id='Mdy20'></tbody>
          <tfoot id='Mdy20'></tfoot>
        1. <small id='Mdy20'></small><noframes id='Mdy20'>

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

                  问题描述

                  如何确定 MethodInfo 是否适合不同的委托类型?

                  How can I determine if a MethodInfo fits a distinct Delegate Type?

                  bool IsMyDelegate(MethodInfo method);
                  

                  我得到了一个 MethodInfo 对象,想知道它是否适合委托接口.除了明显的

                  I'm given a MethodInfo object and want to know if it fits the delegate interface. Apart from the obvious

                      private bool IsValidationDelegate(MethodInfo method)
                      {
                          var result = false;
                          var parameters = method.GetParameters();
                          if (parameters.Length == 2 &&
                              parameters[0].ParameterType == typeof(MyObject1) &&
                              parameters[1].ParameterType == typeof(MyObject2) &&
                              method.ReturnType == typeof(bool))
                          {
                              result = true;
                          }
                          else
                          {
                              m_Log.Error("Validator:IsValidationDelegate", "Method [...] is not a ValidationDelegate.");
                          }
                          return result;
                      }
                  

                  推荐答案

                  如果method是静态方法:

                  bool isMyDelegate =
                    (Delegate.CreateDelegate(typeof(MyDelegate), method, false) != null);
                  

                  如果 method 是实例方法:

                  bool isMyDelegate =
                    (Delegate.CreateDelegate(typeof(MyDelegate), someObj, method, false) != null)
                  

                  (不幸的是,在这种情况下您需要一个实例,因为 Delegate.CreateDelegate 将尝试绑定一个委托实例,尽管在这种情况下,我们关心的是委托 是否可以 被创建还是不是.)

                  (Unfortunately you need an instance in this case because Delegate.CreateDelegate is going to try to bind a delegate instance, even though in this case all we care about it whether the delegate could be created or not.)

                  在这两种情况下,诀窍基本上是要求 .NET 从 MethodInfo 创建所需类型的委托,但如果方法的签名错误,则返回 null 而不是抛出异常.然后针对 null 的测试告诉我们委托是否具有正确的签名.

                  In both cases, the trick is basically to ask .NET to create a delegate of the desired type from the MethodInfo, but to return null rather than throwing an exception if the method has the wrong signature. Then testing against null tells us whether the delegate had the right signature or not.

                  请注意,因为这实际上试图创建委托,它还将为您处理所有委托差异规则(例如,当方法返回类型兼容但与委托返回类型不完全相同时).

                  Note that because this actually tries to create the delegate it will also handle all the delegate variance rules for you (e.g. when the method return type is compatible but not exactly the same as the delegate return type).

                  这篇关于针对委托检查 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() 和泛型:错误绑定到目标方法)
                • <small id='hzR3b'></small><noframes id='hzR3b'>

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

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

                            <tfoot id='hzR3b'></tfoot>