3 回答

TA貢獻(xiàn)1859條經(jīng)驗(yàn) 獲得超6個(gè)贊
否。每當(dāng)實(shí)現(xiàn)接口或重寫派生類中的成員時(shí),都需要重新聲明屬性。
如果您只關(guān)心ComponentModel(而不是直接反射),則有一種方法([AttributeProvider])從現(xiàn)有類型中建議屬性(以避免重復(fù)),但這僅對(duì)屬性和索引器用法有效。
舉個(gè)例子:
using System;
using System.ComponentModel;
class Foo {
[AttributeProvider(typeof(IListSource))]
public object Bar { get; set; }
static void Main() {
var bar = TypeDescriptor.GetProperties(typeof(Foo))["Bar"];
foreach (Attribute attrib in bar.Attributes) {
Console.WriteLine(attrib);
}
}
}
輸出:
System.SerializableAttribute
System.ComponentModel.AttributeProviderAttribute
System.ComponentModel.EditorAttribute
System.Runtime.InteropServices.ComVisibleAttribute
System.Runtime.InteropServices.ClassInterfaceAttribute
System.ComponentModel.TypeConverterAttribute
System.ComponentModel.MergablePropertyAttribute
- 3 回答
- 0 關(guān)注
- 732 瀏覽
添加回答
舉報(bào)