关于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()方法的

None.gif public   object  CreateInstance( string  typeName,  bool  ignoreCase, BindingFlags bindingAttr, Binder binder,  object [] args, CultureInfo culture,  object [] activationAttributes)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif      Type type1 
= this.GetTypeInternal(typeName, false, ignoreCase, false);
InBlock.gif      
if (type1 == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif            
return null;
ExpandedSubBlockEnd.gif      }

InBlock.gif      
//注意一下这一句,晕。。。。这里居然调用了Activator.CreateInstance方法
InBlock.gif
      return Activator.CreateInstance(type1, bindingAttr, binder, args, culture, activationAttributes);
ExpandedBlockEnd.gif}

源码如下

None.gif public   static   object  CreateInstance(Type type, BindingFlags bindingAttr, Binder binder,  object [] args, CultureInfo culture,  object [] activationAttributes)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif      
object obj1;
InBlock.gif      
if (type == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif            
throw new ArgumentNullException("type");
ExpandedSubBlockEnd.gif      }

InBlock.gif      
if (type is TypeBuilder)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif            
throw new NotSupportedException(Environment.GetResourceString("NotSupported_CreateInstanceWithTypeBuilder"));
ExpandedSubBlockEnd.gif      }

InBlock.gif      
if ((bindingAttr & ((BindingFlags) 0xff)) == BindingFlags.Default)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif            bindingAttr 
|= BindingFlags.CreateInstance | BindingFlags.Public | BindingFlags.Instance;
ExpandedSubBlockEnd.gif      }

InBlock.gif      
if ((activationAttributes != null&& (activationAttributes.Length > 0))
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif            
if (!type.IsMarshalByRef)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                  
throw new NotSupportedException(Environment.GetResourceString("NotSupported_ActivAttrOnNonMBR"));
ExpandedSubBlockEnd.gif            }

InBlock.gif            
if (!type.IsContextful && ((activationAttributes.Length > 1|| !(activationAttributes[0is UrlAttribute)))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                  
throw new NotSupportedException(Environment.GetResourceString("NotSupported_NonUrlAttrOnMBR"));
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif      }

InBlock.gif      
try
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif            obj1 
= ((RuntimeType) type.UnderlyingSystemType).CreateInstanceImpl(bindingAttr, binder, args, culture, activationAttributes);
ExpandedSubBlockEnd.gif      }

InBlock.gif      
catch (InvalidCastException)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif            
throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"), "type");
ExpandedSubBlockEnd.gif      }

InBlock.gif      
return obj1;
ExpandedBlockEnd.gif}

None.gif
None.gif

 
一个facade模式,就解决了问题,而System.Activator.CreateInstance()方法的代码,下次再研究,先把facade补习一下,呵呵。

转载于:https://www.cnblogs.com/kchen/archive/2006/11/02/548390.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值