zoukankan      html  css  js  c++  java
  • 关于反射中Assembly.Load("程序集").CreateInstance("命名空间.类")与Activator.CreateInstance()方法

    动态创建类对象,大多是Activator.CreateInstance()和Activator.CreateInstance<T>()方法,有的用了Assembly.Load("AssemblyName").CreateInstance ("ClassName");的方法,研究一下这两者到底有什么区别

    System.Reflection.Assembly位于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);
    }

    System.Activator也位于mscorlib.dll里,CreateInstance()方法的
    源码如下

    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;
    }
    ===================================================================================

    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);

  • 相关阅读:
    在IE和Firfox获取keycode
    using global variable in android extends application
    using Broadcast Receivers to listen outgoing call in android note
    help me!virtual keyboard issue
    using iscroll.js and iscroll jquery plugin in android webview to scroll div and ajax load data.
    javascript:jquery.history.js使用方法
    【CSS核心概念】弹性盒子布局
    【Canvas学习笔记】基础篇(二)
    【JS核心概念】数据类型以及判断方法
    【问题记录】ElementUI上传组件使用beforeupload钩子校验失败时的问题处理
  • 原文地址:https://www.cnblogs.com/dudu837/p/1561249.html
Copyright © 2011-2022 走看看