自定义特性
class Program { static void Main(string[] args) { Console.ReadLine(); } } [AttributeUsage(AttributeTargets.All, AllowMultiple = false, Inherited = false)] public class HelpAttribute : Attribute { protected string description; public HelpAttribute(string description_in) { this.description = description_in; } public string Description { get { return this.description; } } public string name { get; set; } } [Help("this is do Nothing Class", name = "zhangsan")] public class AnyClass { }
获取特性
HelpAttribute helpAttribute; foreach (var attr in typeof(AnyClass).GetCustomAttributes(true)) { helpAttribute = attr as HelpAttribute; if (null != helpAttribute) { Console.WriteLine("AnyClass Description:{0}", helpAttribute.Description); } }
时间: 2024-10-13 07:10:50