.NET框架 三种预定义特性的介绍

1、AttributeUsage(AttributeUsageAttribute 类):用来控制自定义特性的使用。 

[AttributeUsage(System.AttributeTargets.All,
                   AllowMultiple = false,
                   Inherited = true)]
 

参数解析:

validon :特性可被应用到的语言元素。来源于枚举器 AttributeTargets 的值的组合,多个值之间使用”|“连接。默认值是 AttributeTargets.All
  
AttributeTargets枚举值说明
All可以对任何应用程序元素应用属性。
Assembly可以对程序集应用属性。
Class可以对类应用属性。
Constructor可以对构造函数应用属性。
Delegate可以对委托应用属性。
Enum可以对枚举应用属性。
Event可以对事件应用属性。
Field可以对字段应用属性。
GenericParameter可以对泛型参数应用属性。
Interface
可以对接口应用属性。
Method可以对方法应用属性。
Module
可以对模块应用属性。
Parameter可以对参数应用属性。
Property可以对属性 (Property) 应用属性 (Attribute)。
ReturnValue可以对返回值应用属性。
Struct可以对结构应用属性,即值类型。

 
AllowMultiple:特性是否可对单个实体应用多次。(可选项)。默认值是 false。
 Inherited:特性是否可被派生类继承。(可选项)。默认值是 false。

例如:
[AttributeUsage(AttributeTargets.Constructor |AttributeTargets.Field |AttributeTargets.Method,AllowMultiple = true,Inherited = true)]


2、Conditional(ConditionalAttribute 类):据预处理标识符执行方法;封闭#if 和 #endif 内部方法的替代方法;可应用于方法、类。

[Conditional(conditionalSymbol)]

例:
#define CONDITION1
#define CONDITION2
using System;
using System.Diagnostics;

class Test
{
    static void Main()
    {               
        Console.WriteLine("Calling Method1");
        Method1(3);
        Console.WriteLine("Calling Method2");
        Method2();

        Console.WriteLine("Using the Debug class");
        Debug.Listeners.Add(new ConsoleTraceListener());
        Debug.WriteLine("DEBUG is defined");
    }

    [Conditional("CONDITION1")]
    public static void Method1(int x)
    {
        Console.WriteLine("CONDITION1 is defined");
    }

    [Conditional("CONDITION1"), Conditional("CONDITION2")]  
    public static void Method2()
    {
        Console.WriteLine("CONDITION1 or CONDITION2 is defined");
    }
}

/*
When compiled as shown, the application (named ConsoleApp) 
produces the following output.

Calling Method1
CONDITION1 is defined
Calling Method2
CONDITION1 or CONDITION2 is defined
Using the Debug class
DEBUG is defined
*/


3、Obsolete(ObsoleteAttribute 类):标记不再使用的程序元素;适用于类、结构、枚举、构造函数、方法、属性、字段、事件、接口、委托。

[Obsolete(Message)]
[Obsolete(Message,IsError)]

参数解析:
Message:获取变通方法消息,包括对可选程序元素的说明;即对使用该元素时的提示信息。
IsError:编译器编译的时候是否将已过时的程序元素视为错误;true将使用已过时的元素视为错误;false将使用已过时的元素视为警告。默认值是 false。
例:

using System;
using System.Reflection;

public class Example
{
   // Mark OldProperty As Obsolete.
   [ObsoleteAttribute("This property is obsolete. Use NewProperty instead.", false)] 
   public string OldProperty
   { get { return "The old property value."; } }

   public string NewProperty
   { get { return "The new property value."; } }

   // Mark OldMethod As Obsolete.
   [ObsoleteAttribute("This method is obsolete. Call NewMethod instead.", true)] 
   public string OldMethod()
   {
      return "You have called OldMethod.";
   }

   public string NewMethod() 
   {   
      return "You have called NewMethod.";
   }   

   public static void Main()
   {                 
      // Get all public members of this type.
      MemberInfo[] members = typeof(Example).GetMembers();
      // Count total obsolete members.
      int n = 0;

      // Try to get the ObsoleteAttribute for each public member.
      Console.WriteLine("Obsolete members in the Example class:\n");
      foreach (var member in members) {
         ObsoleteAttribute[] attribs = (ObsoleteAttribute[]) 
                                        member.GetCustomAttributes(typeof(ObsoleteAttribute), 
                                                                   false);
         if (attribs.Length > 0) {
            ObsoleteAttribute attrib = attribs[0];
            Console.WriteLine("Member Name: {0}.{1}", member.DeclaringType.FullName, member.Name);
            Console.WriteLine("   Message: {0}", attrib.Message);             
            Console.WriteLine("   Warning/Error: {0}", attrib.IsError ? "Error" : "Warning");      
            n++;
         }
      }

      if (n == 0)
         Console.WriteLine("The Example type has no obsolete attributes.");
   } 

}
// The example displays the following output:
//       Obsolete members in the Example class:
//       
//       Member Name: Example.OldMethod
//          Message: This method is obsolete. Call NewMethod instead.
//          Warning/Error: Error
//       Member Name: Example.OldProperty
//          Message: This property is obsolete. Use NewProperty instead.
//          Warning/Error: Warning



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值