Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET: Get all classes derived from specific class

I have a custom control and a number of controls derived from it. I need to get all classes in the current assembly that are derived from the main class and check their attributes. How to accomplish this?

like image 476
SharpAffair Avatar asked Dec 12 '22 15:12

SharpAffair


1 Answers

var type = typeof(MainClass);

var listOfDerivedClasses = Assembly.GetExecutingAssembly()
    .GetTypes()
    .Where(x => x.IsSubclassOf(type))
    .ToList();

foreach (var derived in listOfDerivedClasses)
{
   var attributes = derived.GetCustomAttributes(typeof(TheAttribute), true);

   // etc.
}
like image 163
Ben M Avatar answered Dec 30 '22 14:12

Ben M