最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

AttributeUsage

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

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

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

validon:自定義特性的對象,可以是類、方法、屬性等對象(默認值是 AttributeTargets.All)
AllowMultiple:是否允許被多次使用(默認值為false:單用的)
Inherited:是否可被派生類繼承(默認值為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)]

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

第一個參數(shù):
因為它的特性目標是 AttributeTargets.Class,而它放在函數(shù)前面,所以上面的程序會報錯:特性“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);
        }
    }
}

第二個參數(shù):

因為AllowMultiple = false,上面多次使用,所以報錯 重復的“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);
        }
    }
}

第三個參數(shù):

因為Inherited = false,所以運行結果為;

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

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

相關文章

最新評論

谷城县| 饶平县| 临洮县| 临沭县| 石阡县| 囊谦县| 嘉黎县| 安塞县| 伊春市| 舒城县| 肇源县| 汉源县| 罗甸县| 博白县| 武邑县| 高雄市| 招远市| 平利县| 偃师市| 常德市| 金昌市| 定陶县| 道真| 东至县| 益阳市| 吴旗县| 鄂伦春自治旗| 康定县| 孟州市| 武乡县| 略阳县| 新安县| 旺苍县| 双辽市| 义马市| 乐业县| 阿克苏市| 凤台县| 顺昌县| 揭西县| 丹寨县|