惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

GbyAI
GbyAI
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Blog — PlanetScale
Blog — PlanetScale
PCI Perspectives
PCI Perspectives
K
Kaspersky official blog
T
Tenable Blog
Help Net Security
Help Net Security
Vercel News
Vercel News
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
F
Fortinet All Blogs
罗磊的独立博客
P
Palo Alto Networks Blog
爱范儿
爱范儿
Google DeepMind News
Google DeepMind News
T
Threat Research - Cisco Blogs
Security Archives - TechRepublic
Security Archives - TechRepublic
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
人人都是产品经理
人人都是产品经理
L
LangChain Blog
Recent Announcements
Recent Announcements
有赞技术团队
有赞技术团队
博客园_首页
D
Darknet – Hacking Tools, Hacker News & Cyber Security
H
Help Net Security
S
Secure Thoughts
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Project Zero
Project Zero
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
V
V2EX
Last Week in AI
Last Week in AI
H
Heimdal Security Blog
U
Unit 42
Y
Y Combinator Blog
The GitHub Blog
The GitHub Blog
SecWiki News
SecWiki News
量子位
博客园 - 【当耐特】
Martin Fowler
Martin Fowler
NISL@THU
NISL@THU
S
Securelist
P
Proofpoint News Feed
宝玉的分享
宝玉的分享
T
Tailwind CSS Blog
云风的 BLOG
云风的 BLOG
I
Intezer
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
博客园 - Franky
Cisco Talos Blog
Cisco Talos Blog
小众软件
小众软件
C
CXSECURITY Database RSS Feed - CXSecurity.com

博客园 - Forrest Gump

关于LINQ中数据库连接字符串的问题 项目开发经验-ASP.NET项目开发中的异常处理 关于模态窗口(showModalDialog)的专题【收藏】 C#面试题 C# 将数据导出到Excel汇总 PowerDesigner概念设计模型(CDM)中的3种实体关系 C#基础概念二十五问 Microsoft .NET Pet Shop 4:将 ASP.NET 1.1 应用程序迁移到 2.0 用Inno Setup制作WEB程序安装包 冒泡法数组排序与 System.Array.Sort()排序性能比较 堆排序 (Heap sort) 合并排序法(Merge Sort) 希尔排序法 quick sort 关于switch的小技巧 C#中一些很基础但有经常导致错误的一些概念 Enterprise Library Step By Step系列(十六):使用AppSetting Application Block Enterprise Library Step By Step系列(十五):配置应用程序块——设计篇 创建基于消息队列(MSMQ)的异步日志
关于Assembly.CreateInstance()与Activator.CreateInstance()方法
Forrest Gump · 2008-05-02 · via 博客园 - Forrest Gump

关于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 objType = Type.GetType(TypeName,true);
return (IDbObject)Activator.CreateInstance(objType);