none
Listing all the base classes, sub classes and interfaces in a DLL using reflection in C# RRS feed

  • Question

  • I have 3 classes (C1, C2, C3), 1 interface(IC) in a C# class library.

    Class C2 inherits class C1.

    Class C3 implements interface IC.

    Now in my program I want to list all the base classes first and then interfaces. For each base class I wanted to display all the child classes and for each interface I want to list all the classes that implement the interface.

    In my program I would be loading a dll using below statement and want to achieve above said functionality in C#.

              

    Assembly assemblies = Assembly.LoadFrom(dllName);

    Is there any method that is available in C# that gives the list of sub classes for a class or list of classes implementing a particular interface? If not will it not be good to provide the methods and if there are already ones then please route me to those methods.

    Also I would like to know if there is a property that tells if a type is an abstract class, interface or a class?

    I would appreciate if some one can help me on this.


    Akkiraju Ivaturi


    • Edited by Akkiraju Ivaturi Sunday, August 12, 2012 1:46 AM Wanted to add extra question
    Sunday, August 12, 2012 1:31 AM

Answers

  • Hi Akkiraju,

    Welcome to the MSDN Forum.

    >> I want to list all the base classes first and then interfaces. 

    In this example, the based class is C1, right?

    >>For each base class I wanted to display all the child classes 

    To achieve this purpose, you need to check each class, as far as I know, there is no such property for your reference now.

    >>for each interface I want to list all the classes that implement the interface.

    Similar to above.

    Here is the properties for your reference:

                foreach (Type ty in ass.GetTypes())
                {
                    Console.WriteLine(ty.Name);
                    Console.WriteLine(" IsAbstract:" + ty.IsAbstract);
                    Console.WriteLine(" IsClass:" + ty.IsClass);
                    if (ty.BaseType != null)
                        Console.WriteLine(" BaseType:" + ty.BaseType.Name);
                    Console.Write(" Interfaces:");
                    foreach (Type interf in ty.GetInterfaces())
                        Console.Write("    " + interf.Name);
                    Console.WriteLine();
                }

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, August 13, 2012 11:14 AM
    Moderator

All replies

  • Hi Akkiraju,

    Welcome to the MSDN Forum.

    >> I want to list all the base classes first and then interfaces. 

    In this example, the based class is C1, right?

    >>For each base class I wanted to display all the child classes 

    To achieve this purpose, you need to check each class, as far as I know, there is no such property for your reference now.

    >>for each interface I want to list all the classes that implement the interface.

    Similar to above.

    Here is the properties for your reference:

                foreach (Type ty in ass.GetTypes())
                {
                    Console.WriteLine(ty.Name);
                    Console.WriteLine(" IsAbstract:" + ty.IsAbstract);
                    Console.WriteLine(" IsClass:" + ty.IsClass);
                    if (ty.BaseType != null)
                        Console.WriteLine(" BaseType:" + ty.BaseType.Name);
                    Console.Write(" Interfaces:");
                    foreach (Type interf in ty.GetInterfaces())
                        Console.Write("    " + interf.Name);
                    Console.WriteLine();
                }

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, August 13, 2012 11:14 AM
    Moderator
  • Yes C1 is the base class. And thanks for your response.

    Akkiraju Ivaturi

    Monday, August 13, 2012 6:16 PM