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

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

问题描述

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

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未处理

ArgumentNullException was unhandled

值不能为null

参数名称:类型


推荐答案

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

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

eg AssemblyName.Namespace.ClassName

MSDN文档上的组装限制名称

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

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