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

 

posted @ 2018-12-25 10:55  我是外婆  阅读(132)  评论(0编辑  收藏  举报