C#特性的簡單介紹

來源:互聯網
上載者:User

標籤:

 特性應該我們大多接觸過,比喻經常使用的[Obsolete],[Serializable]等下面我就主要介紹一個特性的一些用法

摘自MSDN定義:用以將中繼資料或聲明資訊與代碼(程式集、類型、方法、屬性等)相關聯。 

意思就是把我們自訂的特性或者微軟內建的特性和我們的代碼進行組合,其實就是為我們某些代碼附加一些資訊

1:先看.Net帶的三種特性

1.1:[Obsolete]這個預定義特性標記了不應被使用的程式實體

  • 參數 message,是一個字串,描述項目過時原因以及特帶的項目。
  • 參數 error,是一個布爾值。如果該值為 true,編譯器應把該項目的使用當作一個錯誤。預設值是 false(編譯器產生一個警告)。
                 [Obsolete("過時方法")]        private static void OutModed()        {            Console.WriteLine("我是過時的方法");        }

然後引用的時候就出現

如果加上false我們發現在引用的使用就沒法編譯過去大家可以自己實驗下

1.2:[Conditional]這個預定義特性指示編譯器應忽略方法調用或屬性,除非已定義指定的條件編譯符號

  • 參數 conditionString,擷取與 ConditionalAttribute 屬性相關的條件編譯符號。

 

private static void Main(string[] args)        {            Debug();            Trace();                    }        [Conditional("DEBUG")]        private static void Debug() {            Console.WriteLine("我是debug");        }        [Conditional("TRACE")]        public static void Trace()        {            Console.WriteLine("我是TRACE");        }

當調試成trace模式的時候只能結果:

1.3:[AttributeUsage]描述了如何使用一個自訂屬性類別。並加上限制

  • 參數AttributeTargets 指定可以對它們應用特性的應用程式元素
  • 參數allowMultiple 指示該特性是單用還是多用  預設false
  • 參數inherited是否可以繼續 預設true

建立一個自訂特性

[AttributeUsage(AttributeTargets.Method)]    public class CustomAttribute:Attribute    {        public string Name { get; set; }        public CustomAttribute(string name)        {            Name = name;        }    }

上面的限制是只能用於方法

     [Custom()]//報錯    internal class Program    {        private static void Main(string[] args)        {               }

allowMultiple = false。它規定了特性不能被重複放置多次所以下面代碼會報錯

        [Custom("1")] //報錯        [Custom("2")]        public void Method()        {        }
 2:自訂特性

先定義一個屬性類別

    [AttributeUsage(AttributeTargets.All,AllowMultiple = true,Inherited = false)]    public class CustomAttribute:Attribute    {        public string Name { get; set; }        public int Age { get; set; }        public CustomAttribute(string name,int age)        {            Name = name;            Age = age;        }    }

然後定義一個基類

[Custom("張三", 3)]    public class Base    {                public static void Method()        {            Console.WriteLine("我具有一個特性");        }            }
    public static void GetAttributeInfo(Type t) {            var myattribute = (CustomAttribute)Attribute.GetCustomAttribute(t, typeof(CustomAttribute));            if (myattribute!=null)            {                Console.WriteLine("姓名:{0}\n年齡:{1}", myattribute.Name, myattribute.Age);            }            }

調用

GetAttributeInfo(typeof(Base));

 

    public class Base    {            [Custom("張三", 3)]//方法上           public static void Method()        {            Console.WriteLine("我具有一個特性");        }            }

就改變t的寫法:t.GetMethod("Method")這樣來擷取特性運行效果一樣

 

C#特性的簡單介紹

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.