.NET / C# - Reflection Help - Classes in an Assembly
-
12-09-2019 - |
Question
What is the best way to loop through an assembly, and for each class in the assembly list out it's "SuperClass"?
Solution
Assembly assembly = typeof(DataSet).Assembly; // etc
foreach (Type type in assembly.GetTypes())
{
if (type.BaseType == null)
{
Console.WriteLine(type.Name);
}
else
{
Console.WriteLine(type.Name + " : " + type.BaseType.Name);
}
}
Note that generics and nested types have funky names, any you might want to use FullName
to include the namespace.
OTHER TIPS
foreach(Type type in assembly.GetTypes()) {
var isChild = type.IsSubclassOf(typeof(parentClass))
}
Reference from MSDN.
Assembly.GetTypes
and Type.BaseType
:
Assembly a;
foreach(var type in a.GetTypes()) {
Console.WriteLine(
String.Format("{0} : {1}",
type.Name,
type.BaseType == null ? String.Empty : type.BaseType.Name
);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow