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) /// (利用工厂模式+反射机制+缓存机制,实现动态创 ...
随机推荐
- Ubuntu下su:authentication failure的解决办法
$ su - rootPassword: su: Authentication failureSorry. 这时候输入 $ sudo passwd rootEnter new UNIX passwor ...
- SysErrorMessage 函数和系统错误信息表
在看 API 文档时, 我们经常见到 GetLastError; 它可以返回操作后系统给的提示.但 GetLastError 返回的只是一个信息代码, 如何返回对应的具体信息呢?FormatMessa ...
- Zlib压缩算法在Java与Delphi间交互实现(压缩XML交互)
一个典型应用中,使用delphi作为客户端,J2EE服务端,两者之间用XML作为数据交换,为了提高效率,对XML数据进行压缩,为此需要找到一种压缩/解压算法能够两个平台之间交互处理,使用ZLIB算法就 ...
- Git标签管理
一般我们发布一个新版本到线上服务器时都会在版本库中打一个标签,这时就确定了某个版本将发布到线上.我们可以随时可以查看这个打标签的版本,也就 是说标签其实呢,就是版本库中一个快照.简单说标签就是指向某个 ...
- 关于sql语句in的使用注意规则( 转)
select * from tuser where userno not in(select userno from filter_barcode) 上面这条语句子查询里,userno 并不存在fil ...
- 【CF】244C Checkposts
题目需要求啥很明确了.主要思想是先计算机联通块,然后每个块内找到一个最小值(以及该值的次数).最小值和结果1,次数乘积为结果2.联通块tarjan可解. /* 427C */ #include < ...
- 【HDOJ】3367 Pseudoforest
并查集. #include <cstdio> #include <cstring> #include <cstdlib> #define MAXN 10005 #d ...
- crontab(linux下定时执行任务命令)
在linux在可以通过在脚本里(列如sh)写如日常需要进行的操作,然后通过crontab定时运行脚本. Linux下的任务调度分为两类,系统任务调度和用户任务调度. 系统任务调度:系统周期性所要执行的 ...
- 10个经典的 Java main 方法面试题
以下是笔者认为比较经典的关于Java main方法的面试题,与其说是Java面试题,其实也是Java的一些最基础知识问题,分享给大家,如有错误,请指出. 1.不用main方法如何定义一个类? 不行,没 ...
- java 判断是不是检查性异常
/** * Return whether the given throwable is a checked exception: * that is, neither a RuntimeExcepti ...