gpt4 book ai didi

c# - App.Config自定义配置部分问题

转载 作者:太空狗 更新时间:2023-10-29 23:34:40 24 4
gpt4 key购买 nike

我已经为我的应用程序创建了一个自定义配置部分。出于某种原因,Visual Studio 2010 没有获取我的自定义属性。对于所有“添加”键,我都收到与此类似的警告:

Could not find schema information for the element 'urlFilterSection'

配置文件:

<configSections>
<section name="urlFilterSection" type="BotFinderApp.Models.UrlFilterSection, BotFinder" />
</configSections>

<urlFilterSection>
<urlFilterCollection>
<add url="urlhere.com.au" numberOfIpsToExtract="10" />
<add url="urlhere.com.au" numberOfIpsToExtract="10" />
<add url="urlhere.com.au" numberOfIpsToExtract="10" />
<add url="urlhere.com.au" numberOfIpsToExtract="10" />
<add url="urlhere.com.au" numberOfIpsToExtract="10" />
<add url="urlhere.com.au" numberOfIpsToExtract="10" />
<add url="urlhere.com.au" numberOfIpsToExtract="10" />
<add url="urlhere.com.au" numberOfIpsToExtract="10" />
<add url="urlhere.com.au" numberOfIpsToExtract="10" />
</urlFilterCollection>
</urlFilterSection>

UrlFilterSection:

namespace BotFinderApp.Models
{
public class UrlFilterSection : ConfigurationSection
{
public UrlFilterSection()
{
}

[ConfigurationProperty("urlFilterCollection", IsDefaultCollection = false)]
[ConfigurationCollection(typeof(UrlFilterCollection), AddItemName = "add", ClearItemsName = "clear", RemoveItemName = "remove")]
public UrlFilterCollection Urls
{
get
{
var urlsCollection = (UrlFilterCollection)base["urlFilterCollection"];
return urlsCollection;
}
}
}
}

UrlFilterCollection

namespace BotFinderApp.Models
{
public class UrlFilterCollection : ConfigurationElementCollection
{
public UrlFilterCollection()
{
}

protected override ConfigurationElement CreateNewElement()
{
return new UrlFilter();
}

protected override object GetElementKey(ConfigurationElement element)
{
return ((UrlFilter)element).Url;
}
}
}

网址过滤器

namespace BotFinderApp.Models
{
public class UrlFilter : ConfigurationElement
{
public UrlFilter()
{
}

[ConfigurationProperty("url", DefaultValue = "", IsRequired = true)]
public string Url
{
get { return (string)this["url"]; }
set { this["url"] = value; }
}

[ConfigurationProperty("numberOfIpsToExtract", DefaultValue = "0", IsRequired = true)]
public int NumberOfIpsToExtract
{
get { return (int)this["numberOfIpsToExtract"]; }
set { this["numberOfIpsToExtract"] = value; }
}
}
}

最佳答案

发现问题:

Decyclone 是正确的,错误实际上只是编译时警告。

真正的问题是我是这样访问我的配置的:

UrlFilterCollection serviceConfigSection = ConfigurationManager.GetSection("urlFilterSection") as UrlFilterCollection;

本来应该是这样的

UrlFilterSection serviceConfigSection = ConfigurationManager.GetSection("urlFilterSection") as UrlFilterSection;

谢谢 FlipScript 和 Decyclone :)

更新:

我发现了如何删除编译时警告 - 我使用的是 Visual Studio 2010。在创建我的自定义配置部分后,我使用工具栏中的“创建模式”按钮为配置生成模式文件。然后我将其保存到我的项目中,警告消失了。

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

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