//如果没有定义 wjire 或者 refuge, //编译器不会再元数据中生成特性信息,也就是说利用反射找不到特性信息 //但是,特性类的定义元数据和实现仍在程序集中 //#define wjire #define refuge using System; using System.Diagnostics; using System.Reflection; namespace Test3 { [Cond] class Program { static void Main(string[] args) { //条件特性类 var flag = CustomAttributeExtensions.IsDefined(typeof(Program), typeof(CondAttribute)); Console.WriteLine(flag); var cond = CustomAttributeExtensions.GetCustomAttribute(typeof(Program), typeof(CondAttribute)); Console.WriteLine(cond?.GetType()); Console.ReadKey(); } } [Conditional("wjire")] [Conditional("refuge")] class CondAttribute : Attribute { public CondAttribute() { Console.WriteLine("this is ctor"); } } }
原文地址:https://www.cnblogs.com/refuge/p/8971711.html
时间: 2024-10-06 00:23:33