创建自定义配置

时间:2010-04-21 16:19:14

标签: c# app-config

我创建了一个客户配置类Reports。然后我创建了另一个名为“ReportsCollection”的类。当我尝试执行“ConfigurationManager.GetSection()”时,它不会填充我的集合变量。任何人都可以在我的代码中看到任何错误吗?

这是集合类:

public class ReportsCollection : ConfigurationElementCollection
{
    public ReportsCollection()
    {
    }

    protected override ConfigurationElement CreateNewElement()
    {
        throw new NotImplementedException();
    }

    protected override ConfigurationElement CreateNewElement(string elementName)
    {
        return base.CreateNewElement(elementName);
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
        throw new NotImplementedException();
    }

    public Report this[int index]
    {
        get { return (Report)BaseGet(index); }
    }
}

以下是报告类:

public class Report : ConfigurationSection
{
    [ConfigurationProperty("reportName", IsRequired = true)]
    public string ReportName
    {
        get { return (string)this["reportName"]; }
        //set { this["reportName"] = value; }
    }

    [ConfigurationProperty("storedProcedures", IsRequired = true)]
    public StoredProceduresCollection StoredProcedures
    {
        get { return (StoredProceduresCollection)this["storedProcedures"]; }
    }

    [ConfigurationProperty("parameters", IsRequired = false)]
    public ParametersCollection Parameters
    {
        get { return (ParametersCollection)this["parameters"]; }
    }

    [ConfigurationProperty("saveLocation", IsRequired = true)]
    public string SaveLocation
    {
        get { return (string)this["saveLocation"]; }
    }

    [ConfigurationProperty("recipients", IsRequired = true)]
    public RecipientsCollection Recipients
    {
        get { return (RecipientsCollection)this["recipients"]; }
    }
}

public class StoredProcedure : ConfigurationElement
{
    [ConfigurationProperty("storedProcedureName", IsRequired = true)]
    public string StoredProcedureName
    {
        get { return (string)this["storedProcedureName"]; }
    }
}

public class StoredProceduresCollection : ConfigurationElementCollection
{
    protected override ConfigurationElement CreateNewElement()
    {
        throw new NotImplementedException();
    }

    protected override ConfigurationElement CreateNewElement(string elementName)
    {
        return base.CreateNewElement(elementName);
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
        throw new NotImplementedException();
    }

    public StoredProcedure this[int index]
    {
        get { return (StoredProcedure)base.BaseGet(index); }
    }
}
}

以下是创建变量的非常简单的代码:

ReportsCollection reportsCollection = (ReportsCollection) System.Configuration.ConfigurationManager.GetSection("ReportGroup");

EDIT 添加了App.Config

<configSections>
<sectionGroup name="ReportGroup">
  <section name="Reports" type="ReportsGenerator.ReportsCollection"/>
</sectionGroup>
</configSections>
<ReportGroup>
<Reports name="DailyIssues" SaveLocation="">
  <StoredProcedures>
    <add StoredProcedureName="RPTDailyIssues" />
    <add StoredProcedureName="RPTNoIssues" />
  </StoredProcedures>
  <Parameters>
    <add ParameterName="@FromDate" />
    <add ParameterName="@ThruDate" />
  </Parameters>
  <Recipients>
    <add RecipientName="me@mycompany.com"
  </Recipients>
</Reports>
</ReportGroup>

2 个答案:

答案 0 :(得分:4)

你应该在CodeProject上查看Jon Rista关于.NET 2.0配置的三部分系列文章。

强烈推荐,写得很好,非常有帮助!

此外,Visual Studio还有一个Configuration Section Designer加载项,它对创建自定义配置部分非常有用 - 它具有可视化设计器,可在后台创建所有必需的XSD和类。

马克

答案 1 :(得分:0)

我暂时没有写配置部分,但是我的头脑中你的CreateNewElement()方法抛出异常..让它们至少返回虚拟条目,也许这就是原因..)

另外,在web.config中显示reportsCollection元素..是否正确注册了?