[英]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.