Getting all types under custom build

I am trying to get all types defined in a specific user namespace

Assembly.GetEntryAssembly().GetTypes().Where(t => t.Namespace == "namespace")


    <>c__DisplayClass3_0    
    <>c__DisplayClass4_0    
    <>c__DisplayClass6_0    
    <>c__DisplayClass2_0    
    <>c__DisplayClass2_1    
    <>c__DisplayClass2_2    
    <>c__DisplayClass2_3    
    <>c__DisplayClass2_4    
    <>c__DisplayClass2_5    
    <>c__DisplayClass2_6    
    <>c__DisplayClass2_7    
    <>c__DisplayClass2_8

My question is Why am I getting this extra type that is not defined in this namespace?

How to choose a type that is a user-defined type?

will someone explain to me what it is and how they are defined in the user namespace.

+6
source share
2 answers

These are all types generated by the compiler. The C # compiler generates types to implement such things as:

  • Lambda expressions and anonymous methods
  • Iterator Blocks
  • Asynchronous methods
  • Anonymous types

CompilerGeneratedAttribute, , :

var types = Assembly.GetEntryAssembly()
    .GetTypes()
    .Where(t => t.Namespace == "namespace")
    .Where(t => !t.GetTypeInfo().IsDefined(typeof(CompilerGeneratedAttribute), true));
+8

.

, : # DisplayClass LINQ Any() ?

CompilerGeneratedAttribute, , :

Assembly.GetEntryAssembly().GetTypes()
   .Where(t => t.Namespace == "namespace")
   .Where(x => !x.GetTypeInfo().GetCustomAttributes<CompilerGeneratedAttribute>().Any());
+4

Source: https://habr.com/ru/post/1016105/


All Articles