.NET 中的自定义配置

发布于 2024-07-16 20:33:24 字数 848 浏览 8 评论 0原文

我看到有关 .NET 中自定义配置的简单示例。 我的情况有点复杂,有嵌套节点。

我希望能够从配置文件中读取此内容:

 <environments>

<environment name="live" url="http://www.live.com">
  <server name="a" IP="192.168.1.10"></server>
  <server name="b" IP="192.168.1.20"></server>
  <server name="c" IP="192.168.1.30"></server>      
</environment>

<environment name="dev" url="http://www.dev.com">
  <server name="a" IP="192.168.1.10"></server>
  <server name="c" IP="192.168.1.30"></server>
</environment>

<environment name="test" url="http://www.test.com">
  <server name="b" IP="192.168.1.20"></server>
  <server name="d" IP="192.168.1.40"></server>
</environment></environments> 

如果有人可以为此提供一些代码,我将不胜感激。

谢谢!

I am seeing simple examples regarding custom configuration in .NET. My case is a bit more complex, with nested nodes.

I'd like to be able to read this from the configuration file:

 <environments>

<environment name="live" url="http://www.live.com">
  <server name="a" IP="192.168.1.10"></server>
  <server name="b" IP="192.168.1.20"></server>
  <server name="c" IP="192.168.1.30"></server>      
</environment>

<environment name="dev" url="http://www.dev.com">
  <server name="a" IP="192.168.1.10"></server>
  <server name="c" IP="192.168.1.30"></server>
</environment>

<environment name="test" url="http://www.test.com">
  <server name="b" IP="192.168.1.20"></server>
  <server name="d" IP="192.168.1.40"></server>
</environment></environments> 

If anyone could provide some code for that, I'd appreciate it.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

偷得浮生 2024-07-23 20:33:25

您可以通过实现从 ConfigurationElement 类继承的自定义配置类来阅读此内容。

以下是“server”元素的示例:

public class ServerElement: ConfigurationElement
{
    [ConfigurationProperty("name", IsRequired = true, IsKey = true)]
    public string Name
    {
        get { return ((string)base["name"]); }
        set { base["name"] = value; }
    }

    ...
}

环境元素实际上是一个集合,可以这样实现:

public class EnvironmentElement: ConfigurationElementCollection
{
    protected override ConfigurationElement CreateNewElement(string elementName)
    {
        return new ServerElement(...);
    }
}

You can read this by implementing custom configuration classes inheriting from the ConfigurationElement class.

Here is an example of the "server" element:

public class ServerElement: ConfigurationElement
{
    [ConfigurationProperty("name", IsRequired = true, IsKey = true)]
    public string Name
    {
        get { return ((string)base["name"]); }
        set { base["name"] = value; }
    }

    ...
}

The environment element is actually a collection and could be implemented like this:

public class EnvironmentElement: ConfigurationElementCollection
{
    protected override ConfigurationElement CreateNewElement(string elementName)
    {
        return new ServerElement(...);
    }
}
不知在何时 2024-07-23 20:33:25

我最近遇到的一个很棒的工具是 CodePlex 上的配置部分设计器; 很好地集成到 VS 中。

生成类、XML 架构等。推荐。

A nifty tool I came across recently is the Configuration Section Designer up on CodePlex; integrates into VS nicely.

Generates classes, XML schema, etc. Recommended.

双手揣兜 2024-07-23 20:33:25

您需要创建一些类来处理配置文件的自定义配置部分。

Phil Haack 对此有一篇非常好的博客文章:通过 3 个简单步骤实现自定义配置部分。

编辑:

我试图找到我过去使用过的代码项目文章来学习如何实现这一目标,我在 Phil 上找到了它博客条目:

这里是揭开.NET 2.0 配置的神秘面纱

它包含处理嵌套元素和集合所需的信息。

Your need to create some classes to handle the custom configuration sections of the config file.

There's a very good blog entry by Phil Haack on this: Custom Configuration Sections in 3 Easy Steps.

Edit:

I was trying to find the Code Project article I've used in the past to learn how to achieve this, and I've found it on Phil's blog entry:

here it is Unraveling the Mysteries of .NET 2.0 Configuration

it contains the info necessary to handle nested elements and collections.

枕梦 2024-07-23 20:33:25

查看示例的内容(针对不同的暂存环境),您可能需要查看 Web 部署项目,如 回答之前的问题

相反,它们在部署时处理配置合并:一个好主意,特别是如果您有敏感信息,例如带有服务帐户密码的连接字符串......

Looking at the content of your example (targeting different staging environments), rather than reading config files dynamically, you might want to look at web deployment projects, as indicated in the answer to an earlier question.

They handle config merging at deployment time instead: a good idea, especially if you have sensitive information like connection strings with service account passwords...

自演自醉 2024-07-23 20:33:25

我会使用 App.Config 文件

System.Configuration.ConfigurationSettings.AppSettings["DatabasePath"];

http://www.eggheadcafe.com/community/ aspnet/2/10004734/appconfig-file-c.aspx

I would use App.Config file

System.Configuration.ConfigurationSettings.AppSettings["DatabasePath"];

http://www.eggheadcafe.com/community/aspnet/2/10004734/appconfig-file-c.aspx

多情出卖 2024-07-23 20:33:25

以下是我在阅读本文后最近实现的:

使用反射将类绑定到 .ini 文件

http://dvuyka.spaces.live.com/blog/cns!305B02907E9BE19A!174.entry

Here is what I implemented recently after reading this article:

Using Reflection for binding classes to .ini files

http://dvuyka.spaces.live.com/blog/cns!305B02907E9BE19A!174.entry

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文