4
所以我有一種情況,我希望能夠將屬性應用於派生類中的(虛擬)方法,但我希望成爲能夠給出一個在我的基類中使用這些屬性的默認實現。從基類訪問應用於派生類中的方法的屬性
我原來這樣做的計劃是要覆蓋的方法在派生類中並調用基實現,在這一點上應用所需的屬性,如下所示:
public class Base {
[MyAttribute("A Base Value For Testing")]
public virtual void GetAttributes() {
MethodInfo method = typeof(Base).GetMethod("GetAttributes");
Attribute[] attributes = Attribute.GetCustomAttributes(method, typeof(MyAttribute), true);
foreach (Attibute attr in attributes) {
MyAttribute ma = attr as MyAttribute;
Console.Writeline(ma.Value);
}
}
}
public class Derived : Base {
[MyAttribute("A Value")]
[MyAttribute("Another Value")]
public override void GetAttributes() {
return base.GetAttributes();
}
}
僅打印「一個基地值測試「,而不是我真正想要的其他值。
有沒有人有任何建議,我可以如何修改此以獲得所需的行爲?
這做到了。謝謝! – 2008-11-10 19:47:26