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