反射替换工厂模式switch--case

原代码:

public static IMonitorDeviceManager GetDeviceManager(string flag)
        {
            try
            {
                switch (flag)
                {
                    case "1":return new DBManager();break;
                    default:return null ;break;
                }
               
        }

修改后:

  public static IMonitorDeviceManager GetDeviceManager(string flag)
        {
            try
            {
               
                string dllfile = ConfigurationManager.AppSettings["dllfile"];//dll名称
                string dllclass = ConfigurationManager.AppSettings["dllclass"];//命名空间+类名
                Assembly assembly = Assembly.LoadFrom(dllfile);
                IMonitorDeviceManager obj = assembly.CreateInstance(dllclass) as IMonitorDeviceManager;
                return obj;
            }
            catch (Exception e)
            {
                Log.WriteError(e.Message);
                return null;
            }
        }

config

 <add key="dllfile" value="Camera.DB.dll"/>
<add key="dllclass" value="Camera.DCZY.DBManager"/>

 

相关文章:

  • 2021-04-21
  • 2022-01-20
  • 2021-09-14
  • 2022-01-28
  • 2021-08-07
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-11-20
  • 2021-10-20
  • 2021-11-03
  • 2022-02-05
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案