I am trying to create an application that creates objects from data that is read from an XML file.
Using reflection I have managed to create the objects I need and assign some of the properties like primitive types and ENUM types.
For primitive types where property is a Dictionary entry with the Property name to change and the value to set
type.GetProperty((string)property.Key).SetValue(control, Convert.ChangeType((string)property.Value, propertyType, null), null);
and for ENUM types
object desiredPropertyValue = Enum.Parse(propertyType, (string)property.Value);
propertyInfo.SetValue(control, desiredPropertyValue, null);
The problem I have is that I can't seem to find a way to set other types of properties like Fontweight, fontfamily, Margin and many others I think these are of type structure, any help would be appreciated
You can use the associated type converters to convert the objects to/from a string. For example, for FontWeight you can use the FontWeightConverter like so:
object value = new FontWeightConverter().ConvertFromString((string)property.Value)
Likewise, you can use ConvertToString to convert to a string for saving in your dictionary.
来源:https://stackoverflow.com/questions/6073677/wpf-assigning-control-properties-using-reflection-in-c-sharp