如何检查对象是否具有与特定委托(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/