特性(attribute)

 自定义特性

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);
                }
            }
原文地址:https://www.cnblogs.com/handsomer/p/4553729.html