c# attribute
阿新 • • 發佈:2018-12-25
using System; using System.Reflection; namespace console_attribute { class Program { public static void Main(string[] args) { Type t = typeof(Books); foreach (var attr in t.GetCustomAttributes(true)) {if (attr is Author) { Author obj = (Author)attr; Console.WriteLine(obj.version); } } Console.ReadKey(true); } } class Author: Attribute{private string _name; public string version; public Author(string name){ this._name = name; } } [Author("leo", version="1.1")] class Books{ public Books(){ Console.WriteLine("attribute test...."); } } }