//定义了一个接口(IDBOperator.cs),
namespace jm.Interface
{
    public interface IDBOperator<T>
    {
         long Add(T model);
    }
}
//继承这个接口的类(Customer.cs),
namespace jm.BLL
{
     public partial class Customer:IDBOperator<jm.Model.Customer>
    {
          public long Add(jm.Model.Customer model)
{
return dal.Add(model);
}
     }
//页面上创建实例(Customer.aspx.cs)
namespace UI.Public
{
    public partial class searchCustomer : System.Web.UI.Page
    {
        private readonly IDBOperator<jm.Model.Customer> icustomer = OperatorFactory.CreateDBOperator("customer");
     }
}//通过反射创建(OperatorFactory.cs)
namespace jm.Factory
{
    public class OperatorFactory
    {
        private static readonly string AssemblyPath = ConfigurationManager.AppSettings["BLL"];
        //通过反射生成接口实例
        public static object CreateObject(string AssemblyPath, string ClassNamespace)
        { 
            object objType = null;
            if (objType == null)
            {
                try
                {
                    //objType = Assembly.Load(AssemblyPath).CreateInstance(ClassNamespace);//反射创建
                    objType = Assembly.LoadFile(System.Web.HttpContext.Current.Server.MapPath("~/bin/BLL.dll")).CreateInstance(ClassNamespace);
                }
                catch
                { }
            }
            return objType;
        }
        //如果非泛型的话就是这样创建的,但是加了这个<T>以后应该如何创建这个接口的实例?
        public static jm.Interface.IDBOperator<T> CreateDBOperator(string classname)
        {
            string ClassNamespace = AssemblyPath + "."+classname;
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (jm.Interface.IDBOperator<T>)objType;
        }    }
}
我的本意就是想达到中间的这个<T>是可以更换的,可以是jm.Model.Customer,换成另一个页面也可以是jm.Model.Member这样,该如何实现?

解决方案 »

  1.   

    CreateDBOperator 也应该用泛型而classname就可以不要了。public static jm.Interface.IDBOperator<T> CreateDBOperator<T>()
    {
        //...
        return (jm.Interface.IDBOperator<T>)objType;
    }
      

  2.   


    好像不对啊,string ClassNamespace = AssemblyPath + "."+classname;
    //这里的classname从哪来,ClassNamespace最终应该是jm.BLL.Customer才对啊
      

  3.   

    形如:public interface IDBOperator<T>
    {
        long Add(T model);
    }public partial class CustomerOP : IDBOperator<Customer>
    {
        public long Add(Customer model)
        {
            return 0;
        }
    }public class Customer
    {  
    }public class Factory
    {
        public static IDBOperator<T> Create<T>(string classname)
        {
            Type type = Type.GetType(classname);
            return (IDBOperator<T>)Activator.CreateInstance(type);
        }
    }
      

  4.   

    Type type = Type.GetType(classname);
    typeof(List<>).MakeGenericType(type)Type.MakeGenericType 方法(System)替代由当前泛型类型定义的类型参数组成的类型数组的元素,并返回表示结果构造类型
      

  5.   


    public partial class Customer:IDBOperator<jm.Model.Customer>
    改成public partial class CustomerOP : IDBOperator<Customer>这样?
    那<Customer>里的这个Customer就表示jm.BLL.Customer了
      

  6.   

    public partial class Customer:IDBOperator<jm.Model.Customer>
    改成public partial class CustomerOP : IDBOperator<Customer>这样?
    那<Customer>里的这个Customer就表示jm.BLL.Customer了--------------------不用改啊,我那就是个例子啊。可以这样:
    private readonly IDBOperator<jm.Model.Customer> icustomer = OperatorFactory.CreateDBOperator<jm.Model.Customer>("jm.BLL.Customer");也可以这样
    private readonly IDBOperator<jm.Model.Customer> icustomer = OperatorFactory.CreateDBOperator<jm.BLL.Customer, jm.Model.Customer>();