Get overridden property attribute

二次信任 提交于 2019-12-02 02:41:54

问题


I have a custom attribute like this:

public class PropertyInfoAttribute : Attribute
{
    public bool IsAutoComplete { get; set; }
}

And there is a class like this:

public class Article
{
    public virtual int Order { get; set; }
    //other properties
}

In another class,which inherits from Article, I override Order property and declare the attribute for it like this:

public class ArticleDetails : Article
{
    [PropertyInfo(IsAutoCompele = true)]
    public override int Order { get; set; }
}

The problem appears when I want to get attributes by using the GetCustomAttributes method in PropertyInfo class. I do it like this:

PropertyInfo propInfo = //do something for getting property info from the
                        //ArticleDetails class;

var attr = propInfo.GetCustomAttribute<PropertyInfoAttribute>();

But it returns nothing! I don't know why!

UPDATE:

I get property info in this method:

    public static void InitPropertyInfoAttribute<TModel, TProperty>(MvcHtmlString source, Expression<Func<TModel, TProperty>> expression)
    {
        PropertyInfo propInfo = (expression.Body as MemberExpression).Member as PropertyInfo;
    }

回答1:


I think the problem hides in here:

PropertyInfo propInfo = //do something for getting property info from the
                        //ArticleDetails class;

I presume that you actually obtain this property info from Article class, not ArticleDetails and that's why it returns null. The following snippet worked as expected for me:

PropertyInfo propInfo = typeof(ArticleDetails).GetProperty("Order");
var attr = propInfo.GetCustomAttribute<PropertyInfoAttribute>();

Update


According to your update - the problem is that Member property of the MemberExpression points to the Article type;

As a solution to this you can update your InitPropertyInforAttribute as follows:

MemberExpression memberExpression = (expression.Body as MemberExpression);
return typeof(TModel).GetProperty(memberExpression.Member.Name);

And don't forget that you should pass ArticleDetails as first generic type parameter - InitPropertyInfoAttribute<ArticleDetails, propertyType>.




回答2:


Sorry, but I can't reproduce the error. Attribute is extracted. Could you provide the details?

// Your classes
public class PropertyInfoAttribute: Attribute {
  public bool IsAutoComplete {
    get;
    set;
  }
}

public class Article {
  public virtual int Order {
    get;
    set;
  }
}

public class ArticleDetails: Article {
  [PropertyInfo(IsAutoComplete = true)]
  public override int Order {
    get;
    set;
  }
}

...

// My test

// Let's do it explicitly:
// ask for public and instance (not static) property
PropertyInfo pi = typeof(ArticleDetails).GetProperty("Order", BindingFlags.Public | BindingFlags.Instance);
// Then ask for the attribute
Attribute at = pi.GetCustomAttribute(typeof(PropertyInfoAttribute));

// And, finally, check if attribute is existing
// ... And so, assertion passes - attribute is existing
Trace.Assert(!Object.ReferenceEquals(null, at), "No Attribute found.");


来源:https://stackoverflow.com/questions/20835132/get-overridden-property-attribute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!