【发布时间】:2011-11-01 13:30:40
【问题描述】:
有什么干净的方法可以从表达式树中获取PropertyDescriptor?
我目前有PropertyInfo,但理想情况下我想要PropertyDescriptor,我的代码:
var prop =
(System.Reflection.PropertyInfo)
((MemberExpression)
((Expression<Func<TestClass, long>>)
(p => p.ID)).Body).Member;
我需要 PropertyDescriptor 是因为我需要使用:
if (prop.CanResetValue(this))
{
prop.ResetValue(this);
}
else
{
prop.SetValue(this, null);
}
我不能使用PropertyInfo.SetValue(this, null, null),因为它不适合我的需要,因为我需要重置为DefaultValueAttribute指定的默认值。
【问题讨论】:
标签: c# .net expression-trees typedescriptor propertydescriptor