What is the best way to loop through an assembly, and for each class in the assembly list out it's "SuperClass"?
+1
A:
homework?
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.
Marc Gravell
2009-07-09 13:08:32
Nope. Not homework. Survey my 201 questions - not a student. Just your average overworked / underpaid coder.
tyndall
2009-07-09 13:14:14
+1
A:
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
);
}
Jason
2009-07-09 13:09:05
+3
A:
foreach(Type type in assembly.GetTypes()) {
var isChild = type.IsSubclassOf(typeof(parentClass))
}
Reference from MSDN.
Adrian Godong
2009-07-09 13:11:52
+1 - cool additional info. won't be testing Subclass/Superclass relationships on my current app, but thanks.
tyndall
2009-07-09 13:24:10