首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

属性上的自定义特性(Attribute)没有在重载的属性上找到,Inherited已设true解决思路

2012-02-03 
属性上的自定义特性(Attribute)没有在重载的属性上找到,Inherited已设true我的自定交特性:[AttributeUsage

属性上的自定义特性(Attribute)没有在重载的属性上找到,Inherited已设true
我的自定交特性:
[AttributeUsage(AttributeTargets.All, Inherited = true, AllowMultiple = true)]
 public class CommFlagAttribute : System.Attribute

基类:
[CommFlag("DictionaryItem")]
  public class DictionaryItem : Entity
  {
  [CommFlag("itemvalue")]
  public virtual String itemvalue
  {
  get;
  set;
  }

派生类:
public class ViewDictionaryItem : DictionaryItem
{

  public override string itemvalue
  {
  get
  {
  return base.itemvalue;
  }
  set
  {
  。。。。
  }
  }

结果在反射时,这个itemvalue上就找不到CommFlag特性了,其它没重载的都可以。不是说Inherited设置后就可以在子类中也有吗?

[解决办法]
不要用PropertyInfo.GetCustomAttributes,而是用Attribute的静态方法:

C# code
PropertyInfo pi = typeof(ViewDictionaryItem).GetProperty("itemvalue");Attribute[] attributes = Attribute.GetCustomAttributes(pi, true); 

热点排行