C# Using Reflection list only top classes excluding linked ones

橙三吉。 提交于 2021-01-28 09:33:44

问题


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 Method100Response201()
        {
            super = new Method100Response201_1();
        }

        public string aName { get; set; }
        public string R1_201 { get; set; } = "R1_201";
        public Method100Response201_1 super { get; set; }

        public void DoSpecialThing()
        {
             Console.WriteLine ("Something Blue.."); //just complimentary-no use
        }

    }

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

    public class Method100Response404
    {
        public void DoSpecialThing()
        {
          Console.WriteLine ("Something Old.."); 
        }

        public string R1_04 { get; set; } = "R1_04";
    }

    public class Method200Response200
    {
        public string R2_02 { get; set; } = "R2_02";
    }
}

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();

having

private static Type[] GetTypesInNamespace(Assembly assembly, string nameSpace)
{
    return assembly.GetTypes()
                   .Where(t => String.Equals(t.Namespace, nameSpace, StringComparison.Ordinal))
                   .ToArray();
}

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)

Expected results

Method100Response201
Method100Response404
Method200Response200

not the Method100Response201_1 because is used by Method100Response201


回答1:


Maybe you want something like this

var classes = AppDomain.CurrentDomain.GetAssemblies()
                       .SelectMany(t => t.GetTypes())
                       .Where(t => 
                                 t.IsClass && 
                                 t.Namespace == "ConsoleApp1.Methods" && 
                                 !t.IsNested)
                       .ToList();

var properties = classes.SelectMany(
   x => x.GetProperties(BindingFlags.Public | BindingFlags.Instance)
         .Select(y => y.PropertyType));

var results = classes.Where(x => !properties.Contains(x))
                     .Select(x => x.Name);

foreach (var result in results)
   Console.WriteLine(result);

Given your exact namespace, the results are

Method100Response201
Method100Response404
Method200Response200

Note : i made some assumptions here, however tweak this code as you need




回答2:


Well, if you want to do that then you’ve got some work to do. Put the types in a hashset, Loop through all the types and get all the properties, look at the return types of the properties, remove those types from the hash set. Nothing magical about it.



来源:https://stackoverflow.com/questions/60132203/c-sharp-using-reflection-list-only-top-classes-excluding-linked-ones

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!