Like distingush classes created by the compiler from custom classes in .NET.

I have a piece of code in my program that distinguishes classes created using the compiler by checking if they contain a “DisplayClass” in their type name.
after reading this answer , I think I need a better way. How are distingush-compiler-generated classes from custom classes in .NET?

+6
source share
2 answers

Check classes for the CompilerGenerated attribute to distinguish compiler-generated classes from others

http://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.compilergeneratedattribute.aspx

In the reflector, these display classes can be seen as follows:

 [CompilerGenerated] private sealed class <>c__DisplayClass1 {..} 
+13
source

This answer really helped me! Here is the code I need to add in order to check the Type for CompilerGeneratedAttribute , as Valentin Kuzub said:

 using System.Runtime.CompilerServices; //... bool IsCompilerGenerated(Type t) { var attr = Attribute.GetCustomAttribute(t, typeof(CompilerGeneratedAttribute)); return attr != null; } 
+7
source

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


All Articles