在我的反射代码中,我遇到了我的通用代码部分的问题.特别是当我使用一个字符串.
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)
如何使用反射创建字符串?