我有一个我已经定义的对象,我希望使用PropertyDescriptor修改一个名为“DeviceType”的属性,但它对我来说不起作用。
我可以检索属性DeviceType的值,但是当我使用SetValue()更改其属性值时,我总是得到异常:“对象引用未设置为对象的实例”,即使我尝试设置属性与我刚检索到的值相同。
以下是一些代码:
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(theBase);
var objValue = properties["DeviceType"].GetValue(theBase);
try
{
if (theBase != null && objValue != null)
{
properties["DeviceType"].SetValue(theBase, objValue);
}
}
catch (Exception ex)
{
}
非常感谢任何帮助。 感谢。
答案 0 :(得分:2)
创建对象时,属性未正确初始化,因此setter属性生成了空引用异常错误。