//定义了一个接口(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这样,该如何实现?
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这样,该如何实现?
解决方案 »
- 请问lucene.net的索引文件怎么和数据库中的数据同步?
- 高分! 如何用 DirectoryEntry 获取 别的计算机的上 某个用户组下的 全部用户
- <怎么打WINFORM中实现 点击按钮 弹出网页 来显示我TEXTBOX的HTML>
- 获取sqlserver服务器名称,地址,版本信息.
- 求助
- 请教.webform下menu的用法
- DataGridView
- 请问:为什么调用COM中的事件会报错?
- 高分求解,DataTable更新到表记录中的问题!!!
- 无法显示windows应用程序的FORM???
- 顶者有分!C#怎么获取windows系统登陆信息
- 求winform上外观好看立体感较强的自制combox
{
//...
return (jm.Interface.IDBOperator<T>)objType;
}
好像不对啊,string ClassNamespace = AssemblyPath + "."+classname;
//这里的classname从哪来,ClassNamespace最终应该是jm.BLL.Customer才对啊
{
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);
}
}
typeof(List<>).MakeGenericType(type)Type.MakeGenericType 方法(System)替代由当前泛型类型定义的类型参数组成的类型数组的元素,并返回表示结果构造类型
public partial class Customer:IDBOperator<jm.Model.Customer>
改成public partial class CustomerOP : IDBOperator<Customer>这样?
那<Customer>里的这个Customer就表示jm.BLL.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>();