【发布时间】:2017-01-22 23:10:26
【问题描述】:
我正在尝试使用GetCustomAttributes() 来获取在属性上定义的属性。问题是该属性是一个被覆盖的属性,我无法弄清楚如何从表达式中提取被覆盖的属性。我只能弄清楚如何获得基类的那个。
这里有一些代码
public class MyAttribute : Attribute
{
//...
}
public abstract class Text
{
public abstract string Content {get; set;}
}
public class Abstract : Text
{
[MyAttribute("Some Info")]
public override string Content {get; set;}
}
现在我正试图将MyAttribute 从抽象类中取出。但我需要通过Expression 获得它。这是我一直在使用的:
Expression<Func<Abstract, string>> expression = c => c.Content;
Expression exp = expression.Body;
MemberInfo memberType = (exp as MemberExpression).Member;
var attrs = Attribute.GetCustomAttributes(memberType, true);
不幸的是,atts 最终是空的。问题是 menberType 最终是 Text.Content 而不是 Abstract.Content 类。所以当我得到属性时,它什么也不返回。
【问题讨论】:
标签: .net linq-expressions getcustomattributes