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. a标签增加onclick事件提示未定义function

    项目使用的是ext框架,版本是ext4.2 出现的问题代码如下: renderer : function(value){ var html = "<a href=\"java ...

  2. trident教程

      (一)理论基础更多理论以后再补充,或者参考书籍1.trident是什么?Trident is a high-level abstraction for doing realtime computi ...

  3. 关于PHP伪静态Rewrite设置

    Rewirte主要的功能就是实现URL的跳转和隐藏真实地址,基于Perl语言的正则 表达式规范.平时帮助我们实现拟静态,拟目录,域名跳转,防止盗链等   一.Apache配置:   1.支持httpd ...

  4. php实现网页HTML标签补全方法

    如果你的网页内容的html标签显示不全,有些表格标签不完整而导致页面混乱,或者把你的内容之外的局部html页面给包含进去了,我们可以写个函数方法来补全html标签以及过滤掉无用的html标签. php ...

  5. 在centos中使用vim编辑器

    下面用编辑crontab举个例子: 在命令行输入 crontab -e 会直接进入vim编辑模式编辑crontab文件. 随后可以输入“i”进入insert模式 然后移动光标在指定的位置添加文字,可以 ...

  6. akka简单示例-1

    刚刚开始接触akka,网上找了2个简单示例,并在公司运营机器上尝试,踩了一些坑,在此记录. 1. 本地hello world [torstan@sparkb5-i ~/akka_example/hel ...

  7. [转]Android读写文件

    一.       从resource中的raw文件夹中获取文件并读取数据(资源文件只能读不能写) String res = ""; try{ InputStream in = ge ...

  8. Android——在活动中使用Menu

    ADT在HelloWorldActivity中自动创建了一个onCreateOptionsMenu()方法.这个方法是用于在活动中创建菜单的,现在我们来仔细分析一下了.手机毕竟和电脑不同,它的屏幕空间 ...

  9. Altium Designer 等长线&&蛇形线

    Altium Designer 里面怎么画等长线 (1)一般是将走线布完后,新建一个class. Design -> Classes 如上图添加完后可以点击close. (2)快捷键 T + R ...

  10. QT:不规则窗口的实现

    主要思路:1:将窗体设为Qt::FramelessWindowHint(去掉标题栏).2:用一幅有部分区域是透明的图片作为程序的界面,并将图片透明的地方设为穿透. 3:重载程序的鼠标事件. 运行时截图 ...