日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

如何在 C# 中使用 Attribute

發布時間:2023/12/4 C# 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 如何在 C# 中使用 Attribute 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

譯文鏈接:https://www.infoworld.com/article/3006630/how-to-work-with-attributes-in-c.html?nsdr=true

Attribute 在 C# 中是一個非常強大的特性,它能夠給你的程序集添加元數據信息。

Attribute 實際上是一個對象,它可以與以下元素中的任何一個相關聯: 程序集、類、方法、委托、枚舉、事件、字段、接口、屬性和結構,它會在這些對象上做信息聲明,當程序運行之后,你可以通過反射來獲取關聯到這些對象上的 Attribute 信息,換句話說:你可以通過 Atrribute 向程序集注入一些額外信息,然后在運行時通過反射來獲取,attribute 一般由 名字 + 一些可選參數 構成, attribute 名字對應著 atrribute 類。

你可以利用 attribute 去校驗你的業務model的正確性, attribute 有兩種:內置 + 自定義, 前者是 .net framework 框架的組成部分,后者需要通過繼承 System.Attribute 類來實現自定義。

現在來看看代碼怎么寫,Obsolete 特性用來標記一個方法是過時的,這個過時的意思是:你不應該再使用這個方法了,未來框架也會將其剔除,目前也存在其替代方案。其實在第三方框架中有很多這樣的例子,下面的代碼片段展示了如何在方法頂部使用 Obsolete 特性。

[Obsolete("This?method?is?obsolete...")] public?static?void?DoSomeWork() { }

如果你在程序中調用了這個方法,當你編譯代碼時,在 Visual Studio 輸出窗口中會現在一些警告信息,如下圖:

當然,如果你一定要忽視它也是可以的,現在,假如你希望你的開發同事不允許調用這個方法,那如何去限定呢?哈哈,可以使用 Obsolete 的第二個參數,這個參數是可選的,下面是 DoSomeWork() 方法的修改版本,請注意這是一個 Boolean 型參數。

[Obsolete("This?method?is?obsolete...",?true)]public?static?void?DoSomeWork(){}

當把 true 給了這個可選參數后,再次編譯代碼,你會發現代碼根本編譯不通過,是不是完美的解決了你的問題,是吧!截圖如下:

自定義 attribute

這一小節我們來看一下如何去實現自定義的 attribute,要想自定義實現,可以創建一個類并繼承 System.Attribute 類即可,如下代碼所示:

using?System; public?class?CustomAttribute?:?Attribute {}

要想限定 CustomAttribute 的使用,可以用 AttributeUsage 類去標記,這個類包含了如下屬性:ValidOn,AllowMultiple,Inherited 等等,這些標記都可以限定 CustomAttribute 的使用。

下面的代碼片段展示了 CustomAttribute 的修改版本,這個類使用構造函數去給內部的私有 string 賦值,代碼僅僅用于演示目的。

[AttributeUsage(AttributeTargets.All)]public?class?CustomAttribute?:?Attribute{private?string?text;public?CustomAttribute(string?text){this.Text?=?text;}public?string?Text?{?get?=>?text;?set?=>?text?=?value;?}}

當然你也可以按需去指定這些 AttributeTargets,如下代碼所示:

[AttributeUsage(AttributeTargets.Class?|AttributeTargets.Constructor?|AttributeTargets.Field?|AttributeTargets.Method?|AttributeTargets.Property,AllowMultiple?=?true)]public?class?CustomAttribute?:?Attribute{private?string?text;public?CustomAttribute(string?text){this.Text?=?text;}public?string?Text?{?get?=>?text;?set?=>?text?=?value;?}}

接下來你可以用反射來獲取應用到對象上的所有attributes,代碼如下:

static?void?Main(string[]?args){MemberInfo?memberInfo?=?typeof(CustomAttribute);object[]?attributes?=?memberInfo.GetCustomAttributes(true);for?(int?i?=?0,?j?=?attributes.Length;?i?<?j;?i++){Console.WriteLine(attributes[i]);}}

接下來我準備將 CustomAttribute 類應用到 下面的 SomeClass 類上。

[CustomAttribute("Hello?World...")] public?class?SomeClass { }

可以著重看下 CustomAttribute 是如何安插在 SomeClass 上的,而且我還傳遞了一個 Hello World... 字符串給它,下面的代碼展示了如何將 CustomAttribute 中的 Text 屬性打印出來。

class?Program{static?void?Main(string[]?args){MemberInfo?memberInfo?=?typeof(SomeClass);object[]?attributes?=?memberInfo.GetCustomAttributes(true);foreach?(object?attribute?in?attributes){CustomAttribute?customAttribute?=?attribute?as?CustomAttribute;if?(customAttribute?!=?null)Console.WriteLine("Text?=?{0}",?customAttribute.Text);elseConsole.WriteLine();}}}[CustomAttribute("Hello?World...")]public?class?SomeClass{}[AttributeUsage(AttributeTargets.Class?|AttributeTargets.Constructor?|AttributeTargets.Field?|AttributeTargets.Method?|AttributeTargets.Property,AllowMultiple?=?true)]public?class?CustomAttribute?:?Attribute{private?string?text;public?CustomAttribute(string?text){this.Text?=?text;}public?string?Text?{?get?=>?text;?set?=>?text?=?value;?}}

總結

以上是生活随笔為你收集整理的如何在 C# 中使用 Attribute的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。