▲△▲△ 这是.NET Framework的bug吗?AttributeUsage的Inherited属性对Property无效!

qpl007 2005-03-13 01:22:09
MSDN 对 AttributeUsage 特性 Inherited 属性的说明:获取或设置一个布尔值,该值指示指示的属性能否由派生类和重写成员继承。

但是,请看以下试验 ——

//定义Attribute,将AttributeUsage的Inherited属性设为 true
[AttributeUsage(AttributeTargets.All, Inherited = true, AllowMultiple = false)]
public class MyAttribute : Attribute {}

public class MyClassBase
{
[MyAttribute] //将 Attribute 应用于方法上
public virtual void Method(){}

[MyAttribute] //将 Attribute 应用于属性上
public virtual int Property
{
get{return 0;}
set{}
}
}

public class MyClass: MyClassBase
{
public override void Method(){} //在派生类中重写方法

public override int Property //在派生类中重写属性
{
get{return 0;}
set{}
}
}

public class TestClass
{
public void run()
{
MyClass test = new MyClass();

//取应用于Method方法的特性个数
int a = test.GetType().GetMethod("Method").GetCustomAttributes(true).Length;
//取应用于Property属性的特性个数
int b = test.GetType().GetProperty("Property").GetCustomAttributes(true).Length;

// 输出结果
// a = 1 说明方法能成功继承父类的特性
// b = 0 说明属性无法继承父类的特性
// way ????
}
}

难道AttributeUsage的Inherited属性对Property无效吗?
那我要增加父类属性上的特性该怎么做??

忘高手们不吝赐教!!
...全文
265 25 打赏 收藏 转发到动态 举报
写回复
用AI写文章
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
hedonister 2005-03-16
  • 打赏
  • 举报
回复
应该能实现,不过还是觉得别扭
hivak47 2005-03-16
  • 打赏
  • 举报
回复
不是吧?
qpl007 2005-03-16
  • 打赏
  • 举报
回复
多谢两位大哥的帮助!

我是想通过 Attribute 来控制权限,并且希望子类的属性在增加权限的同时,能拥有父类属性的权限!

再次感谢两位大哥 ^-^
qpl007 2005-03-16
  • 打赏
  • 举报
回复
原来如此!

那我怎么才能增加父类属性上的特性?
难道要把父类属性上的特性在子类属性上全部重新写一次??

public class BaseClass
{
[Attribute1,Attribute2, ... Attribute100]
public virtual int property{...}
}

public class MyClass : BaseClass
{
//先把父类属性上的特性全部重写一遍,在加新的特性
[Attribute1,Attribute2, ... Attribute100,Attribute101]
public override int property{...}
}

这样写不是很累也很麻烦?
而且随着继承链的增加,为子类属性增加特性岂不成为很痛苦的事???

有没有什么其他方法让子类属性也能继承父类属性的特性?

public class MyClass : BaseClass
{
//能这样多好啊!
[Attribute101]
public override int property{...}
}

速马 2005-03-16
  • 打赏
  • 举报
回复
嗯,试验了下,这里还有个更酷的办法 (-:

property本质上也是method,所以:
把Attribute1 - Attribute100设置为AttributeTargets.Method
然后:
public class BaseClass
{
public virtual int property
{
[Attribute1,Attribute2, ... Attribute100]
get{...}
}
}

public class MyClass : BaseClass
{
public override int property
{
[Attribute101]
get{...}
}
}

这样可以在新的属性上增加新的attribute
速马 2005-03-16
  • 打赏
  • 举报
回复
public class BaseClass
{
[Attribute1,Attribute2, ... Attribute100]
public virtual int property{return getproperty();}

protected virtual int getproperty(){...}
}

public class MyClass : BaseClass
{
protected override int getproperty(){...}
}
速马 2005-03-16
  • 打赏
  • 举报
回复
有个变通的办法
在基类的property里面调用一个protected的虚函数
子类不重写property,而是重写这个虚函数
可以达到同样的效果(子类直接使用基类的property,并且有相同的attributes)
心情解码 2005-03-16
  • 打赏
  • 举报
回复
:)





---






--------
saucer 2005-03-16
  • 打赏
  • 举报
回复
不清楚你的目的是什么,恐怕你也不愿意在编码里自己顺着类结构往上找某个Atrtibute,建议你先用Method吧,别一条路走到黑,再把问题提到DevelopMentor或微软的英文新闻组去问


PropertyInfo pi = test.GetType().GetProperty("Property");
Attribute[] atts = Attribute.GetCustomAttributes(pi, true);
Console.WriteLine(atts.Length);

atts = Attribute.GetCustomAttributes(pi, typeof(MyAttribute), true);
Console.WriteLine(atts.Length);
qpl007 2005-03-16
  • 打赏
  • 举报
回复
up + 期待……
qpl007 2005-03-16
  • 打赏
  • 举报
回复
期待……
saucer 2005-03-16
  • 打赏
  • 举报
回复
yes, it sounds odd, but from this post, it seems, property and event are special

http://groups-beta.google.com/group/microsoft.public.dotnet.framework.clr/msg/231f3e2018859b06

"As a side note, MemberInfo.IsDefined does not search the overridden
properties and events for inheritable attributes because the CLS does not
define what it means to "override" a property or event. "
haoco 2005-03-15
  • 打赏
  • 举报
回复
up
qpl007 2005-03-15
  • 打赏
  • 举报
回复
up
zr1982930 2005-03-15
  • 打赏
  • 举报
回复
我顶 !
qpl007 2005-03-15
  • 打赏
  • 举报
回复
Thanks!

期待高手的回答!!
kingboy001 2005-03-15
  • 打赏
  • 举报
回复
我也想知道,up一下
qpl007 2005-03-15
  • 打赏
  • 举报
回复
接着顶,直到高手出现
qpl007 2005-03-14
  • 打赏
  • 举报
回复
是我没把问题描述清楚吗?

怎么没人肯帮忙?
qpl007 2005-03-14
  • 打赏
  • 举报
回复
高手们还没上班吗?
加载更多回复(5)

110,537

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