C# Attribute

http://www.csharpwin.com/csharpspace/3917r4248.shtml

 

1、什么是Atrribute
公共语言运行时允许你添加类似关键字的描述声明,叫做Attribute, 它对程序中的元素进行标注,如类型、字段、方法和属性等。Attribute和Microsoft .NET Framework文件的元数据保存在一起,可以用来向运行时描述你的代码,或者在程序运行的时候影响应用程序的行为。

在.NET中,Attribute被用来处理多种问题,比如序列化、程序的安全特征、防止即时编译器对程序代码进行优化从而代码容易调试等等。下面,我们先来看几个在.NET中标准的属性的使用,稍后我们再回过头来讨论Attribute这个类本身。(文中的代码使用C#编写,但同样适用所有基于.NET的所有语言)

2、Attribute作为编译器的指令
在C#中存在着一定数量的编译器指令,如:#define DEBUG, #undefine DEBUG, #if等。这些指令专属于C#,而且在数量上是固定的。而Attribute用作编译器指令则不受数量限制。比如下面的三个Attribute:

   Conditional:起条件编译的作用,只有满足条件,才允许编译器对它的代码进行编译。一般在程序调试的时候使用。
   DllImport:用来标记非.NET的函数,表明该方法在一个外部的DLL中定义。
   Obsolete:这个属性用来标记当前的方法已经被废弃,不再使用了。
下面的代码演示了上述三个属性的使用:

 

 

#define DEBUG //这里定义条件
    
using System;
using System.Runtime.InteropServices;
using System.Diagnostics;
    
namespace AttributeDemo
{
   class MainProgramClass
   {
 
      [DllImport("User32.dll")]
      public static extern int MessageBox(int hParent, string Message, string Caption, int Type);
     
      static void Main(string[] args)
      {
         DisplayRunningMessage();
         DisplayDebugMessage();
     
         MessageBox(0,"Hello","Message",0);
     
         Console.ReadLine();
      }
     
      [Conditional("DEBUG")]
      private static void DisplayRunningMessage()
      {
         Console.WriteLine("开始运行Main子程序。当前时间是"+DateTime.Now);
      }
  
      [Conditional("DEBUG")]
      //[Obsolete("Don't use Old method, use New method", true)] 
      [Obsolete]

      private static void DisplayDebugMessage()
      {
         Console.WriteLine("开始Main子程序");
      }
   }
}

 

如果在一个程序元素前面声明一个Attribute,那么就表示这个Attribute被施加到该元素上,前面的代码,[DllImport]施加到MessageBox函数上, [Conditional]施加到DisplayRuntimeMessage方法和DisplayDebugMessage方法,[Obsolete]施加到DisplayDebugMessage方法上。

根据上面涉及到的三个Attribute的说明,我们可以猜到程序运行的时候产生的输出:DllImport Attribute表明了MessageBox是User32.DLL中的函数,这样我们就可以像内部方法一样调用这个函数。

重要的一点就是Attribute就是一个类,所以DllImport也是一个类,Attribute类是在编译的时候被实例化的,而不是像通常的类那样在运行时候才实例化。Attribute实例化的时候根据该Attribute类的设计可以带参数,也可以不带参数,比如DllImport就带有"User32.dll"的参数;Conditional对满足参数的定义条件的代码进行编译,如果没有定义DEBUG,那么该方法将不被编译,读者可以把#define DEBUG一行注释掉看看输出的结果(release版本,在Debug版本中Conditional的debug总是成立的);Obsolete表明了DispalyDebugMessage方法已经过时了,它有一个更好的方法来代替它,当我们的程序调用一个声明了Obsolete的方法时,那么编译器会给出信息,Obsolete还有其他两个重载的版本。大家可以参考msdn中关于的ObsoleteAttribute 类的描述。

3、Attribute类
除了.NET提供的那些Attribute派生类之外,我们可以自定义我们自己的Attribute,所有自定义的Attribute必须从Attribute类派生。现在我们来看一下Attribute 类的细节:

protected Attribute(): 保护的构造器,只能被Attribute的派生类调用。

三个静态方法:

static Attribute GetCustomAttribute():这个方法有8种重载的版本,它被用来取出施加在类成员上指定类型的Attribute。

static Attribute[] GetCustomAttributes(): 这个方法有16种重载版本,用来取出施加在类成员上指定类型的Attribute数组。

static bool IsDefined():由八种重载版本,看是否指定类型的定制attribute被施加到类的成员上面。

实例方法:

bool IsDefaultAttribute(): 如果Attribute的值是默认的值,那么返回true。

bool Match():表明这个Attribute实例是否等于一个指定的对象。

公共属性: TypeId: 得到一个唯一的标识,这个标识被用来区分同一个Attribute的不同实例。

我们简单地介绍了Attribute类的方法和属性,还有一些是从object继承来的。这里就不列出来了。

下面介绍如何自定义一个Attribute: 自定义一个Attribute并不需要特别的知识,其实就和编写一个类差不多。自定义的Attribute必须直接或者间接地从Attribute这个类派生,如:

public MyCustomAttribute : Attribute { ... }

这里需要指出的是Attribute的命名规范,也就是你的Attribute的类名+"Attribute",当你的Attribute施加到一个程序的元素上的时候,编译器先查找你的Attribute的定义,如果没有找到,那么它就会查找“Attribute名称"+Attribute的定义。如果都没有找到,那么编译器就报错。

4、定义或控制特性的使用

对于一个自定义的Attribute,你可以通过AttributeUsage的Attribute来限定你的Attribute 所施加的元素的类型。代码形式如下:
[AttriubteUsage(参数设置)]

public 自定义Attribute : Attribute { ... }

 非常有意思的是,AttributeUsage本身也是一个Attribute,这是专门施加在Attribute类的Attribute. AttributeUsage自然也是从Attribute派生,它有一个带参数的构造器,这个参数是AttributeTargets的枚举类型。下面是AttributeTargets 的定义:

public enum AttributeTargets
{
   All=16383,
   Assembly=1,
   Module=2,
   Class=4,
   Struct=8,
   Enum=16,
   Constructor=32,
   Method=64,
   Property=128,
   Field=256,
   Event=512,
   Interface=1024,
   Parameter=2048,
   Delegate=4096,
   ReturnValue=8192
}

 

 

作为参数的AttributeTarges的值允许通过“或”操作来进行多个值得组合,如果你没有指定参数,那么默认参数就是All 。 AttributeUsage除了继承Attribute 的方法和属性之外,还定义了以下三个属性:

AllowMultiple: 读取或者设置这个属性,表示是否可以对一个程序元素施加多个Attribute 。

Inherited:读取或者设置这个属性,表示是否施加的Attribute 可以被派生类继承或者重载。

ValidOn: 读取或者设置这个属性,指明Attribute 可以被施加的元素的类型。

下面让我们来做一些实际的东西。我们将会在Help特性前放置AttributeUsage特性以期待在它的帮助下控制Help特性的使用。

 

using System; 
[AttributeUsage(AttributeTargets.Class), AllowMultiple = falseInherited = false ] 
public class HelpAttribute : Attribute 

public HelpAttribute(String Description_in) 

this.description = Description_in; 

protected String description; 
public String Description 

get 

return this.description; 


}

 

