相关疑难解决方法(0)

我如何使用Activator.CreateInstance与字符串?

在我的反射代码中,我遇到了我的通用代码部分的问题.特别是当我使用一个字符串.

var oVal = (object)"Test";
var oType = oVal.GetType();
var sz = Activator.CreateInstance(oType, oVal);
Run Code Online (Sandbox Code Playgroud)

例外

An unhandled exception of type 'System.MissingMethodException' occurred in mscorlib.dll

Additional information: Constructor on type 'System.String' not found.
Run Code Online (Sandbox Code Playgroud)

我试过这个用于测试目的,它也发生在这个单一的衬里

var sz = Activator.CreateInstance("".GetType(), "Test");
Run Code Online (Sandbox Code Playgroud)

我最初写的

var sz = Activator.CreateInstance("".GetType());
Run Code Online (Sandbox Code Playgroud)

但我得到这个错误

Additional information: No parameterless constructor defined for this object.
Run Code Online (Sandbox Code Playgroud)

如何使用反射创建字符串?

.net c# reflection activator

28
推荐指数
2
解决办法
3万
查看次数

标签 统计

.net ×1

activator ×1

c# ×1

reflection ×1