天天看點

unity基礎學習十一,C#進階屬性:特性1.C# 特性(Attribute)引用

1.C# 特性(Attribute)

特性,是用來給代碼添加額外資訊的一種手段,我們通常是将特性标記到方法,類或者屬性上,在使用的這些結構的時候,通過反射(reflection)這一非常進階的技術,擷取它們通過特性标記的資訊(在編譯好的程式集中,作為中繼資料),進而進行某些特殊的處理。

.Net 架構提供了兩種類型的特性:預定義特性和自定義特性。

1.1 規定特性(Attribute)

規定特性(Attribute)的文法如下:

[attribute(positional_parameters, name_parameter = value, ...)]
element
           

使用:

特性的使用很簡單,在結構聲明的上一行,用"[]"擴起特性類名即可:

系統也給我們提供了一些特性,比如Serializable 标記一個可序列化的類,DebuggerStepThrough設定方法在調試時為跳過的狀态。

[Serializable]
 class Sunshine{...}
           

它其實是省略了(),如果使用特性的其它的構造方法,顯示聲明即可:

[Serializable("test")]
 class Sunshine{...}
           

1.2 預定義特性(Attribute)

.Net 架構提供了三種預定義特性:

  • AttributeUsage
  • Conditional
  • Obsolete

1.2.1 AttributeUsage(自定義特性)

預定義特性 AttributeUsage 描述了如何使用一個自定義特性類。它規定了特性可應用到的項目的類型。

規定該特性的文法如下:

[AttributeUsage(
   validon,
   AllowMultiple=allowmultiple,
   Inherited=inherited
)]
           

其中:

  • 參數 validon 規定特性可被放置的語言元素。它是枚舉器 AttributeTargets 的值的組合。預設值是 AttributeTargets.All。
  • 參數 allowmultiple(可選的)為該特性的 AllowMultiple 屬性(property)提供一個布爾值。如果為 true,則該特性是多用的。預設值是 false(單用的)。
  • 參數 inherited(可選的)為該特性的 Inherited 屬性(property)提供一個布爾值。如果為 true,則該特性可被派生類繼承。預設值是 false(不被繼承)。

例如:

[AttributeUsage(AttributeTargets.Class |
AttributeTargets.Constructor |
AttributeTargets.Field |
AttributeTargets.Method |
AttributeTargets.Property, 
AllowMultiple = true)]
           

 使用:

  1. 建構一個自定義特性[AttributeUsage()]
  2. 編寫一個自定義特性類,類名必須以Attribute結尾并繼承自Attribute類
  3. 将定位的參數通過構造函數傳遞(每個自定義特性類必須至少有一個構造函數)
  4. 通過反射檢索到特性資訊
using System;
 
class MainClass
{
    public static void Main()
    {
        Maoxiandao test = new Maoxiandao();
        Type t = test.GetType();
        object[] o = t.GetCustomAttributes(true);
        TestAttribute attribute = (TestAttribute)o[0];
        Console.WriteLine(attribute.msg);
    }
}
 
[Test("這是測試類")] //這裡的Test對應下方的TestAttribute
public class Maoxiandao { }
 
[AttributeUsage(AttributeTargets.All)]
public class TestAttribute : Attribute
{
    public string msg;
 
    public TestAttribute(string msg)
    {
        this.msg = msg;
    }
}
           

1.2.2 Conditional

可以為一個方法添加Conditional特性使這個方法的執行依賴于指定的預處理辨別符

規定該特性的文法如下:

[Conditional(
   conditionalSymbol
)]
           

使用案例:

#define Debug //有這個就列印兩個,沒這個就列印一個
 
using System;
using System.Diagnostics;
 
class MainClass
{
    public static void Main()
    {
        Fun1();
        Fun2();
    }
 
    [Conditional("Debug")]
    public static void Fun1()
    {
        Console.WriteLine("Fun1");
    }
 
    public static void Fun2()
    {
        Console.WriteLine("Fun2");
    }
}
           

1.2.3 Obsolete

可以為類、屬性、字段、方法添加Obsolete特性使這個目标元素标記為過時的

Obsolete特性的第二個參數是否為error預設為false,不影響編譯通過隻會提示warning,如果為true則編譯不會通過

規定該特性的文法如下:

[Obsolete(
   message
)]
[Obsolete(
   message,
   iserror
)]
           
  • 參數 message,是一個字元串,描述項目為什麼過時以及該替代使用什麼。
  • 參數 iserror,是一個布爾值。如果該值為 true,編譯器應把該項目的使用當作一個錯誤。預設值是 false(編譯器生成一個警告)。
using System;
public class MyClass
{
   [Obsolete("Don't use OldMethod, use NewMethod instead", true)]
   static void OldMethod()
   {
      Console.WriteLine("It is the old method");
   }
   static void NewMethod()
   {
      Console.WriteLine("It is the new method");
   }
   public static void Main()
   {
      OldMethod();
   }
}
           

 當您嘗試編譯該程式時,編譯器會給出一個錯誤消息說明:

Don't use OldMethod, use NewMethod instead      

引用

C#特性(Attribute) - 知乎

C# 特性(Attribute) | 菜鳥教程

C#中的特性_Hello Bug.的部落格-CSDN部落格_c# 特性