• 关于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);

  • 相关阅读:
    必知必会 | Android 性能优化的方面方面都在这儿
    上周热点回顾(1.19-1.25)团队
    上周热点回顾(1.12-1.18)团队
    如何在博客园的markdown编辑器中输入数学公式团队
    上周热点回顾(1.5-1.11)团队
    上周热点回顾(12.29-1.4)团队
    上周热点回顾(12.22-12.28)团队
    云计算之路-阿里云上-寒流来袭:2014年12月23日21:45-23:15网站故障团队
    上周热点回顾(12.15-12.21)团队
    【活动】加班一整年了,程序员们,你们还好吗?团队
  • 原文地址:https://www.cnblogs.com/Veakey/p/3524174.html
Copyright © 2020-2023  润新知