从string创建类实例

Tim*_*per 4 c# reflection class instance argumentnullexception

我有一个C#方法,它从字符串创建一个类的新实例,但是,我在运行代码时遇到错误.

obj = (ClassX)Activator.CreateInstance(Type.GetType("classPrefix_" + className));
Run Code Online (Sandbox Code Playgroud)

ArgumentNullException未处理

值不能为空

参数名称:类型

任何有关此错误的帮助将不胜感激.

Ama*_*ena 5

您可能需要使用程序集限定名作为Type.GetType的参数

eg AssemblyName.Namespace.ClassName
Run Code Online (Sandbox Code Playgroud)

关于程序集限定名称的MSDN Doc