(转) C# Activator.CreateInstance()方法使用
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()方法使用的更多相关文章
- 关于Assembly.CreateInstance()与Activator.CreateInstance()方法
于Assembly.CreateInstance()与Activator.CreateInstance()方法 动 态创建类对象,大多是Activator.CreateInstance()和Activ ...
- Activator.CreateInstance 方法 (Type) 的用法
转自:http://www.cnblogs.com/lmfeng/archive/2012/01/30/2331666.html Activator.CreateInstance 方法 (Type) ...
- C#中Activator.CreateInstance()方法用法分析
本文实例讲述了C#中Activator.CreateInstance()方法用法. Activator 类 包含特定的方法,用以在本地或从远程创建对象类型,或获取对现有远程对象的引用. C#在类工厂中 ...
- C# Activator.CreateInstance()方法使用
C#在类工厂中动态创建类的实例,所使用的方法为: 1. Activator.CreateInstance (Type) 2. Activator.CreateInstance (Type, Objec ...
- 注意Activator.CreateInstance两个重载方法的性能
今天扩展一个Type的扩展方法New: public static object New(this Type type, params object[] args) { Guard.ArgumentN ...
- C# Activator.CreateInstance()
C#在类工厂中动态创建类的实例,所使用的方法为: 1. Activator.CreateInstance (Type) 2. Activator.CreateInstance (Type, Objec ...
- Assembly.CreateInstance和Activator.CreateInstance
本来是在设计模式中的工厂方法,在实现抽象工厂时,用到了一直都不熟悉的反射. namespace Factory { public abstract class Factory { public abs ...
- 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 中,执行该操作的另一种方法 ...
- cSharp:use Activator.CreateInstance with an Interface?
///<summary> ///数据访问工厂 ///生成時間2015-2-13 10:54:34 ///塗聚文(Geovin Du) /// (利用工厂模式+反射机制+缓存机制,实现动态创 ...
随机推荐
- SQL Server 中的SET XACT_ABORT各种用法及显示结果
源地址:http://www.cnblogs.com/rob0121/articles/2320932.html 点击进入 默认行为:默认为SET XACT_ABORT OFF,没有事务行为. S ...
- JQUERY1.9学习笔记 之基本过滤器(三)偶数选择器
偶数选择器 jQuery( ":even" ) 例:查询偶数个表格的行. <!DOCTYPE html><html lang="zh-cn"& ...
- php数组存到文件的实现代码
php的数组十分强大,有些数据不存入数据库直接写到文件上,用的时候直接require 第一次分享代码: (实际中有用到把数组存在到文件中的功能,不过分享的代码跟实际应用中的有点不同) 代码1: < ...
- liunx运维面试题汇总二
一.填空题:1. 在Linux系统中,以 文件 方式访问设备 .2. Linux内核引导时,从文件 /etc/fstab 中读取要加载的文件系统. 3. Linux文件系统中每个文件用 i节点 来标识 ...
- 不能将值 NULL 插入列 'ID',表 'EupStoreDemoDB.dbo.OrderDiary';列不允许有 Null 值。INSERT 失败。
MVC,使用EF构建实体.将数据存入数据库,执行到_db.SaveChange()时,会报如下错误:
- 【学习笔记】【oc】copy与mutableCopy
copy 返回一个不可变的对象: mutableCopy 返回一个可变的对象: 使用copy方法时 类必须实现:<NSCopying>协议中的-(id)copyWithZone:(NSZo ...
- jQuery CSS3 照片墙
<html> <head> <style type="text/css"> .picture-wall-container{ position: ...
- Spark Streaming Backpressure分析
1.为什么引入Backpressure 默认情况下,Spark Streaming通过Receiver以生产者生产数据的速率接收数据,计算过程中会出现batch processing time > ...
- RAILS 4 ON RUBY的AJAX实现过程
XXX,最近在笨手笨脚的写一个自动化SVN更新的WEB操作小平台, 这次决定用RAILS 4实现. 但因为网上的教材都是以3或2版本实现的.所以这次搞了不少弯路,现总结如下: 相关的VIEW代码: & ...
- Altium Designer同一个工程里不同原理图导入到不同的PCB
问题: 在用Altium Designer进行PCB工程设计时,有时一个工程里可能不止一块PCB,比如,一个设备里有主板和扩展板或者按键板等等,这时就需要在一个工程里添加多个PCB文件.如图: 我们知 ...