samedi 8 février 2020

C# Using Reflection list only top classes excluding linked ones

I have a rather simple collection of many (too many..) classes in a specific namespace. They have no nested classes, but the do have linked ones. Example (simplified):

namespace ConsoleApp1.Methods
{
public class Method100Response201
    {
    public string aName { get; set; }
    public string R1_201 { get; set; } = "R1_201";
    public void DoSpecialThing()
        { Console.WriteLine ("Something Blue.."); }
    public Method100Response201_1 super { get; set; }
    public Method100Response201()
        {
        super = new Method100Response201_1();
        }
    }

public class Method100Response201_1
    {
    public string aName { get; set; }
    public string R1_201_1 { get; set; } = "R1_201_1";

    }

public class Method100Response404
    {
    public string R1_04 { get; set; } = "R1_04";
    public void DoSpecialThing()
        { Console.WriteLine ("Something Old.."); }
    }
public class Method200Response200
    {
    public string R2_02 { get; set; } = "R2_02";
    public void DoSpecialThing()
        { Console.WriteLine ("Something New.."); }
    }
}

This is what I have (consider that all my classes are in "ConsoleApp1.Methods"):

            Type[] typelist = GetTypesInNamespace (Assembly.GetExecutingAssembly (), "ConsoleApp1.Methods");
        for ( int i = 0; i < typelist.Length; i++ )
            {
            Console.WriteLine ("Found Response Model: "+ typelist[i].Name);
            }
        Console.ReadKey ();

How could I transform my code so that I get a list of only the top classes (consider as top classes those not present as types of properties of other classes) , i.e. for this example, only Method100Response201 (not the Method100Response201_1 because is used by Method100Response201), Method100Response404 and Method200Response200 ?





Aucun commentaire:

Enregistrer un commentaire