【问题标题】:Where can I find some open source code to read/write configuration files?我在哪里可以找到一些开源代码来读/写配置文件?
【发布时间】:2011-07-15 17:55:48
【问题描述】:

我不喜欢 .NET Framework 中的标准配置机制。 (ConfgurationManager、ConfigurationSection 等)。

我想要更简单的功能来管理我的应用程序配置文件。

例如,我想在我的应用程序文件夹中创建一个名为“设置”的文件夹。有几个名为的配置文件。

请看一下:

Settings:
- smtp.config
- database.config
- something.config

假设“smtp.config”文件具有以下简单结构:

<smtp>
  <username>something</username>
  <hostname>something</hostname>
  ...
</smtp>

我想创建以下类:

public class MySmtpSettings : SomeBaseClassFromSomeConfigLibrary
{
   // May be some simple attributes here
   public string username;

   // May be some simple attributes here. For example:
   // Like XPath: [ConfigAttr("smtp\username")], or simply: ConfigAttr("username")
   public string hostname;
   ...

   // Only code containing properties declaration.
}

我想使用我的设置对象:

var settings = new MySmtpSettings("smtp.config");
var hostname = settings.Hostname;

我不想使用 ConfigurationSection 类。看起来很难。

你知道我在哪里可以找到一个可扩展的、简单的开源库吗?

UPD。

@jjrdk:感谢您的回答,但我通常使用 ConfigurationSection 类创建下一个代码:

public class MyConfigurationSection : ConfigurationSection
{

  [ConfigurationProperty("username")]
  public ConfigurationTextElement<string> Username
  {
     get { return (ConfigurationTextElement<string>)this["username"]; }
     set { this["username"] = value; }
  }

  // ...
{

public class ConfigurationTextElement<T> : ConfigurationElement
{
    private T _value;
    protected override void DeserializeElement(XmlReader reader, bool serializeCollectionKey)
    {
        _value = (T)reader.ReadElementContentAs(typeof(T), null);
    }
    public T Value
    {
        get { return _value; }
    }
}

看起来并不简单。也许我做了一些我不明白的事情?

【问题讨论】:

  • 我建议您重新考虑您对 ConfigurationSection 类的看法。这并不难,并且完全按照您的要求进行。将其视为一种学习体验。
  • 你不喜欢 .NET Framework 中的标准配置机制吗?
  • @RQDQ:描述类和文件之间的链接需要大量代码。

标签: c# xml configuration configuration-files


【解决方案1】:

如果您不想使用ConfigurationManager,您可能需要查看一些对象序列化程序。

您也可以使用众多 Dependency Injection (DI)/Inversion of Control (IoC) 框架之一...但如果您不喜欢 ConfigurationManager 背后的复杂性,我相信您会发现 DI 更不吸引人。

作为旁注,您甚至可以使用LINQ-to-XML 或.Net Framework 中的许多其他XML/文本阅读器之一。甚至good-oleINI文件。

【讨论】:

  • 我喜欢 DataContractSerializer 并已将它用于许多事情。但是,DataContractSerializer 的一个缺点(尤其是对于可能在记事本中手动编辑的配置文件)是 xml 元素必须按字母顺序排列。这样做是出于性能原因,但对于配置文件来说并不直观。
  • 非常正确。我也避免在配置文件中使用它,因为它不支持 XML 属性(没有自定义序列化程序。)
  • XmlSerializer 对我来说看起来很有趣。我现在就试试。
【解决方案2】:

我使用了一段我一开始不知道它来自哪里的代码,但是使用谷歌代码搜索我设法在这里找到它:http://m3dafort2d.googlecode.com/svn/trunk/projects/NCode/NCode/Configuration/

然后我像这样使用它:

public class AppSettings : DictionaryConvertible
{
    public AppSettings() { 
       // For unit testing
    }

    public AppSettings(ISettingsProvider settingsProvider) 
        : base(settingsProvider) {}        

    public string MyStringSetting { get; set; }
    public int MyIntSetting { get; set; }
    public bool MyBooleanSetting { get; set; }
}

这是在 IoC 场景中使用的设置,但您可能会找到一种以正常方式使用它的方法。

我通常只在 IoC 容器中配置 ISettingsProvider,然后我也配置 AppSettings 类。然后我可以将该类注入到任何需要设置的组件中。如果我想将配置设置类分组为逻辑集合,我还可以将它们拆分为多个类。

如果您想读取与 web.config 中 AppSettings 不同的位置的设置,只需实现另一个 ISettingsProvider。我曾经为 SQL Server 创建了一个。

【讨论】:

    【解决方案3】:

    编写 ConfigurationSectionHandler 并不难;当 ConfigurationManager 调用您的代码时,只需遍历作为参数提供给您的 XML 中的节点即可。您编写一种方法(可能还有一些帮助程序)来返回您想要用于配置的数据结构。对于简单的 XML,该方法也可以非常简单。请参阅http://msdn.microsoft.com/en-us/library/ms228056.aspx 上的示例。

    public class MyHandler : IConfigurationSectionHandler
    {
        #region IConfigurationSectionHandler Members
    
        public object Create(object parent, object configContext, XmlNode section)
        {
            var config = new MailConfiguration();
            foreach (XmlAttribute attribute in section.GetAttributes())
            {
                  switch (attribute.Name)
                  {
                       case "server":
                           config.Server = attribute.Value;
                           break;
                       ...
                  }
            }
    
            foreach (XmlNode node in section.ChildNodes)
            {
                  switch (node.Name)
                  {
                       case "server":
                           config.Server = node.Value;
                           break;
                       ...
                  }
            }
        }
    
        return config;
    
        #endregion
      }
    }
    

    用作

    var config = ConfigurationManager.GetSection("smtp") as MailConfiguration;
    

    【讨论】:

    • 是的,看起来不错。但是为什么 IConfigurationSectionHandler 在 .NET Framework 2.0 版中已被弃用? (在你给我的页面上有一个注释)
    猜你喜欢
    • 2011-08-31
    • 1970-01-01
    • 2019-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-12
    相关资源
    最近更新 更多