先让我们来看一下AttributeTargets.Class。它规定了Help特性只能被放在class的前面。这也就意味着下面的代码将会产生错误:

 

[Help("this is a do-nothing class")] 
public class AnyClass 

[Help("this is a do-nothing method")] //error 
public void AnyMethod() 



}

 

 

编译器报告错误如下:

  AnyClass.cs: Attribute 'Help' is not valid on this declaration type.

  It is valid on 'class' declarations only.

 

  我们可以使用AttributeTargets.All来允许Help特性被放置在任何程序实体前。可能的值是:

  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 )

  下面考虑一下AllowMultiple = false。它规定了特性不能被重复放置多次。

 

 

它产生了一个编译期错误。

  AnyClass.cs: Duplicate 'Help' attribute

  Ok,现在我们来讨论一下最后的这个属性。Inherited, 表明当特性被放置在一个基类上时,它能否被派生类所继承。

 

 

[Help("BaseClass")] 
public class Base 



public class Derive : Base 

}

 

 
  这里会有四种可能的组合:

  [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false ]

  [AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = false ]

  [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true ]

  [AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true ]

  第一种情况:

  如果我们查询(Query)(稍后我们会看到如何在运行期查询一个类的特性)Derive类,我们将会发现Help特性并不存在,因为inherited属性被设置为false。

  第二种情况:

  和第一种情况相同,因为inherited也被设置为false。

  第三种情况:

  为了解释第三种和第四种情况,我们先来给派生类添加点代码:

 

[Help("BaseClass")] 
public class Base 


[Help("DeriveClass")] 
public class Derive : Base 

}

 

  现在我们来查询一下Help特性,我们只能得到派生类的属性,因为inherited被设置为true,但是AllowMultiple却被设置为false。因此基类的Help特性被派生类Help特性覆盖了。

  第四种情况:

  在这里,我们将会发现派生类既有基类的Help特性,也有自己的Help特性,因为AllowMultiple被设置为true。

 

