博客
关于我
C# Activator.CreateInstance()方法使用
阅读量:394 次
发布时间:2019-03-05

本文共 939 字,大约阅读时间需要 3 分钟。

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/daodaowolf/article/details/8977234 )

你可能感兴趣的文章
WCF学习之旅—第三个示例之一(二十七)
查看>>
java ThreadPoolExecutor初探
查看>>
快速指数算法
查看>>
python去除字符串中的特殊字符(爬虫存储数据时会遇到不能作为文件名的字符串)
查看>>
SpringCloud微服务(03):Hystrix组件,实现服务熔断
查看>>
Spring 框架基础(01):核心组件总结,基础环境搭建
查看>>
Cassandra数据建模
查看>>
Internet Explorer 10 专题上线
查看>>
云计算之路-阿里云上:0:25~0:40网络存储故障造成网站不能正常访问
查看>>
网站故障公告1:使用阿里云RDS之后一个让人欲哭无泪的下午
查看>>
上周热点回顾(6.3-6.9)
查看>>
上周热点回顾(8.12-8.18)
查看>>
【故障公告】升级阿里云 RDS SQL Server 实例故障经过
查看>>
蹒跚来迟:新版博客后台上线公测
查看>>
[网站公告]11月26日00:00-04:00阿里云RDS升级
查看>>
[网站公告]又拍云API故障造成图片无法上传(已恢复)
查看>>
云计算之路-阿里云上:“黑色30秒”走了,“黑色1秒”来了,真相也许大白了
查看>>
上周热点回顾(6.9-6.15)
查看>>
上周热点回顾(10.20-10.26)
查看>>
上周热点回顾(2.16-2.22)
查看>>