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

C#中屬性(Attribute)的用法

 更新時間:2022年05月11日 15:11:20   作者:springsnow  
這篇文章介紹了C#中屬性(Attribute)的用法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

一、創(chuàng)建屬性

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Constructor, AllowMultiple = true, Inherited = true)]
//AttributeTargets:屬性應(yīng)用到的目標(biāo)類型。AllowMultiple:是否允許一個元素應(yīng)用多個此屬性。Inherited:屬性能否有派生類繼承。
public class CodeStatusAttribute : Attribute
{
    private string status;
    public CodeStatusAttribute(string status)//構(gòu)造函數(shù)為位置參數(shù)
    {
        this.status = status;
    }
    public string Tester { set; get; }//屬性和公共字段為命名參數(shù)
    public string Coder { set; get; }
    
    public override string ToString()
    {
        return status;
    }
}

二、應(yīng)用屬性

//1、使用單個屬性
[CodeStatus("a版")]
public class Tringe
{ }

//2、使用多個屬性
[CodeStatus("b版", Coder = "小李")]
[CodeStatus("b版", Coder = "小王")]
//也可以[CodeStatus("aa",Coder="小李"),CodeStatus("aa",Coder="小王")]
public class Square
{ }

//3、使用位置參數(shù)和命名參數(shù)
//type表示此屬性與什么元素關(guān)聯(lián),可能有:assembly,field,method,param,property,return,moudule,event,type等。。
[type: CodeStatus("最終版", Coder = "小李", Tester = "老李")]
public class Circle
{
    [CodeStatus("最終版", Coder = "小李", Tester = "老李")]
    public Circle()
    {

    }
}

三、反射屬性

//1、獲取類上的屬性。
Type t = typeof(Circle);
Attribute[] attArr = Attribute.GetCustomAttributes(t, typeof(CodeStatusAttribute));
//或
object[] attArr1 = t.GetCustomAttributes(typeof(CodeStatusAttribute), true);

//2、獲取成員上屬性
Attribute[] attArr3 = t.GetConstructors()[0].GetCustomAttributes().ToArray();//構(gòu)造函數(shù),獲取字段GetField("..")

//3、遍歷
foreach (Attribute attr in attArr3)
{
    CodeStatusAttribute item = (CodeStatusAttribute)attr;
    Console.Write(item.ToString() + item.Coder + item.Tester);
}

四、Net內(nèi)置屬性

[Condeitonal] //條件控制
[Obsolete] //廢棄屬性
[Serializable]//可序列化屬性
[AssemblyDelaySign] //程序集延遲簽名

到此這篇關(guān)于C#屬性(Attribute)的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

河间市| 海城市| 揭东县| 个旧市| 永顺县| 土默特右旗| 荃湾区| 平和县| 会理县| 连南| 塔河县| 庄浪县| 海林市| 汝阳县| 吉安县| 绥阳县| 衡水市| 海兴县| 克东县| 虹口区| 泰兴市| 赣榆县| 富锦市| 浮山县| 马边| 马关县| 清新县| 涟源市| 古蔺县| 内江市| 潮安县| 宝山区| 轮台县| 新化县| 伊春市| 类乌齐县| 蒙自县| 霍邱县| 夏河县| 昌乐县| 大名县|