如何生成一个未知类型在运行时的一个实例?实例、类型

2023-09-03 10:12:24 作者:落叶飘舞 舞去一世繁华

我已经得到了C#中的以下内容:

 字符串类型名=System.Int32的;
字符串值=4;
 

载文应采取两个字符串来生成指定类型的具有指定值的对象...

结果应该是:

 对象o = CreateUnknownType(类型名称,值);
...
INT32测试=(Int32)在O;
 

解决方案

这是什么你在想什么?

 对象result = Convert.ChangeType(4,Type.GetType(System.Int32的));
 
第一行代码android的第一个例子在虚拟机中运行时总是已停止运行,求大神帮帮忙.

i've got the following in C#:

string typename = "System.Int32";
string value = "4";

theses two strings should be taken to generate an object of the specified type with the specified value...

result should be:

object o = CreateUnknownType(typename, value);
...
Int32 test = (Int32)o;

解决方案

Is this what are you are thinking?

object result = Convert.ChangeType("4", Type.GetType("System.Int32"));