[英]How to use Convert.ChangeType() when conversionType is a nullable int
[英]How do I use Convert.ChangeType to cast to a specific class?
有谁知道我如何使用Convert.ChangeType从XML转换为特定的类?
例如:
我的财产
public Point Loc
{
get { return GetValue<Point2D>("Loc"); }
set { SetValue<Point2D>("Loc", value); }
}
我的XML:
<Loc>
<X>1.0</X>
<Y>1.0</Y>
</Loc>
致电转换:
prop.SetValue(targetObj,
Convert.ChangeType(xmlProperty.Value, prop.PropertyType));
我看过使用IConvertible的方法,但是没有一个方法被调用。
我可以找到的所有示例都使用简单类型。 没有显示强制转换为类的实例。
谢谢,
里克
为什么您认为需要Convert.ChangeType()
?
当然,您可以使用以下重载的SetValue
方法在PropertyInfo
对象上调用SetValue()
:
prop.SetValue(targetObj,xmlProperty.Value,null);
但是,您首先需要使用Activator.CreateInstance()
类的方法创建特定类型的实例,并为Point设置各个属性。 随后,然后根据需要将点对象/结构分配为SetValue
输入。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.