Does anyone know how I can use Convert.ChangeType to cast from XML to a specific class?
For example:
My Property
public Point Loc
{
get { return GetValue<Point2D>("Loc"); }
set { SetValue<Point2D>("Loc", value); }
}
My XML:
<Loc>
<X>1.0</X>
<Y>1.0</Y>
</Loc>
Call to convert:
prop.SetValue(targetObj,
Convert.ChangeType(xmlProperty.Value, prop.PropertyType));
I have looked at using IConvertible but none of the methods are being called.
All of the examples I could find are using simple types. None show casting to an instance of a class.
Thank you,
Rick
Why do you think you need Convert.ChangeType()
?
Surely you could just call SetValue()
on the PropertyInfo
object using the following overloaded SetValue
method.:
prop.SetValue(targetObj,xmlProperty.Value,null);
However, you will first need to create an instance of the specific type, using something like Activator.CreateInstance()
and set the individual properties for the Point. Subsequently, you then assign that point object/struct as input for the SetValue
as you have it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.