zoukankan      html  css  js  c++  java
  • 反射应用

    反射替换工厂模式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"/>
    萌橙 你瞅啥?
  • 相关阅读:
    LeetCode
    LeetCode
    LeetCode
    LeetCode
    LeetCode
    LeetCode
    LeetCode
    LeetCode
    LeetCode
    LeetCode
  • 原文地址:https://www.cnblogs.com/daimaxuejia/p/11692706.html
Copyright © 2011-2022 走看看