创建一个从字符串类的实例 [英] Create class instance from string

查看:187
本文介绍了创建一个从字符串类的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  OBJ =(ClassX)Activator.CreateInstance(Type.GetType(classPrefix_+的className)); 




ArgumentNullException了未处理



值不能为空



参数名:键入




在此错误,将不胜感激任何帮助。


解决方案

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

 如AssemblyName.Namespace.ClassName 

上装配合格的名称MSDN文档


I have a C# method which creates a new instance of a class from a string, however, I get an error when running the code.

obj = (ClassX)Activator.CreateInstance(Type.GetType("classPrefix_" + className));

ArgumentNullException was unhandled

Value cannot be null

Parameter name: type

Any help on this error would be appreciated.

解决方案

You may need to use the assembly qualified name as the argument to Type.GetType

eg AssemblyName.Namespace.ClassName

MSDN Doc on assembly qualified names

这篇关于创建一个从字符串类的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