C#在类工厂中动态创建类的实例,所使用的方法为:

1. Activator.CreateInstance (Type)

2. Activator.CreateInstance (Type, Object[])

 

两种方法区别仅为:创建无参数的构造方法和创建有参数的构造函数。

//Activator.CreateInstance(Type)

object result = null;
Type typeofControl =null;

typeofControl = Type.GetType(vFullClassName);
result = Activator.CreateInstance(typeofControl);

//Activator.CreateInstance(Type,Object[])

object result = null;
Type typeofControl =null;

typeofControl = Type.GetType(vFullClassName);
result = Activator.CreateInstance(typeofControl, objParam);

但是在动态创建时,可能会动态使用到外部应用的DLL中类的实例,则此时需要进行反编译操作,使用Reflection命名控件下的Assembly类。

//先使用Assembly类载入DLL,再根据类的全路径获取类

object result = null;
Type typeofControl = null;
Assembly tempAssembly;

tempAssembly = Assembly.LoadFrom(vDllName);
typeofControl = tempAssembly.GetType(vFullClassName);
result = Activator.CreateInstance(typeofControl, objParam);

(转) C# Activator.CreateInstance()方法使用的更多相关文章

  1. 关于Assembly.CreateInstance()与Activator.CreateInstance()方法

    于Assembly.CreateInstance()与Activator.CreateInstance()方法 动 态创建类对象,大多是Activator.CreateInstance()和Activ ...

  2. Activator.CreateInstance 方法 (Type) 的用法

    转自:http://www.cnblogs.com/lmfeng/archive/2012/01/30/2331666.html Activator.CreateInstance 方法 (Type) ...

  3. C#中Activator.CreateInstance()方法用法分析

    本文实例讲述了C#中Activator.CreateInstance()方法用法. Activator 类 包含特定的方法,用以在本地或从远程创建对象类型,或获取对现有远程对象的引用. C#在类工厂中 ...

  4. C# Activator.CreateInstance()方法使用

    C#在类工厂中动态创建类的实例,所使用的方法为: 1. Activator.CreateInstance (Type) 2. Activator.CreateInstance (Type, Objec ...

  5. 注意Activator.CreateInstance两个重载方法的性能

    今天扩展一个Type的扩展方法New: public static object New(this Type type, params object[] args) { Guard.ArgumentN ...

  6. C# Activator.CreateInstance()

    C#在类工厂中动态创建类的实例,所使用的方法为: 1. Activator.CreateInstance (Type) 2. Activator.CreateInstance (Type, Objec ...

  7. Assembly.CreateInstance和Activator.CreateInstance

    本来是在设计模式中的工厂方法,在实现抽象工厂时,用到了一直都不熟悉的反射. namespace Factory { public abstract class Factory { public abs ...

  8. EF Core使用SQL调用返回其他类型的查询 ASP.NET Core 2.0 使用NLog实现日志记录 CSS 3D transforms cSharp:use Activator.CreateInstance with an Interface? SqlHelper DBHelper C# Thread.Abort方法真的让线程停止了吗? 注意!你的Thread.Abort方法真

    EF Core使用SQL调用返回其他类型的查询   假设你想要 SQL 本身编写,而不使用 LINQ. 需要运行 SQL 查询中返回实体对象之外的内容. 在 EF Core 中,执行该操作的另一种方法 ...

  9. cSharp:use Activator.CreateInstance with an Interface?

    ///<summary> ///数据访问工厂 ///生成時間2015-2-13 10:54:34 ///塗聚文(Geovin Du) /// (利用工厂模式+反射机制+缓存机制,实现动态创 ...

随机推荐

  1. 【转】setStyleSheet用法

    [转自]http://blog.csdn.net/seanyxie/article/details/5925723 使用setStyleSheet来设置图形界面的外观: QT Style Sheets ...

  2. 在windows下搭建linux-c学习环境

    下载virtualbox并安装: https://www.virtualbox.org/wiki/Downloads 现在vagrant并安装: https://www.vagrantup.com/d ...

  3. jsp语法与标签

    语法: <% 多行java代码 %> 在一个JSP页面中可以有多个脚本片断,在两个或多个脚本片断之间可以嵌入文本.HTML标记和其他JSP元素. 举例: <% int x = 10; ...

  4. 使用国内镜像更新sdk方法

    使用国内的镜像资源: 1.mirrors.neusoft.edu.cn //东软信息学院 2.ubuntu.buct.edu.cn/ubuntu.buct.cn //北京化工大学 3.mirrors. ...

  5. AVAudioPlayer音频播放器-备用

    IOS中有三种播放音频的方式:AVAudioPlayer.音频服务.音频队列. 此文主要讲AVAudioPlayer,其他两个请见相关文章. AVAudioPlayer在AVFoundation框架下 ...

  6. 离线破win2003域账号密码(更新域数据库不在默认路径的情况)

    国内网站讲了许多方法,很多也讲得不清楚.我把我尝试成功的经验跟大家分享. 方法是按照tim的文章操作,链接如下: http://pauldotcom.com/2011/11/safely-dumpin ...

  7. Linux下配置QT环境

    一.下载Qt源码包到本机,然后解压缩 #tar zxvf qt-x11-opensource-src-4.3.2.tar.gz -C /usr/local //将qt-x11-opensource-s ...

  8. Kill Process by Name

    Kill Process by Name(works in: Microsoft Windows 95/98/ME/NT/2000/XP)It is sometimes necessary to te ...

  9. 【转】android UI进阶之实现listview中checkbox的多选与记录--不错

    原文网址:http://www.cnblogs.com/notice520/archive/2012/02/17/2355415.html 今天继续和大家分享涉及到listview的内容.在很多时候, ...

  10. 算法导论(第三版)Problems2(归并插入排序、数列逆序计算)

    讨论内容不说明,仅提供相应的程序. 2.1:归并插入排序θ(nlgn) void mergeInsertionSort(int a[], int l, int r, int k) { int m; & ...