I want to identify all delegates of a class using reflection. I don't want to invoke them, just identify. I suppose that I may use getMembers()
like that :
Type t = myType;
MemberInfo[] mia = t.GetMembers(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic).OrderBy(item => item.Name).ToArr开发者_如何转开发ay();
foreach (MemberInfo m in mia)
{
//Find delegates
}
EDIT : Delegate example :
class ClassTest
{
public delegate void SampleEventHandler(object sender, EventArgs e);
}
It's possible with this method, or another ?
Thanks for your answers!
A delegate is just a (special, compiler-generated) class, that may be declared inside a class as a nested type - but bear in mind that they may also be declared directly in a namespace, like normal classes.
This will find you all nested types in a given type t
, that are delegates:
private IEnumerable<Type> DelegatesDefinedInType(Type t)
{
var nestedTypes = t.GetNestedTypes();
return nestedTypes.Where(typeof(Delegate).IsAssignableFrom);
}
精彩评论