日期:2014-05-20  浏览次数:20836 次

属性上的自定义特性(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);