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>
.