工厂示例
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Configuration; using System.Reflection; namespace Common.DALFactory { /// <summary> /// 数据层对象实例创建 /// </summary> public partial class DbFactory { /// <summary> /// 做缓存 /// </summary> private static Dictionary<string, object> dic = new Dictionary<string, object>(); #region 程序集配置 -static readonly string DALAssembly /// <summary> /// 程序集配置 /// </summary> private static readonly string DALAssembly = ConfigurationManager.AppSettings["dalAssembly"]; #endregion #region 命名空间配置 -static readonly string DALNameSpace /// <summary> /// 命名空间配置 /// </summary> private static readonly string DALNameSpace = ConfigurationManager.AppSettings["dalNameSpace"]; #endregion #region 创建对象实例 -static object CreateInstance(string fullName, string assemblyName) /// <summary> /// 创建对象实例 /// </summary> /// <param name="fullName">全名称 命名空间.类名</param> /// <param name="assemblyName">程序集名称</param> /// <returns></returns> private static object CreateInstance(string fullName, string assemblyName) { string key = string.Format("{0}+{1}", assemblyName, fullName); if (!dic.ContainsKey(key)) { Assembly assembly = Assembly.Load(assemblyName); // 加载程序集 object obj = assembly.CreateInstance(fullName); dic.Add(key, obj); } return dic[key]; } #endregion } }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步