//第一步创建一个AbstractFactory类,在这个类里面定义两个静态属性:AssemblyPath ,NameSpace //第二步在工厂类里面写一个静态方法:public static IUserInfoDal CreateUserInfoDal(){}//第三步再写一个方法:private static object CreateInstance(string fullName)通过Assembly反射来创建实例//第四步在web.config文件下配置这两个静态属性:AssemblyPath ,NameSpace
public class AbstractFactory { private static readonly string AssemblyPath = ConfigurationManager.AppSettings["AssemblyPath"]; private static readonly string NameSpace = ConfigurationManager.AppSettings["NameSpace"]; public static IUserInfoDal CreateUserInfoDal() { string fullName = NameSpace + ".UserInfoDal"; return CreateInstance(fullName) as IUserInfoDal; } private static object CreateInstance(string fullName) { Assembly assembly = Assembly.Load(AssemblyPath); return assembly.CreateInstance(fullName); } }
时间: 2024-10-13 00:29:14