C# 自定义特性

using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            HelpAttribute helpAttribute;
            foreach (var item in typeof(AnyClass).GetCustomAttributes(true))
            {
                helpAttribute = item as HelpAttribute;
                if (helpAttribute.Name != null)
                {
                    Console.WriteLine(helpAttribute.Name);
                }
            }
        }
    }
    [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
    public class HelpAttribute : Attribute
    {
        private string name;
        public string Name
        {
            get
            {
                return this.name;
            }
        }
        public HelpAttribute(string _name)
        {
            this.name = _name;
        }
    }
    [Help("name")]
    public class AnyClass
    {

    }
}

 

posted @ 2019-08-06 15:40  liliyou  阅读(86)  评论(0编辑  收藏  举报