How to distingush compiler-generated classes from user classes in .NET

前端 未结 2 1267
一个人的身影
一个人的身影 2020-12-20 17:24

I have a piece of code in my program that distinguishes compiler-generated classes by checking whether they contain \"DisplayClass\" in its type name.
upon reading this

2条回答
  •  醉话见心
    2020-12-20 18:09

    This answer really helped me out! Here's the code I needed to add to check a Type for the CompilerGeneratedAttribute as Valentin Kuzub mentioned:

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

提交回复
热议问题