C#中根据类的名称字符串创建类的实例

来源:互联网 发布:北电网络被谁收购 编辑:程序博客网 时间:2024/06/06 09:59

这种用法很像是工厂类,但是我们不需要自己实现字符串到类型的对应关系,也不需要创建的类有继承关系,

代码如下:

 

// 第一步:得到类的全名(命名空间+类名)

string adaptorName = this.GetType().FullName + “.Case” + "+HA_" + this._pi.Name;

 

// 第二部:根据全名得到类的类型

Type adaptorType = Type.GetType(adaptorName);

HumanizerAdaptorCommon hac = null;//基类

 

// 第三步:创建类实例

if (adaptorType != null) { hac = Activator.CreateInstance(Type.GetType(adaptorName), this._pi) as HumanizerAdaptorCommon; }

 

 

 

string fullName = this.GetType().FullName;

 

            string Namespace = this.GetType().Namespace;

 

            string name = this.GetType().Name;

 

原创粉丝点击