Как я могу проверить, имеет ли объект метод с той же подписью определенного делегата
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'
}
Вы можете сделать это, нахождение всех методов в типе, имеющих тот же тип возврата, а той же последовательность типов в параметрах:
var matchingMethods = o.GetType().GetMethods().Where(mi =>
mi.ReturnType == gs.Method.ReturnType
&& mi.GetParameters().Select(pi => pi.ParameterType)
.SequenceEqual(gs.Method.GetParameters().Select(pi => pi.ParameterType)));
// 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");
}
}