views:

406

answers:

3

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
Nope. Not homework. Survey my 201 questions - not a student. Just your average overworked / underpaid coder.
tyndall
+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
Watch out; interfaces might not have a BaseType; nor "object"
Marc Gravell
@Marc: Good catch.
Jason
+3  A: 
foreach(Type type in assembly.GetTypes()) {
  var isChild = type.IsSubclassOf(typeof(parentClass))
}

Reference from MSDN.

Adrian Godong
+1 - cool additional info. won't be testing Subclass/Superclass relationships on my current app, but thanks.
tyndall
Oh, I read your question wrongly. Glad it helped.
Adrian Godong