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);
参考处处:http://blog.csdn.net/jaydawson/article/details/5539438
--------------------------------------------------
参考二:
--------------------------------------------------
使用与指定参数匹配程度最高的构造函数来创建指定类型的实例。
命名空间:System
程序集:mscorlib(在 mscorlib.dll 中)
public static Object CreateInstance (Type type)
Activator.CreateInstance 泛型方法 ()
注意:此方法在 .NET Framework 2.0 版中是新增的。
创建类型的一个实例,该类型由指定的泛型类型参数指定。
命名空间:System
程序集:mscorlib(在 mscorlib.dll 中)
public static T CreateInstance<T> ()
Activator.CreateInstance
(Type)实例
输入一个类的名称为参数,返回一个相应的类的实例。
这在工厂模式中是非常有用的,这样,可以使程序有更高的扩展性
/*
* Created by SharpDevelop.
* User: 19004991
* Date: 2009-8-6
* Time: 9:10
*/
using System; namespace ActivatorCreateInstance
{
public interface IObject
{
void printName();
}
public class ClassExam : IObject
{
private string name="default name";
public ClassExam()
{
}
public ClassExam(string name)
{
this.name = name;
}
public void printName()
{
Console .WriteLine (this.name );
}
}
public class Program
{
public Program()
{
}
public static void Main(string[] args)
{
// TODO: Implement Functionality Here
IObject obj1=(IObject)Activator.CreateInstance(System.Type.GetType ("ActivatorCreateInstance.ClassExam"));
obj1.printName();
IObject obj2=(IObject)Activator.CreateInstance(System.Type.GetType("ActivatorCreateInstance.ClassExam"),new string[]{"seted new name"});
obj2.printName();
Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
}
}
}
网上代码System.Type.GetType内为"ActivatorCreateInstance.ClassExam,ActivatorExample"编译失败
C# Activator.CreateInstance()的更多相关文章
- 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 ...
- 关于Assembly.CreateInstance()与Activator.CreateInstance()方法
于Assembly.CreateInstance()与Activator.CreateInstance()方法 动 态创建类对象,大多是Activator.CreateInstance()和Activ ...
- Activator.CreateInstance 反射实例化对象
public class CommonReq { private String TransNo { get; set;} public String SubmitData { get; set; } ...
- Activator.CreateInstance 方法 (Type) 的用法
转自:http://www.cnblogs.com/lmfeng/archive/2012/01/30/2331666.html Activator.CreateInstance 方法 (Type) ...
- 用Activator.CreateInstance代替new实现类的实例化(转)
一直想得到这样一个函数,输入一个类的名称为参数,返回一个相应的类的实例. 这在工厂模式中是非常有用的 这样,可以使程序有更高的扩展性,例如,,下面的例子 如果现在有一个类,专门用来计算交通工具的速度, ...
- (转) C# Activator.CreateInstance()方法使用
C#在类工厂中动态创建类的实例,所使用的方法为: 1. Activator.CreateInstance (Type) 2. Activator.CreateInstance (Type, Objec ...
- C#中Activator.CreateInstance()方法用法分析
本文实例讲述了C#中Activator.CreateInstance()方法用法. Activator 类 包含特定的方法,用以在本地或从远程创建对象类型,或获取对现有远程对象的引用. C#在类工厂中 ...
- cSharp:use Activator.CreateInstance with an Interface?
///<summary> ///数据访问工厂 ///生成時間2015-2-13 10:54:34 ///塗聚文(Geovin Du) /// (利用工厂模式+反射机制+缓存机制,实现动态创 ...
随机推荐
- 删除数据表和清空数据表的内容(保存表结构)的SHELL脚本
A,删除指定数据库的所有数据表 #!/bin/bash # 删除mysql中所有表 # 示例: # Usage: ./script user password dbnane # Usage: ./sc ...
- 【POJ3415】 Common Substrings(后缀数组|SAM)
Common Substrings Description A substring of a string T is defined as: T(i, k)=TiTi+1...Ti+k-1, 1≤i≤ ...
- android实现json数据的解析和把数据转换成json格式的字符串
利用android sdk里面的 JSONObject和JSONArray把集合或者普通数据,转换成json格式的字符串 JSONObject和JSONArray解析json格式的字符串为集合或者一般 ...
- c++复习基础要点02 虚函数与模板 与static inline是否共存
1. 虚函数能否定义为模板函数 当一个类有虚函数时,它一定有一个虚表,用来纪录每个虚函数的实际地址.这也就是说这个虚表的大小是在编译期就确定了的.有多少个虚函数,虚表就纪录几个. ...
- 2015第45周五IE11实用开发工具摘录及设置IE缓存
UI响应工具的作用 UI响应工具顾名思义就是查看UI响应时间的工具,通过这个工具可以帮助我们确定应用中的哪些组件占用了多少CPU时间,让我们之后可以更有针对性的进行优化,从而最大限度地改善应用性能,同 ...
- Linux下把U盘格式化为fat32
在linux下也是支持fat32的,如果U盘中了病毒可以插入linux系统进行格式化比较安全,下面介绍如何在linux下把u盘格式化为fat32的方法 一.执行fdisk -l查看linux设备,我的 ...
- oracle查询语句2【转载】
本文使用的实例表结构与表的数据如下: scott.emp员工表结构如下: SQL> DESC SCOTT.EMP; Name Type Nullable Defaul ...
- javascript:void到底是个什么?
一般都是用作 实现 如下功能,当点击一个超链接的时候,不想出发超链接自带的动作,而触发自定义的js方法,一般与onclick 一起出现.如果不写void(0)点击超链接时候虽然调用js方法,但是也会出 ...
- How to disable Eclipse splash
Run eclipse with the -nosplash option.
- Eclipse中svn图标不显示
在菜单栏中:windows ->preferences->General->Appearance->Lable Decorations 勾选其中的 SVN 项,最后应用确认之后 ...