理解.NET中的Attribute

using System;

namespace TestMethod
{
    class Program
    {
        static void Main(string[] args)
        {
            ObsoleteClass.test();
            Console.ReadKey();
        }
    }

    [Obsolete("Don't use OldMethod, use NewMethod instead", false)]
    public class ObsoleteClass
    {
        public static void test()
        {
            Console.WriteLine("ObsoleteClass.test");
        }
    }
}

using System;

namespace BoerCloud.Core.Util
{
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = false)]
    public class RecordAttribute : Attribute
    {
        private string _recordType;
        private string _author;
        private DateTime _date;
        private string _apiType;
        private string _desc;

        public RecordAttribute(string recordType,string author,string date,string apiType,string desc)
        {
            this._recordType = recordType;
            this._author = author;
            this._date = Convert.ToDateTime(date);
            this._apiType = apiType;
            this._desc = desc;
        }

        public string RecordType { get { return _recordType; } }
        public string Author { get { return _author; } }
        public DateTime Date { get { return _date; } }
        public string ApiType { get { return _apiType; } }
        public string Desc {
            get { return _desc; }
            set { _desc = value; }
        }

    }
}

[Record("create", "ChenQP", "2016-08-09", "OpenApi", "RegisterSite")]

扩展阅读

  • C# 特性(Attribute)
  • C# 特性/属性(Attribute) 以及使用反射查看自定义特性

你可能感兴趣的:(理解.NET中的Attribute)