关于Assembly.CreateInstance()与Activator.CreateInstance()方法
于Assembly.CreateInstance()与Activator.CreateInstance()方法
动 态创建类对象,大多是Activator.CreateInstance()和Activator.CreateInstance<T>() 方法,非常好用,一般都用了Assembly.Load("AssemblyName").CreateInstance ("ClassName");的方法,研究一下这两者到底有什么区别,在msdn里,查到了两个方法的介绍:
Assembly.CreateInstance 方法 (String)
使用区分大小写的搜索,从此程序集中查找指定的类型,然后使用系统激活器创建它的实例。
Activator.CreateInstance 方法 (Type)
使用与指定参数匹配程度最高的构造函数来创建指定类型的实例。
看完以后,忽然觉得说了跟没说一样。不知道是我文字理解能力有问题,还是它表达有问题。
于是,没办法,只好用Reflector看看源代码了。
System.Reflection.Assembly位于mscorlib.dll里,CreateInstance()方法的源码是这样的
System.Activator也位于mscorlib.dll里,CreateInstance()方法的
public object CreateInstance(string typeName, bool ignoreCase, BindingFlags bindingAttr, Binder binder, object[] args, CultureInfo culture, object[]
activationAttributes)
{
Type type1 = this.GetTypeInternal(typeName, false, ignoreCase, false);
if (type1 == null)
{
return null;
}
//注意一下这一句,晕。。。。这里居然调用了Activator.CreateInstance方法
return Activator.CreateInstance(type1, bindingAttr, binder, args, culture, activationAttributes);
}
源码如下
public
static object CreateInstance(Type type, BindingFlags bindingAttr,
Binder binder, object[] args, CultureInfo culture, object[]
activationAttributes)
{
object obj1;
if (type == null)
{
throw new ArgumentNullException("type");
}
if (type is TypeBuilder)
{
throw new NotSupportedException(Environment.GetResourceString("NotSupported_CreateInstanceWithTypeBuilder"));
}
if ((bindingAttr & ((BindingFlags) 0xff)) == BindingFlags.Default)
{
bindingAttr |= BindingFlags.CreateInstance | BindingFlags.Public | BindingFlags.Instance;
}
if ((activationAttributes != null) && (activationAttributes.Length > 0))
{
if (!type.IsMarshalByRef)
{
throw new NotSupportedException(Environment.GetResourceString("NotSupported_ActivAttrOnNonMBR"));
}
if (!type.IsContextful && ((activationAttributes.Length > 1)
|| !(activationAttributes[0] is UrlAttribute)))
{
throw new NotSupportedException(Environment.GetResourceString("NotSupported_NonUrlAttrOnMBR"));
}
}
try
{
obj1 = ((RuntimeType)
type.UnderlyingSystemType).CreateInstanceImpl(bindingAttr, binder, args,
culture, activationAttributes);
}
catch (InvalidCastException)
{
throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"), "type");
}
return obj1;
}
一个facade模式,就解决了问题,而System.Activator.CreateInstance()方法的代码,下次再研究,先把facade补习一下,呵呵。
===================================================================================
DALFactory默认是每一层封装到一个程序集(独立项目)组件里。通过反射机制创建对象实例。
//从程序集创建对象实例
string path = System.Configuration.ConfigurationSettings.AppSettings["DAL"];//数据层的程序集名称
return (IDbObject)Assembly.Load(path).CreateInstance(path+".DbObject");
如果你的数据层不是单独的程序集,可以采用如下方法加载:
//使用与指定参数匹配程度最高的构造函数来创建指定类型的实例
string path = System.Configuration.ConfigurationSettings.AppSettings["DAL"];
string TypeName=path+".DbObject"
Type bjType = Type.GetType(TypeName,true);
return (IDbObject)Activator.CreateInstance(objType);
http://www.cnblogs.com/xiaotao823/archive/2008/05/02/1179119.html
关于Assembly.CreateInstance()与Activator.CreateInstance()方法的更多相关文章
- Assembly.CreateInstance和Activator.CreateInstance
本来是在设计模式中的工厂方法,在实现抽象工厂时,用到了一直都不熟悉的反射. namespace Factory { public abstract class Factory { public abs ...
- C# Activator.CreateInstance()方法使用
C#在类工厂中动态创建类的实例,所使用的方法为: 1. Activator.CreateInstance (Type) 2. Activator.CreateInstance (Type, Objec ...
- (转) C# Activator.CreateInstance()方法使用
C#在类工厂中动态创建类的实例,所使用的方法为: 1. Activator.CreateInstance (Type) 2. Activator.CreateInstance (Type, Objec ...
- C#中Activator.CreateInstance()方法用法分析
本文实例讲述了C#中Activator.CreateInstance()方法用法. Activator 类 包含特定的方法,用以在本地或从远程创建对象类型,或获取对现有远程对象的引用. C#在类工厂中 ...
- Activator.CreateInstance 方法 (Type) 的用法
转自:http://www.cnblogs.com/lmfeng/archive/2012/01/30/2331666.html Activator.CreateInstance 方法 (Type) ...
- C# Activator.CreateInstance()
C#在类工厂中动态创建类的实例,所使用的方法为: 1. Activator.CreateInstance (Type) 2. Activator.CreateInstance (Type, Objec ...
- 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 中,执行该操作的另一种方法 ...
- 注意Activator.CreateInstance两个重载方法的性能
今天扩展一个Type的扩展方法New: public static object New(this Type type, params object[] args) { Guard.ArgumentN ...
- cSharp:use Activator.CreateInstance with an Interface?
///<summary> ///数据访问工厂 ///生成時間2015-2-13 10:54:34 ///塗聚文(Geovin Du) /// (利用工厂模式+反射机制+缓存机制,实现动态创 ...
随机推荐
- 分析Linux内核中进程的调度(时间片轮转)-《Linux内核分析》Week2作业
1.环境的搭建: 这个可以参考孟宁老师的github:mykernel,这里不再进行赘述.主要是就是下载Linux3.9的代码,然后安装孟宁老师编写的patch,最后进行编译. 2.代码的解读 课上的 ...
- eclipse code templates 设置(eclipse注释模版配置)
文件(Files)注释标签:/** * @Title: ${file_name} * @Package ${package_name} * @Description: ${todo} * Copyri ...
- 初学Linux
一直觉得Linux敲命令很蛋疼,今天开始学习一下吧,主要以练习(想到啥就查啥)命令和练习在Linux中编程(Python)为主吧. 不记得什么时候安装的Ubuntu 12.04.3 LTS虚拟机,连密 ...
- Android gradle问题解决: This app has been built with an incorrect configuration. Please configure your build for VectorDrawableCompat
1. 问题描述: Android Studio在运行模拟器某些机型或者真机某些机型的时候发生闪退. 错误如下: Java.lang.RuntimeException: Unable to start ...
- C#怎样去掉对于用Splict分隔的数组中的空值?
string[] arrayUserId = userIds.Split(new char[] { ',' },StringSplitOptions.RemoveEmptyEntries); 可以去掉 ...
- WPF快速精通版
命名空间: xmlns:sys="clr-namespace:System;assembly=mscorlib" xmlns:local="clr-namespace:U ...
- 打包时Xcode报:此证书的签发者无效Missing iOS Distribution signing identity
问题描述 今天准备打包上传AppStore,结果Xcode报以下错误:Missing iOS Distribution signing identity for XXXXXX 查看证书后发现,Deve ...
- ch6 影响 MySQLServer 性能的相关因素
第6章影响 MySQLServer 性能的相关因素 前言: 大部分人都一致认为一个数据库应用系统(这里的数据库应用系统概指所有使用数据库的系统)的性能瓶颈最容易出现在数据的操作方面,而数据库应用系统的 ...
- Oracle-ARCGIS-SDE 数据整合遇到的问题
一. 近日在做全文检索,基础采用oracle text,版本是10g,做好管理页面后,有功能是删除索引,就是生成drop index的语句.没有想到这个全文检索的index这么直接弄还不行,经过这样删 ...
- c#列举和迭代器
列举 - Enumeration 迭代器是一个值序列(集合)上的一个只读且只向前移动的游标.迭代器要么实现了IEnumerator接口,要么实现了IEnumerator<T>接口. 从技术 ...