例如泛型类通常的实例方法是
GridViewOperate<VersionInfo> obj= new GridViewOperate<VersionInfo>()其中VersionInfo我希望可以通过反射自定义进去,从而得到实例

解决方案 »

  1.   


    Assembly m_Assembly = Assembly.LoadFrom(str);//载入装配件 //返回指定dll下的类
            static object ActivetorMethodOutObject(string clsName,Assembly m_Assembly)
            {
                object _result = null;
                try
                {
                    string _tmpCls = "Platform." + clsName;
                    Type _CurrentType = m_Assembly.GetType(_tmpCls,false);
                    _result = Activator.CreateInstance(_CurrentType);
                }
                catch { }
                return _result;
            }
      

  2.   

     //返回指定dll下的类
            static object ActivetorMethodOutObject(string clsName,Assembly m_Assembly)
            {
                object _result = null;
                try
                {
                    string _tmpCls = "Platform." + clsName;//Platform是dll文件的名字(换成你的VersionInfo所在的dll文件即可)
                    Type _CurrentType = m_Assembly.GetType(_tmpCls,false);
                    _result = Activator.CreateInstance(_CurrentType);
                }
                catch { }
                return _result;
            }
      

  3.   


    Platform是指泛型类的名称?clsName指泛型参数类的名称吗?
    就是对应的GridViewOperate和VersionInfo???
      

  4.   


    我希望得到的不是一个VersionInfo实例,而是一个GridViewOperate <VersionInfo>实例
      

  5.   

    你不是使用反射得到VersionInfo类吗?你把上面的Platform换成你的实体类(VersionInfo)所在的dll文件名,clsName即是类名,也就是VersionInfo。
      

  6.   

    ="Platform."+ clsName;
    //命名空间+类名
      

  7.   

    我想要的泛型类的反射方法GridViewOperate <VersionInfo>
    如果按照lovelj2012兄弟提供的方法,怎么把versionInfo这个泛型参数传递进去呢?
      

  8.   


    /// <summary>
            /// 返回指定dll下的类
            /// </summary>
            /// <param name="clsName">泛型类名(GridViewOperate)</param>
            /// <param name="entityName">实体类名(VersionInfo)</param>
            /// <param name="m_Assembly">泛型类</param>
            /// <returns></returns>
            static object ActivetorMethodOutObject(string clsName,string entityName,Assembly m_Assembly)
            {
                object _result = null;
                try
                {
                    string _tmpCls = "Platform." + clsName+"<"+entityName+">";//Platform是dll文件的名字(换成你的VersionInfo所在的dll文件即可)
                    Type _CurrentType = m_Assembly.GetType(_tmpCls, false);
                    _result = Activator.CreateInstance(_CurrentType);
                }
                catch { }
                return _result;
            }
      

  9.   

    先取得泛型类行的Type实例,调用MakeGenericType方法,传入泛型参数。
    然后像普通类的反射创建实例一样,创建实例