【问题标题】:how do I find by key in ConfigurationElementCollection如何在 ConfigurationElementCollection 中按键查找
【发布时间】:2018-01-19 07:39:38
【问题描述】:

我有一个继承自 ConfigurationElementCollection 的类。我想添加一个名为 FindByKey 的函数,它返回带有特定键的 ConfigurationElement。键将是一个字符串,我知道 ConfigurationElement 将键存储为一个对象。

有没有人有任何简单的代码来检测键中的元素?我确信一个简单的答案是基于了解 ConfigurationElementCollection 属性并在字符串和对象之间快速转换。

谢谢 J

【问题讨论】:

    标签: c#


    【解决方案1】:

    我今天完成了一个示例,因为我正在为 ConfigurationElementCollection 类编写扩展。它可能不是非常理想,但它具有与您询问 ContainsKey(key) 的方法类似的方法。

    public class ConfigurationElementCollectionExtension : ConfigurationElementCollection, IConfigurationElementCollectionExtension
        {
            protected override ConfigurationElement CreateNewElement()
            {
                throw new NotImplementedException();
            }
    
            protected override object GetElementKey(ConfigurationElement element)
            {
                throw new NotImplementedException();
            }
    
            bool IConfigurationElementCollectionExtension.ContainsKey<T>(T key)
            {
                bool returnValue = false;
    
                object[] keys = base.BaseGetAllKeys();
    
                string keyValue = key.ToString();
                int upperBound = keys.Length;
                List<string> items = new List<string>();
    
                for (int i = 0; i < upperBound; i++)
                {
                    items.Add(keys[i].ToString());
                }
    
                int index = items.IndexOf(keyValue);
                if (index >= 0)
                {
                    returnValue = true;
                }
    
    
                return returnValue;
            }
        }
    

    【讨论】:

      【解决方案2】:

      开始吧....有很多示例。

      public class MYAppConfigTool {
      
          private Configuration _cfg;
      
          public Configuration GetConfig() {
              if (_cfg == null) {
                  if (HostingEnvironment.IsHosted) // running inside asp.net ?
                  { //yes so read web.config at hosting virtual path level
                      _cfg = WebConfigurationManager.OpenWebConfiguration(HostingEnvironment.ApplicationVirtualPath);
                  }
                  else { //no, se we are testing or running exe version admin tool for example, look for an APP.CONFIG file
                      //var x = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
                      _cfg = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                  }
              }
              return _cfg;
          }
      
          public AppSettingsSection GetAppSettings() {
              var cfg = GetConfig();
              return cfg == null ? null 
                                 : cfg.AppSettings;
          }
      
          public string GetAppSetting(string key) {
              // null ref here means key missing.   DUMP is good.   ADD the missing key !!!!
              var cfg = GetConfig();
              if (cfg == null) return null;
              var appSettings = cfg.AppSettings;
              return appSettings == null ? null 
                                         : GetConfig().AppSettings.Settings[key].Value;
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2014-12-24
        • 2016-05-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-12-19
        • 1970-01-01
        相关资源
        最近更新 更多