.NET基礎拾遺(6)特性

來源:互聯網
上載者:User

標籤:

1 神馬是特性?如何自訂一個特性?

(1)特性是什麼

      CLR允許添加類似關鍵字的描述聲明叫做attributes, 它對程式中的目標元素進行標註,如類型、欄位、方法和屬性等。

      Attributes和Microsoft .NET Framework檔案的中繼資料儲存在一起,可以用來向運行時描述你的代碼或者在程式啟動並執行時候影響應用程式的行為。

     總結為:定製特性attribute,本質上是一個類,為目標元素提供關聯附加資訊並在運行期以反射的方式來擷取附加資訊。

           定製特性可應用的目標元素:程式集(assembly)、模組(module)、類型(type)、屬性(property)、事件(event)、欄位(field)、方法(method)、參數(param)、傳回值(return),其實關於數組來接受任意長度的參數就是用特性來做出來的

幾個習慣用法:
    自訂特性必須直接或間接繼承自System.Attribute類而且該類必須要有公有的建構函式;
    自訂特性應該有一個Attribute尾碼,習慣約定(在調用中可以省略,會自動匹配);
    非抽象特性必須要有public存取權限;
    定製特性不會影響應用元素的任何功能,只是約定了該元素具有的特質;  

 

(2)如何自訂特性

定義一個特性的本質就是定義一個繼承自System.Attribute類的類型.

/// <summary>    /// 一個自訂特性MyCustomAttribute    /// </summary>    [AttributeUsage(AttributeTargets.Class)]    public class MyCustomAttribute : Attribute    {        private string className;        public MyCustomAttribute(string className)        {            this.className = className;        }        // 一個唯讀屬性ClassName        public string ClassName        {            get            {                return className;            }        }    }    [MyCustom("UseMyCustomAttribute")]    class UseMyCustomAttribute    {        static void Main(string[] args)        {            Type t = typeof(UseMyCustomAttribute);            // 通過GetCustomAttributes方法得到自訂特性            object[] attrs = t.GetCustomAttributes(false);            MyCustomAttribute att = attrs[0] as MyCustomAttribute;            Console.WriteLine(att.ClassName);            Console.ReadKey();        }    }

  實際項目的例子

namespace SC.Component.Utility{    using System;        [AttributeUsage(AttributeTargets.Method, AllowMultiple=false, Inherited=false)]    public sealed class APIMethodAttribute : Attribute    {        private string m_APIUrl;                public APIMethodAttribute()        {        }                public APIMethodAttribute(string apiUrl)        {            this.m_APIUrl = apiUrl;        }                public string APIUrl        {            get            {                return this.m_APIUrl;            }            set            {                this.m_APIUrl = value;            }        }    }}
        //一個繼承自System.Attribute的類型,就是一個自訂特性,並且可以將其添加到適合的元素之上
[APIMethod("TravelOrderList")] public static string GetTravelList(JourneyCalendarParmas reqParams) { string result = string.Empty; try { ........ } catch (Exception ex) { return null; } return result; }

  

.NET基礎拾遺(6)特性

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.