1. 程式人生 > >c# attribute

c# attribute

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....
"); } } }