继承父类进行二次开发

关于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[0is 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;
}


 

父类代码:

using System;

namespace TwiceDevelep
{
 /// <summary>
 /// Parent 的摘要说明。
 /// </summary>
 public class Parent
 {
  public Parent()
  {
   //
   // TODO: 在此处添加构造函数逻辑
   //
  }
  private string _Text="Parent";
  public virtual string Text
  {
   get
   {
    return _Text;
   }
   set
   {
    _Text = value;
   }
  }
 }
}

子类代码:

 using System;

namespace TwiceDevelep
{
 /// <summary>
 /// Child 的摘要说明。
 /// </summary>
 public class Child:Parent
 {
  public Child()
  {
   //
   // TODO: 在此处添加构造函数逻辑
   //
  }
  
  private string _Text="Child";
  public override string Text
  {
   get
   {
    return _Text;
   }
   set
   {
    _Text = value;
   }
  }
 }
}

 

调用如下:

private void Page_Load(object sender, System.EventArgs e)
  {
   string assemble = "TwiceDevelep.Child,TwiceDevelep";
   Type type = Type.GetType(assemble);
   Parent parent = (Parent)Activator.CreateInstance(type);
   string text = parent.Text;
   Response.Write(text);
  }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值