gpt4 book ai didi

c# - 创建自定义配置

转载 作者:行者123 更新时间:2023-11-30 15:48:22 25 4
gpt4 key购买 nike

我创建了一个客户配置类 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");

编辑添加了 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>

最佳答案

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

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

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

马克

关于c# - 创建自定义配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2684681/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com