至此,我们介绍了有关Attribute类和它们的代码格式。你一定想知道到底如何在你的应用程序中使用Attribute,如果仅仅是前面介绍的内容,还是不足以说明Attribute有什么实用价值的话,那么从后面的章节开始我们将介绍几个Attribute的不同用法,相信你一定会对Attribute有一个新的了解。

 

       有时候我们需要给一个类或者类中的成员加上一些属性或者附加信息,让类或者变量的功能更明确可控制的细粒度更高,打个简单的比方:数据库里面的一张表,表中的每一个字段都有很多属性,如是否主键,默认值,注释信息等等,我们在编写实体类的时候,如何表示这些信息呢?通过自定义属性可以实现。

自定义属性的实现步骤

1、声明一个类,并将 AttributeUsageAttribute 属性应用到该类中。类的名称即为新属性的名称
2、声明该类从 System.Attribute 继承:
3、定义 Private 字段来存储属性值:
4、需要时,请为属性创建构造函数:
5、为属性 (Attribute) 定义方法、字段和属性 (Property):

 实例一个:

属性类(和相关枚举)

 

/// <summary>
    
/// 数据库字段的用途。
    
/// </summary>
    public enum EnumDBFieldUsage

    {
        /// <summary>
        
/// 未定义。
        
/// </summary>
        None = 0x00,

        /// <summary>
        
/// 用于主键。
        
/// </summary>
        PrimaryKey = 0x01,

        /// <summary>
        
/// 用于唯一键。
        
/// </summary>
        UniqueKey = 0x02,

        /// <summary>
        
/// 由系统控制该字段的值。
        
/// </summary>
        BySystem = 0x04

    }

    [AttributeUsage(AttributeTargets.Property, Inherited = true)]
    public class DBFieldAttribute:Attribute
    {
        EnumDBFieldUsage m_usage;
        string m_strFieldName;
        string m_strDescription;
        object m_defaultValue;

        public DBFieldAttribute(string strFieldName,object defaultValue,EnumDBFieldUsage usage,string strDescription)
        {
            m_strFieldName = strFieldName;
            m_defaultValue = defaultValue;
            m_usage = usage;
            m_strDescription = strDescription;
        }

        public DBFieldAttribute(string fieldName) : this(fieldName,null, EnumDBFieldUsage.None,null)
        { }

        public DBFieldAttribute(string fieldName, EnumDBFieldUsage usage) : this(fieldName, null,usage, null)
        { }

        
        // 获取该成员映射的数据库字段名称。
        public string FieldName

        {
            get
            {
                return m_strFieldName;
            }
            set
            {
                m_strFieldName = value;
            }
        }

        // 获取该字段的默认值
        public object DefaultValue

        {
            get
            {
                return m_defaultValue;
            }
            set 
            {
                m_defaultValue = value;
            }
        }
    }

 

此代码说明了如何制作自定义属性类。其实跟一般的类的区别就是此类继承自Attribute,加上AttributeUsage是属性上的属性,是可选的。

数据访问层实体类:

 

class DalObj
    {
        string m_strTableName;
        int m_nID;
        string m_strName;
        string m_password;

        public DalObj(string strTableName)
        {
            m_strTableName = strTableName;
        }

        [DBField("id",EnumDBFieldUsage.PrimaryKey)]
        public int ID
        {
            get { return m_nID; }
            set { m_nID = value; }
        }

        [DBField("name",DefaultValue="游客")]
        public string Name
        {
            get { return m_strName; }
            set { m_strName = value; }
        }

        [DBField("pwd")]
        public string PassWord
        {
            get { return m_password; }
            set { m_password = value; }
        }
    }

 

此代码说明了如何使用自定义的属性。有两点需要注意的地方

第一:类名可以跟自定义的类名一样,也可以加上或减去后面的Attribute,本例子中就是使用的时候跟自定义的类名减少了“Attribute”。

第二:属性参数填写方法,如果自定义属性类(例子中DBFieldAttribute)自己的构造函数带参数,那么这些参数是必选的,可以重载构造函数以满足不同组合,必选参数填完之后,可以继续给自定义属性类中的公共成员带命名地赋值,如例子中的 DefaultValue="游客" 一句就是命名参数。

遍历自定义属性的代码:

          

 

  DalObj dalObj = new DalObj("users");
            StringBuilder sb = new StringBuilder();
            foreach (PropertyInfo proInfo in dalObj.GetType().GetProperties())
            {
                object[] attrs = proInfo.GetCustomAttributes(typeof(DBFieldAttribute), true);
              if (attrs.Length == 1)
              {
                  DBFieldAttribute attr = (DBFieldAttribute)attrs[0];
                  sb.Append(attr.FieldName + ":" + (attr.DefaultValue == null ? "null" : attr.DefaultValue.ToString()) + "\r\n");
              }
            }
            MessageBox.Show(sb.ToString());

 

此代码说明了如何检索自定义属性的值,主要用到了GetCustomAttributes来获取属性值。

 

原文地址:https://www.cnblogs.com/zuiyirenjian/p/2629698.html