关于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) /// (利用工厂模式+反射机制+缓存机制,实现动态创 ...
随机推荐
- jQuery自动分页打印表格(HTMLtable),可以强制换页
最近做项目的时候需要做批量打印订单,一个订单可能在最后一页是的内容是不足一页的,这时候下一个订单需要下一页打印,这样就需要强制换页.在下一页再打印下一个订单 部分代码: 部分重要的css是分页的换页的 ...
- 通过swap代码分析C语言指针在汇编级别的实现
我们先用C语言写一个交换两个数的代码: void swap(int *a, int *b){ int temp = *a; *a = *b; *b = temp; } int main(void) { ...
- 关于TCP的粘包
2014年与宗宗一起去厦门测试软件接口的时候,与上级系统基于TCP方式通讯,数据量大时,经常通讯失败,检查日志发现是上级系统应该多次返回的数据一次性接收到了. 上网搜索了一下,才了解到TCP粘包的问题 ...
- 冒泡算法应用(坐标Y值降序X值升序)
今天有个客户需求是有一坐标数组,希望按Y值降序X值升序排列,我临时写了个算法.先写个坐标类: class XYZ { public XYZ() { } public XYZ(doubl ...
- 关于《精通移动App测试实战:技术、工具和案例》图书勘误信息
首先,对由于我们工作的疏忽向<精通移动App测试实战:技术.工具和案例>读者朋友们表示歉意,同时已将这些问题反馈给了出版社编辑同志,再版时将会统一修正: 其次,勘误信息请参看附件pdf文档 ...
- c#之第四课
数组: , , , , , , -, -, }; foreach (int i in numbers) { System.Console.WriteLine(i); }
- iframeWin For Easy UI. 为 Easy UI 扩展的支持IFrame插件
iframeWin For Easy UI. 为 Easy UI 扩展的支持IFrame插件 在一个项目中用了Easy UI,但是发现里面的 Dialog .Window.Messager 弹窗都不支 ...
- 学习Swift,一定不能错过的10大开源项目!
如果你是位iOS开发者,或者你正想进入该行业,那么Swift为你提供了一个绝佳的机会.Swift的设计非常优雅,较Obj-C更易于学习,当然也非常强大. 为了指导开发者使用Swift进行开发,苹果发布 ...
- android CheckBox的运用
CheckBox定义一个同意协议的按钮,只要同意button才可以点击 XML代码 <CheckBox android:id="@+id/checkbox1" android ...
- 分享自己写的一个小工具RGB转十六进制(高手勿喷)
由于工作经常美工给的颜色是rgb,而我们网页里面是16进制.网上也有很多类型的工具.不过似乎都用浏览器打开.没网就不爽了 实现也很简单.代码已经共享了 http://git.oschina.net/w ...