private List<MethodInfo> GetMethodsWithAttribute(Type attribute, Type t)
{
List<MethodInfo> mi = null;
// Traverse the hierarchy to find all methods with the particular attribute
Type baseType = t;
while (baseType != null && baseType != typeof(object))
{
// Get all methods which are declared on this type, instance and public or nonpublic
MethodInfo[] mis = baseType.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
foreach (MethodInfo m in mis)
{
// For each method find if attribute is present, the return type is void and the method is not virtual
if (m.IsDefined(attribute, false))
{
if (mi == null) mi = new List<MethodInfo>();
mi.Add(m);
}
}
baseType = baseType.BaseType;
}
mi?.Reverse(); // We should invoke the methods starting from base
return mi;
}