1

这是一个简单的 Windows 窗体应用程序,一个屏幕,仅此而已。我创建了自己的 ConfigurationSection 处理程序

程序集 RemoteFactoryManager.Utils.dll

public class EnvironmentSection : ConfigurationSection
{
    public EnvironmentSection() { }

    public EnvironmentSection(  string description,
                                        string path,
                                        string selected)
    {
        this.Description = description;
        this.Path = path;
        this.Selected = selected;
    }

    [ConfigurationProperty("description", IsRequired = true)]
    public string Description
    {
        get { return this["description"] as string; }
        set { this["description"] = value; } 
    }

    [ConfigurationProperty("path", IsRequired = true)]
    public string Path
    {
        get { return this["path"] as string; }
        set { this["path"] = value; }
    }

    [ConfigurationProperty("selected", IsRequired = false)]
    public string Selected
    {
        get { return this["selected"] as string; }
        set { this["selected"] = value; }
    }
}

这是我的 app.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <sectionGroup name="Environments">
      <section name="DEV"  type="RemoteFactoryManager.Utils.EnvironmentSection, RemoteFactoryManager.Utils" />
      <section name="DEV1" type="RemoteFactoryManager.Utils.EnvironmentSection, RemoteFactoryManager.Utils" />
      <section name="DEV2" type="RemoteFactoryManager.Utils.EnvironmentSection, RemoteFactoryManager.Utils" />
    </sectionGroup>
  </configSections>
  <Enviroments>
    <DEV  description="Development"  path="\\SomeServer\C$\somepath" selected="1" />
    <UAT1 description="Devlpmnt - 1" path="\\SomeServer1\C$\somepath" />
    <UAT2 description="Devlpmnt - 2" path="\\SomeServer2\C$\somepath" />
  </Enviroments>
</configuration>

仍在组装RemoteFactoryManager.Utils.dll

public class ConfigReader
{
    private List<Env.Environment> _env;
    public List<Env.Environment> Environments { get { return _env; } }

    public void LoadEnvironments()
    {
        Configuration oConfiguration = ConfigurationManager.OpenExeConfiguration(
            ConfigurationUserLevel.None);

        ConfigurationSectionGroup oSectionGroup = 
            oConfiguration.GetSectionGroup("Environments") as 
                ConfigurationSectionGroup;

        if (oSectionGroup != null)
        {
            _env = new List<Env.Environment>();
            foreach (EnvironmentSection oSection in oSectionGroup.Sections)
            {
                Env.Environment e = new Env.Environment();
                e.Description = oSection.Description; // always empty
                e.Path = oSection.Path;               // always empty
                e.Selected = oSection.Selected;       // always empty
                _env.Add(e);
            }

        }
    }

.exe 代码

 private void frmMain_Load(object sender, EventArgs e)
    {
        ConfigReader c = new ConfigReader();
        c.LoadEnvironments();
    }

我的问题是

oSection.Description、oSection.Path 和 oSection.Selected 始终为空。

我究竟做错了什么?这曾经在另一个项目中工作正常。唯一的区别是现在 app.config 文件属于 .exe 并且处理这些部分的代码位于另一个单独的程序集中。

4

0 回答 0