3 回答

TA貢獻1815條經(jīng)驗 獲得超10個贊
元數(shù)據(jù)。有關您的對象/方法/屬性的數(shù)據(jù)。
例如,我可能會聲明一個名為:DisplayOrder的屬性,因此我可以輕松控制屬性應在UI中顯示的順序。然后我可以將它附加到一個類并編寫一些GUI組件來提取屬性并適當?shù)貙I元素進行排序。
public class DisplayWrapper{ private UnderlyingClass underlyingObject; public DisplayWrapper(UnderlyingClass u) { underlyingObject = u; } [DisplayOrder(1)] public int SomeInt { get { return underlyingObject .SomeInt; } } [DisplayOrder(2)] public DateTime SomeDate { get { return underlyingObject .SomeDate; } }}
從而確保在使用我的自定義GUI組件時,SomeDate始終顯示在SomeDate之前。
但是,您會看到它們在直接編碼環(huán)境之外最常用。例如,Windows Designer廣泛使用它們,因此它知道如何處理自定義對象。像這樣使用BrowsableAttribute:
[Browsable(false)]public SomeCustomType DontShowThisInTheDesigner{ get{/*do something*/}}
告訴設計者不要在設計時在“屬性”窗口的可用屬性中列出這個。
您可能還使用它們生成代碼,預編譯操作(如后夏普)或運行時操作如Reflection.Emit的。例如,您可以編寫一些用于分析的代碼,這些代碼透明地包裝您的代碼所做的每一次調用并對其進行計時。您可以通過放置在特定方法上的屬性“選擇退出”時間。
public void SomeProfilingMethod(MethodInfo targetMethod, object target, params object[] args){ bool time = true; foreach (Attribute a in target.GetCustomAttributes()) { if (a.GetType() is NoTimingAttribute) { time = false; break; } } if (time) { StopWatch stopWatch = new StopWatch(); stopWatch.Start(); targetMethod.Invoke(target, args); stopWatch.Stop(); HandleTimingOutput(targetMethod, stopWatch.Duration); } else { targetMethod.Invoke(target, args); }}
聲明它們很簡單,只需創(chuàng)建一個繼承自Attribute的類。
public class DisplayOrderAttribute : Attribute{ private int order; public DisplayOrderAttribute(int order) { this.order = order; } public int Order { get { return order; } }}
請記住,當您使用該屬性時,您可以省略編譯器將為您添加的后綴“屬性”。
注意:屬性本身不做任何事情 - 需要一些其他代碼使用它們。有時代碼是為您編寫的,但有時您必須自己編寫代碼。例如,C#編譯器關心某些框架并使用某些框架(例如,NUnit在類上查找[TestFixture],在加載程序集時在測試方法上查找[Test])。
因此,在創(chuàng)建自己的自定義屬性時,請注意它根本不會影響代碼的行為。您需要編寫檢查屬性的其他部分(通過反射)并對其進行操作。

TA貢獻1779條經(jīng)驗 獲得超6個贊
很多人都回答了,但到目前為止還沒有人提到這個......
屬性大量用于反射。反思已經(jīng)很慢了。
這是非常值得的標記您的自定義屬性為sealed
班提高自己的運行時性能。
考慮使用放置這樣一個屬性的適當位置,并將屬性(!)屬性指示通過也是一個好主意AttributeUsage
。可用屬性用法列表可能會讓您感到驚訝:
部件
模
類
結構
枚舉
構造函數(shù)
方法
屬性
領域
事件
接口
參數(shù)
代表
返回值
的GenericParameter
所有
AttributeUsage屬性是AttributeUsage屬性簽名的一部分也很酷。哇因為循環(huán)依賴!
[AttributeUsageAttribute(AttributeTargets.Class, Inherited = true)]public sealed class AttributeUsageAttribute : Attribute

TA貢獻1796條經(jīng)驗 獲得超7個贊
屬性是一種用于標記類的元數(shù)據(jù)。這通常在WinForms中用于隱藏工具欄中的控件,但可以在您自己的應用程序中實現(xiàn),以使不同類的實例能夠以特定方式運行。
首先創(chuàng)建一個屬性:
[AttributeUsage(AttributeTargets.Class, AllowMultiple=false, Inherited=true)]public class SortOrderAttribute : Attribute{ public int SortOrder { get; set; } public SortOrderAttribute(int sortOrder) { this.SortOrder = sortOrder; }}
所有屬性類必須具有后綴“Attribute”才有效。
完成此操作后,創(chuàng)建一個使用該屬性的類。
[SortOrder(23)]public class MyClass{ public MyClass() { }}
現(xiàn)在,您可以SortOrderAttribute
通過執(zhí)行以下操作來檢查特定類(如果有)
public class MyInvestigatorClass{ public void InvestigateTheAttribute() { // Get the type object for the class that is using // the attribute. Type type = typeof(MyClass); // Get all custom attributes for the type. object[] attributes = type.GetCustomAttributes( typeof(SortOrderAttribute), true); // Now let's make sure that we got at least one attribute. if (attributes != null && attributes.Length > 0) { // Get the first attribute in the list of custom attributes // that is of the type "SortOrderAttribute". This should only // be one since we said "AllowMultiple=false". SortOrderAttribute attribute = attributes[0] as SortOrderAttribute; // Now we can get the sort order for the class "MyClass". int sortOrder = attribute.SortOrder; } }}
如果您想了解更多有關此內容的信息,請隨時查看MSDN,其中包含非常好的描述。
我希望這能幫到你!
- 3 回答
- 0 關注
- 850 瀏覽
添加回答
舉報