有没有从表达式树中获取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指定的默认值。
发布于 2011-11-01 21:47:53
像这样的怎么样?(未经测试,抱歉!)
var prop = /* same as in your example above */
var descriptors = TypeDescriptor.GetProperties(this);
var descriptor = descriptors[prop.Name];
if (descriptor.CanResetValue(this))
{
descriptor.ResetValue(this);
}
else
{
descriptor.SetValue(this, null);
}https://stackoverflow.com/questions/7967214
复制相似问题