如何动态地投string类型的对象类型T的对象 [英] How to dynamically cast an object of type string to an object of type T
本文介绍了如何动态地投string类型的对象类型T的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这样的XML文档
<AdditionalParameters>
<PublishToPdf Type ="System.Boolean">False</PublishToPdf>
</AdditionalParameters>
在我的代码,我试图建立的包含< PublishToPdf方式>
节点
in my code and I'm trying to build an array of arguments containing the <PublishToPdf>
node.
object test = (object) ((typeof(publishNode.Attributes["Type"].value)) publishNode.InnerText);
这休息,当然,编译时。我无法弄清楚如何铸就 publishNode.InnerText('假')
的XML文件中指定类型的运行时定义的对象并将其存储在一个对象(这将节省型)。
This breaks at compile time of course. I can't figure out how to cast the publishNode.InnerText('false')
to a runtime defined object of type specified in the XML file and store it in an object (which will conserve the type).
推荐答案
您可以使用 Convert.ChangeType
:
object value = Convert.ChangeType(stringValue, destinationType);
这篇关于如何动态地投string类型的对象类型T的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文