问题描述
林节省了一个对象模型到XML,但是当我在加载它回来,我在尝试使用PropertyInfo.SetValue(),因为酒店没有一个二传手只是一个getter时例外。
我想要么不救出来,只有有干将,或找出负载其是否有效,我尝试设置值与否的属性。
任何人都知道如何做到这一点。
干杯
您可以使用 PropertyInfo.GetSetMethod
- 这将返回空
如果有一个属性是只读或二传手是非公
<$ p。 $ p>
如果(property.GetSetMethod()!= NULL)
{
//是的,你可以写信给它。
}
如果你可以用一个非公开的setter应付,你可以使用:
如果(property.GetSetMethod(真)!= NULL)
{
//是啊,有一个二传手 - 但它可能是私人
}
Im saving an object model out to XML but when i load it back in I get exceptions when trying to use PropertyInfo.SetValue() because the property doesn't have a setter just a getter.
I want to either not save out the properties that only have getters or figure out on load whether its valid for me to try and set a value or not.
Anybody know how to do this
Cheers
You can use PropertyInfo.GetSetMethod
- this will return null
if either the property is read-only or the setter is non-public.
if (property.GetSetMethod() != null)
{
// Yup, you can write to it.
}
If you can cope with a non-public setter, you can use:
if (property.GetSetMethod(true) != null)
{
// Yup, there's a setter - but it may be private
}
这篇关于如何判断反思性如果一个属性有一个公共的二传手的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!