簡體   English   中英

C#,silverlight,檢查由ref傳遞的字段是否應用了屬性

[英]c#, silverlight, check if field passed by ref has an attribute applied

在基類中,我具有以下用於派生類的方法:

protected virtual void SetValue<T>(ref T field, string propertyName, T value)
{
   //assign value to the field and do some other staff 
   ...
}

有什么方法可以檢查fieldVar是否應用了屬性(例如DataMemberAttribute)?

不,沒有其他方法可以執行此操作,除了看起來還告訴您屬性名稱。

如果可以找到該字段的FieldInfo,則可以找到任何屬性,但不能僅通過ref-parameter。

在兩行之間閱讀時,您擁有一組私有字段,這些私有字段支持公共屬性的值。 在某些或所有這些屬性上,您要附加一些數據屬性。

 PropertyInfo pi = this.GetType().GetProperty(propertyName);
 object[] dataMemberAttributes = pi.GetCustomAttributes(typeof(DataMemberAttribute, true);
 if (dataMemberAttributes.Length > 0)
 {
     DataMemberAttribute dataMemberAttribute = (DataMemberAttribute)dataMemberAttributes[0];
     // Do stuff with the attribute.
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM