针对委托检查 MethodInfo

7

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

问题描述

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

How can I free 免费vpn下载 vpn determine if a MethodInfo fits a 免费vpn下载 distinct Delegate Type?

bool IsMyDelegate(MethodInfo method);

我得到了一个 vpn下载 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)
    {
      free vpn   var result = false;
 vpn free        var parameters vpn free = method.GetParameters();
       vpn下载  if (parameters.Length == 2 &&
          free vpn vpn下载   parameters[0].ParameterType == typeof(MyObject1) vpn free &&
            parameters[1].ParameterType 免费vpn下载 == typeof(MyObject2) &&
    vpn下载         method.ReturnType == typeof(bool))
     vpn free    {
       vpn free      result = true;
        }
        else
        {
  免费vpn        免费vpn    m_Log.Error("Validator:IsValidationDelegate", "Method [...] is vpn free not a ValidationDelegate.");
        }
  vpn free  vpn free      return result;
    }

推荐答案

如果method是静态方法:

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

如果 method 是实例方法:

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

(不幸的是,在这种情况下您需要一个实例,因为 免费vpn 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 免费vpn we care about 免费vpn it whether the delegate could be created or not.)

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

In both vpn free cases, the trick is basically free vpn to vpn下载 vpn free ask .NET to create a 免费vpn delegate of the 免费vpn 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 vpn下载 had the right signature or free vpn not.

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

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

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

The End

相关推荐

C# 中的多播委托奇怪行为?
Multicast delegate weird behavior in 免费vpn C#?(C# 中的多播委托奇怪行为?)...
2023-11-11 C#/.NET开发问题
6

参数计数与调用不匹配?
Parameter count mismatch with Invoke?(参数计数与调用不匹配?)...
2023-11-11 C#/.NET开发问题
26

如何将代表存储在列表中
How to store delegates in a List(如何将代表存储在列表中)...
2023-11-11 C#/.NET开发问题
6

代表如何工作(在后台)?
How delegates work (in 免费vpn the background)?(代表如何工作(在后台)?)...
2023-11-11 C#/.NET开发问题
5

没有 EndInvoke 的 C# 异步调用?
C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)...
2023-11-11 C#/.NET开发问题
2

Delegate.CreateDelegate() 和泛型:错误绑定到目标方法
Delegate.CreateDelegate() and generics: Error 免费vpn下载 binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)...
2023-11-11 C#/.NET开发问题
14