문제

I have an object that I have defined and I wish to modify one of its property called "DeviceType" using PropertyDescriptor but it's not working out for me.

I can retrieve the value of the property DeviceType but when I use SetValue() to change its property value, I always get the exception: "Object reference not set to an instance of an object" even if I try to set the property to the same value that I just retrieved.

Here is some code:

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)
{

}

Any help would be much appreciated. Thanks.

도움이 되었습니까?

해결책

The property was not initialized correctly when my object was created and so the setter property was generating a null reference exception error.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top