如何反映如果一个属性有一个公共设置器
问题描述:
即时将对象模型保存到XML,但当我加载回来我试图使用PropertyInfo.SetValue()因为该属性没有setter只是一个吸气。如何反映如果一个属性有一个公共设置器
我想不保存只有getter的属性,或者找出加载的属性是否对我有效,以尝试设置值或不设置值。
任何人都知道如何做到这一点
干杯
答
您可以使用PropertyInfo.GetSetMethod
- 这将返回null
如果任一属性为只读或setter方法是不公开的。
if (property.GetSetMethod() != null)
{
// Yup, you can write to it.
}
如果你可以用一个非公开的setter应付,你可以使用:
if (property.GetSetMethod(true) != null)
{
// Yup, there's a setter - but it may be private
}
你知道一个深不可测的数量的东西。非常感谢 – DrLazer 2010-05-11 13:55:10