码迷,mamicode.com
首页 > 其他好文 > 详细

AttributeUsage

时间:2014-09-20 17:24:29      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   io   os   使用   ar   

AttributeUsage

  System.AttributeUsage声明一个Attribute的使用范围与使用原则。

  bubuko.com,布布扣

  AllowMultiple 和 Inherited 参数是可选的,所以此代码具有相同的效果:

  bubuko.com,布布扣

  AttributeTarget的值可以参考1。部分可取值如下:

  bubuko.com,布布扣

  如果 AllowMultiple 参数设置为 true,则返回特性可对单个实体应用多次。

  如果 Inherited 设置为 false,则该特性不由从特性化的类派生的类继承。

  Attribute.GetCustomAttribute可以获取一个类的Attribute。

bubuko.com,布布扣
[AttributeUsage(AttributeTargets.Class)]
    public class VersionAttribute : Attribute
    {
        public string Name { get; set; }
        public string Date { get; set; }
        public string Describtion { get; set; }
    }

 [Version(Name = "hyddd", Date = "2009-07-20", Describtion = "hyddd‘s class")]
    public class MyCode
    {
        //...
    }

 class Program
    {
        static void Main(string[] args)
        {
            var info = typeof(MyCode);
            var classAttribute = (VersionAttribute)Attribute.GetCustomAttribute(info, typeof(VersionAttribute));
            Console.WriteLine(classAttribute.Name);
            Console.WriteLine(classAttribute.Date);
            Console.WriteLine(classAttribute.Describtion);
        }
    }
View Code

 

参考:

1、http://msdn.microsoft.com/zh-cn/library/system.attributetargets.aspx

2、http://msdn.microsoft.com/zh-cn/library/tw5zxet9.aspx

3、http://www.cnblogs.com/hyddd/archive/2009/07/20/1526777.html

AttributeUsage

标签:des   style   blog   http   color   io   os   使用   ar   

原文地址:http://www.cnblogs.com/tekkaman/p/3983360.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!