Attributes in C# 笔记

/*
 *
 * 用户: Changweihua
 * 日期: 2012/11/10
 * 时间:  20:38
 * 邮箱: changweihua@outlook.com
 * 博客: http://www.cmono.net
 *  
 * 描述:
 *  
 *
 *
 * 修改:
 *
 *
 *
 * 不足:
 * 
 *
 *
 *
 *
 * 不足:
 *
 */
using System;
using System.Reflection;
using System.Diagnostics;

 /*
  * attribute identifier 用来标注attribute作用在什么上面,比如是方法上,类型上,返回值上还是全部的程序集上
  * 
  * 所有可能的值包括
  * 
  * assembly
  * module
  * type
  * method
  * property
  * event
  * field
  * param
  * return
  * 
  */
 
//[assembly: AttributeClassName("Description")]指明Attribute作用在整个程序集上
[assembly : Console5.HelpAttribute("This is Assembly demonstrates custom attributes creation and their run-timr query")]


namespace Console5
{
   
    public class Program
    {
        public static void Main(string[] args)
        {
            HelpAttribute HelpAttr;
            
            String assemblyName;
            
            Process p = Process.GetCurrentProcess();
            
            assemblyName = p.ProcessName + ".exe";
            
            Assembly a = Assembly.LoadFrom(assemblyName);
            
            foreach (Attribute attr in a.GetCustomAttributes(true)) 
            {
                HelpAttr = attr as HelpAttribute;
                
                if(HelpAttr != null)
                {
                    Console.WriteLine("Description of {0} : \n{1}", assemblyName, HelpAttr.Description);
                }
            }
            
            Type type = typeof(AnyClass);
            
            //Querying Class Attributes
            foreach (Attribute attr in type.GetCustomAttributes(true)) 
            {
                HelpAttr = attr as HelpAttribute;
                
                if(attr != null)
                {
                    Console.WriteLine("Description of AnyClass : \n{0}", HelpAttr.Description);
                }
            }
            
            //Querying Class-Method Attributes
            foreach (MethodInfo method in type.GetMethods()) 
            {
                foreach (Attribute attr in method.GetCustomAttributes(true)) 
                {
                    HelpAttr = attr as HelpAttribute;
                
                    if(attr != null)
                    {
                        Console.WriteLine("Description of {0} : \n{1}", method.Name, HelpAttr.Description);
                    }
                }
            }
            
             //Querying Class-Field Attributes
            foreach (FieldInfo field in type.GetFields()) 
            {
                foreach (Attribute attr in field.GetCustomAttributes(true)) 
                {
                    HelpAttr = attr as HelpAttribute;
                
                    if(attr != null)
                    {
                        Console.WriteLine("Description of {0} : \n{1}", field.Name, HelpAttr.Description);
                    }
                }
            }
            
            Console.ReadKey(true);
        }
    }
    
    /// <summary>
    /// 使用预定义的Attribute
    /// </summary>
    public class AAnyClass
    {
        //告诉编译器Old是过时的,编译时会将它作为一个错误处理
        [Obsolete("Don't use old method, use new method", true)]
        public static void Old(){}
        
        public static void New(){}
        
    }
    
    
    /*
     * AttributeUsage 是一个特别的类,它修饰自定义的Attribute类,主要是帮助我们定义如何使用attribute,它有三个属性
     * 
     * ValidOn 可以指明attribute可以放在什么地方
     * AllowMultiple 指明一个attribute是否可以多次放在同一个实体里面
     * Inherited 指明这个attribute是否会在所修饰的类的子类里出现
     * 
     * AttributeTargets 包括
     * Assembly
     * Module
     * Class
     * Struct
     * Enum
     * Constructor
     * Method
     * Property
     * Field
     * Event
     * Interface
     * Parameter
     * Delegate
     * All = Assembly | Module | Class | Struct | Enum | Constructor | Method | Property | Field | Event | Interface | Parameter | Delegate
     * ClassMembers = Class | Struct | Enum | Constructor | Method | Property | Field | Event | Delegate | Interface
     * 
     * Positional Parameter 和 Named Parameter
     * Positional 参数是构造器参数,而且是强制要有的,相反,Named参数是可选的
     * 对于Named参数,如果要使用它,必须要定义一个set方法
     * 
     * Description 就是Positional参数,而Version就是Named参数
     * 
     * 
     */
    
    
    /// <summary>
    /// custom attribute class
    /// </summary>
    /// 
    //[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
    public class HelpAttribute : Attribute
    {
        protected string description;
        
        public string Description 
        {
            get { return description; }
        }
        
        public HelpAttribute(string Description_in)
        {
            this.description = Description_in;
        }
    }
    
    
    [Help("this is a do nothing class")]
    public class AnyClass
    {
        [Help("This is a do-nothing Method")]
        public void AnyMethod()
        {}
        
        [Help("This is any Integer")]
        public int AnyInt;
        
    }
    
}

知识共享许可协议
《Attributes in C# 笔记》常伟华 创作。
采用 知识共享 署名-相同方式共享 3.0 中国大陆 许可协议进行许可。
  • 多说评论
  • 签名
  • 新浪微博
  • 默认评论
  • Tab Header 5

0 条评论 / 点击此处发表评论

Tab Content 5

Warning: file_get_contents(http://ip.taobao.com/service/getIpInfo.php?ip=3.237.94.109) [function.file-get-contents]: failed to open stream: HTTP request failed! in D:\wwwroot\changweihua\wwwroot\v\content\templates\darklight\vrecord.php on line 129

开发技术


开发平台和工具

sitemap     19332.97ms