检查的MethodInfo对委托 [英] Checking a MethodInfo against a delegate

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

问题描述

我怎样才能确定是否适合M​​ethodInfo的一个独特的委托类型?

 布尔IsMyDelegate(MethodInfo的方法);

编辑:
我给出的MethodInfo对象,想知道它是否符合委托接口。
除了明显

 私人布尔IsValidationDelegate(MethodInfo的方法)
    {
        VAR的结果=假;
        VAR参数= method.GetParameters();
        如果(parameters.Length == 2和;&放大器;
            参数[0] == .ParameterType typeof运算(MyObject1)及和放大器;
            参数[1] .ParameterType == typeof运算(MyObject2)及和放大器;
            method.ReturnType == typeof运算(布尔))
        {
            结果=真;
        }
        其他
        {
            m_Log.Error(验证:IsValidationDelegate,方法[...]不是ValidationDelegate。);
        }
        返回结果;
    }


解决方案

如果是一个静态方法:

 布尔isMyDelegate =
  (Delegate.CreateDelegate(typeof运算(MyDelegate),方法,FALSE)!= NULL);

如果是一个实例方法:

 布尔isMyDelegate =
  (Delegate.CreateDelegate(typeof运算(MyDelegate),someObj中,法,假的)!= NULL)

(不幸的是你需要一个实例在这种情况下,因为Delegate.CreateDelegate是要尝试绑定委托实例,即使在这种情况下,我们关心的它是否与委托的可能的创建或不是。)

在这两种情况下,关键是基本要求.NET创建从MethodInfo的所需类型的委托,而是返回null而不是抛出一个异常,如果该方法的签名错误。然后测试对空告诉我们委托是否有正确的签名与否。

请注意,由于这实际上试图创建委托它也将处理所有为你委托方差规则(例如,当该方法返回类型是兼容的,但不完全一样的委托返回类型)。

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

bool IsMyDelegate(MethodInfo method);

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

解决方案

If method is a static method:

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

If method is an instance method:

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

(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.)

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对委托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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