I get child properties of an object with this code,
PropertyDescriptorCollection childProperties = TypeDescriptor.GetProperties(theObject)[childNode.Name].GetChildProperties();
think that “theObject” variable is a TextBox and I try to set TextBox.Font.Bold = true;
I use this code for main properties and it works when I customize for main properties. But when I access the child properties,
I get an error which is “Object reference not set to an instance of an object.”.
foreach (PropertyDescriptor childProperty in childProperties)
{
foreach (XmlAttribute attribute in attributes)
{
if (childProperty.Name == attribute.Name)
{
if (!childProperty.IsReadOnly)
{
object convertedPropertyValue = ConverterHelper.ConvertValueForProperty(attribute.Value, childProperty);
childProperty.SetValue(theObject, convertedPropertyValue); //exception throw here
break;
}
}
}
}
It looks like you are passing the wrong object to
SetValue– on the face of it it looks like you get something like:And then you get the
Fontproperty of the text box and theBoldproperty of the font and then you try to assign the valuetrueto theBoldproperty ofTextBox. Obviously this is not going to work.Maybe something like this:
Note that the context for setting properties of the child object is the child object and not the parent object.