c# - 检查对象是否有带有委托(delegate)签名的方法

标签 c# delegates

如何检查对象是否具有与特定委托(delegate)具有相同签名的方法

    public delegate T GetSomething<T>(int aParameter);
    public static void Method<T>(object o, GetSomething<T> gs)
    {
        //check if 'o' has a method with the signature of 'gs'
    }

最佳答案

// You may want to tweak the GetMethods for private, static, etc...
foreach (var method in o.GetType().GetMethods(BindingFlags.Public))
{
    var del = Delegate.CreateDelegate(gs.GetType(), method, false);
    if (del != null)
    {
        Console.WriteLine("o has a method that matches the delegate type");
    }
}

关于c# - 检查对象是否有带有委托(delegate)签名的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2153403/

相关文章:

c# - 将 MySqlDataReader 存储在变量中

c# - 具有不同返回类型和参数数量的 Func 委托(delegate)列表

c# - ViewModel 更新 - 保存前更改属性

c# - 编译器歧义调用错误-具有Func <>或Action的匿名方法和方法组

ios - Objective-C UIViewController 委托(delegate)

ios - 我是否正确使用委托(delegate)?如果没有,我应该怎么做?

C# app.config 问题 - ConfigurationErrorsException : Unrecognized element 'setting'

c# - Windows 窗体 anchor ,以相同的比例保持面板内的控件?

ios - 调用可能导致对象释放的委托(delegate)回调时的正确内存管理

ios - 单击按钮将项目添加到另一个 View Controller TableView