欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C# 特性AttributeUsage簡介與使用教程

 更新時(shí)間:2023年05月27日 14:56:44   作者:wu.g.q  
這篇文章主要介紹了C# 特性AttributeUsage簡介與使用教程,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

AttributeUsage

預(yù)定義特性AttributeUsage描述了如何使用一個(gè)自定義特性類。它規(guī)定了特性可應(yīng)用到的項(xiàng)目的類型。

規(guī)定該特性的語法如下:

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

validon:自定義特性的對象,可以是類、方法、屬性等對象(默認(rèn)值是 AttributeTargets.All)
AllowMultiple:是否允許被多次使用(默認(rèn)值為false:單用的)
Inherited:是否可被派生類繼承(默認(rèn)值為false:不能)

下面請看使用:

using System;
namespace AttributeUsagePractice
{
    [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
    public class HelpAttribute : Attribute
    {
        public HelpAttribute(String Description_in)
        {
            this.description = Description_in;
        }
        protected String description;
        public String Description
        {
            get { return this.description; }
        }
    }
    class Program
    {
        [Help("this is a main class")]  //error
        public static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            // TODO: Implement Functionality Here
            Console.Write("Press any key to continue . . . ");
            Console.ReadKey(true);
        }
    }
}
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]

含義為:定制特性類,不允許多次使用,不能被繼承

第一個(gè)參數(shù):
因?yàn)樗奶匦阅繕?biāo)是 AttributeTargets.Class,而它放在函數(shù)前面,所以上面的程序會(huì)報(bào)錯(cuò):特性“Help”對此聲明類型無效。它只對“class”聲明有效,正確的做法是放在 class Program 上面。

如果是下面的代碼:

using System;
namespace AttributeUsagePractice
{
    [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
    public class HelpAttribute : Attribute
    {
        public HelpAttribute(String Description_in)
        {
            this.description = Description_in;
        }
        protected String description;
        public String Description
        {
            get { return this.description; }
        }
    }
    [Help("this is a main class")]
    [Help("this is a main2 class")]  //error
    class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            // TODO: Implement Functionality Here
            Console.Write("Press any key to continue . . . ");
            Console.ReadKey(true);
        }
    }
}

第二個(gè)參數(shù):

因?yàn)锳llowMultiple = false,上面多次使用,所以報(bào)錯(cuò) 重復(fù)的“Help”特性,正確的做法就是去掉它

using System;
using System.Linq;
namespace AttributeUsagePractice
{
    [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
    public class HelpAttribute : Attribute
    {
        public HelpAttribute(){}
        public HelpAttribute(String Description_in)
        {
            this.description = Description_in;
        }
        protected String description;
        public String Description
        {
            get { return this.description; }
        }
    }
    [Help("this is a HelpAttribute use class")]
    public class UseHelpAttribute
    {
    }
    public class UseHelpAttributeDerive : UseHelpAttribute
    {
    }
    class Program
    {
        public static void Main(string[] args)
        {
            // TODO: Implement Functionality Here
            UseHelpAttributeDerive objHelpAttribute = new UseHelpAttributeDerive();
            Type t = objHelpAttribute.GetType();
            object [] objAttrs = t.GetCustomAttributes(typeof(HelpAttribute),true);
            if(objAttrs!= null && objAttrs.Length > 0)
            {
                object temp = objAttrs.First();
                HelpAttribute myAttr = temp as HelpAttribute;
                Console.WriteLine("類描述:{0}", myAttr.Description);
            }
            else
            {
                Console.WriteLine("沒有類描述");
            }
            Console.ReadKey(true);
        }
    }
}

第三個(gè)參數(shù):

因?yàn)镮nherited = false,所以運(yùn)行結(jié)果為;

如果把Inherited = false 改為 Inherited = true,效果如下:

到此這篇關(guān)于C# 特性AttributeUsage的理解與使用的文章就介紹到這了,更多相關(guān)C# AttributeUsage使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • C#簡易人機(jī)對抗“石頭剪刀布”游戲的實(shí)現(xiàn)

    C#簡易人機(jī)對抗“石頭剪刀布”游戲的實(shí)現(xiàn)

    本文主要介紹了C#簡易人機(jī)對抗“石頭剪刀布”游戲的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-05-05
  • C# 實(shí)現(xiàn)枚舉轉(zhuǎn)列表

    C# 實(shí)現(xiàn)枚舉轉(zhuǎn)列表

    這篇文章主要介紹了C# 如何實(shí)現(xiàn)枚舉轉(zhuǎn)列表,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下
    2020-12-12
  • C#繼承IList?接口的實(shí)現(xiàn)步驟

    C#繼承IList?接口的實(shí)現(xiàn)步驟

    C#中的IList<T>接口是.NET框架中的一種通用接口,它定義了一組在運(yùn)行時(shí)可以使用類型參數(shù)T的元素的集合,本文給大家介紹了C#繼承IList?接口的設(shè)計(jì)方法,文中通過代碼示例給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2024-02-02
  • C#使用smtp發(fā)送帶附件的郵件實(shí)現(xiàn)方法

    C#使用smtp發(fā)送帶附件的郵件實(shí)現(xiàn)方法

    這篇文章主要介紹了C#使用smtp發(fā)送帶附件的郵件實(shí)現(xiàn)方法,可直接將string類型結(jié)果保存為附件,實(shí)例中備有相應(yīng)的注釋便于理解,需要的朋友可以參考下
    2014-11-11
  • 微信開發(fā)--企業(yè)轉(zhuǎn)賬到用戶

    微信開發(fā)--企業(yè)轉(zhuǎn)賬到用戶

    本文主要介紹了微信開發(fā)--企業(yè)轉(zhuǎn)賬到用戶的實(shí)現(xiàn)方法與步驟。具有很好的參考價(jià)值,下面跟著小編一起來看下吧
    2017-01-01
  • C#設(shè)計(jì)模式之外觀模式介紹

    C#設(shè)計(jì)模式之外觀模式介紹

    外觀模式:為子系統(tǒng)中的一組接口提供一個(gè)一致的界面,此模式定義了一個(gè)高層的接口,這個(gè)借口使得這子系統(tǒng)容易使用
    2012-10-10
  • Unity實(shí)現(xiàn)打磚塊游戲

    Unity實(shí)現(xiàn)打磚塊游戲

    這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)打磚塊游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • C#使用哈希表實(shí)現(xiàn)XML文件查詢

    C#使用哈希表實(shí)現(xiàn)XML文件查詢

    這篇文章主要為大家詳細(xì)介紹了C#如何使用哈希表實(shí)現(xiàn)XML文件查詢功能,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,有需要的小伙伴可以參考下
    2024-02-02
  • C#開發(fā)Winform實(shí)現(xiàn)學(xué)生管理系統(tǒng)

    C#開發(fā)Winform實(shí)現(xiàn)學(xué)生管理系統(tǒng)

    這篇文章介紹了C#開發(fā)Winform實(shí)現(xiàn)學(xué)生管理系統(tǒng)的項(xiàng)目案例,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-05-05
  • C#中Lambda表達(dá)式的用法

    C#中Lambda表達(dá)式的用法

    這篇文章介紹了C#中Lambda表達(dá)式的用法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-04-04

最新評論