C# Attribute 用法备忘

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyFirstAttribute
{
    class Program
    {
        static void Main(string[] args)
        {
            System.Reflection.MemberInfo info=typeof(TestClass);
            MyAttribute myAttribute = Attribute.GetCustomAttribute(info, typeof(MyAttribute)) as MyAttribute;
            Console.WriteLine(myAttribute.Author);
            Console.WriteLine(myAttribute.Time);
            Console.ReadLine();
        }
    }

    #region MyAttribute
    [AttributeUsage(AttributeTargets.Class)]
    public class MyAttribute : Attribute
    {
        private string _author;
        private string _time;

        public MyAttribute(string author, string time)
        {
            _author = author;
            _time = time;
        }

        public string Author
        {
            get { return _author; }
        }
        public string Time
        {
            get { return _time; }
        }
    }
    #endregion


    [My("zzy", "2009-3-3")]
    class TestClass
    {

    }
}
原文地址:https://www.cnblogs.com/zhangchenliang/p/2840216.html