嵌套自定义配置元素集合

发布于 2024-10-04 16:50:51 字数 2319 浏览 4 评论 0原文

我正在尝试为我当前参与的 asp.net 项目实现自定义配置解决方案。

这是配置声明

    <sectionGroup name="WebsiteConfig" type="{namespace}.{class}, {assembly}">
        <section name="Languages" type="{namespace}.{class}, {assembly}"/>
        <section name="LinkFormats" type="{namespace}.{class}, {assembly}"/>
        <section name="Countries" type="{namespace}.{class}, {assembly}"/>
    </sectionGroup>

,这是我尝试使用的实际配置

<WebsiteConfig>

    <Languages>
        <Language code="en" domain="...">
            <Theme .../>
            <SiteMap ..."/>
        </Language>
        <Language code="de" domain="...">
            <Theme .../>
            <SiteMap .../>
        </Language>
    </Languages>

    <Countries>
        <Country Code="UK">
            <Files>
                <File name="..." fileUrl="..." enabled="true" />
                <File ... />
            </Files>
            <Messages>
                <Message Enabled="true" Message="..." />
                <Message ... />
            </Messages>
        </Country>
        <Country Code="...">....</Country>
    </Countries>

    <LinkFormats UseRewrites="false">
        <Link name="..." format="..." formatRewrite=".../"/>
        <link .... />
    </LinkFormats>

</WebsiteConfig>

我遇到的问题是文件Country 元素 (ConfigurationElement) 内的 Messages 集合会抛出无法识别的元素“File”等。

我的国家/地区元素具有以下文件和消息属性

    [ConfigurationProperty("Files")]
    public FilesSection Files
    {
        get
        {
            return (FilesSection)this["Files"];
        }
        set
        {
            this["Files"] = (object)value;
        }
    }

    [ConfigurationProperty("Messages")]
    public MessagesSection Messages
    {
        get
        {
            return (MessagesSection)this["Messages"];
        }
        set
        {
            this["Messages"] = (object)value;
        }
    }

FilesSection 和 MessagesSection 均派生自 ConfigurationElement,具有 x 类型的默认集合,该集合是从 ConfigurationElement 派生的项目集合。

有人知道我哪里出了问题吗?

我是否需要将“国家/地区”和“国家/地区”转换为“节组”,然后将“文件”和“消息”转换为“节”?

I'm trying to implement a custom configuration solution for an asp.net project I'm currently involved in.

Here is the configuration declaration

    <sectionGroup name="WebsiteConfig" type="{namespace}.{class}, {assembly}">
        <section name="Languages" type="{namespace}.{class}, {assembly}"/>
        <section name="LinkFormats" type="{namespace}.{class}, {assembly}"/>
        <section name="Countries" type="{namespace}.{class}, {assembly}"/>
    </sectionGroup>

with this being the actual configuration I'm trying to use

<WebsiteConfig>

    <Languages>
        <Language code="en" domain="...">
            <Theme .../>
            <SiteMap ..."/>
        </Language>
        <Language code="de" domain="...">
            <Theme .../>
            <SiteMap .../>
        </Language>
    </Languages>

    <Countries>
        <Country Code="UK">
            <Files>
                <File name="..." fileUrl="..." enabled="true" />
                <File ... />
            </Files>
            <Messages>
                <Message Enabled="true" Message="..." />
                <Message ... />
            </Messages>
        </Country>
        <Country Code="...">....</Country>
    </Countries>

    <LinkFormats UseRewrites="false">
        <Link name="..." format="..." formatRewrite=".../"/>
        <link .... />
    </LinkFormats>

</WebsiteConfig>

The problem I'm having is that the Files and Messages collections inside of the Country element (ConfigurationElement) throws an Unrecognised element 'File' etc.

My country element has the following properties for Files and Messages

    [ConfigurationProperty("Files")]
    public FilesSection Files
    {
        get
        {
            return (FilesSection)this["Files"];
        }
        set
        {
            this["Files"] = (object)value;
        }
    }

    [ConfigurationProperty("Messages")]
    public MessagesSection Messages
    {
        get
        {
            return (MessagesSection)this["Messages"];
        }
        set
        {
            this["Messages"] = (object)value;
        }
    }

FilesSection and MessagesSection are both derived from ConfigurationElement with a default collection of type x, which is a collection of items derived from ConfigurationElement.

Does anyone have an insight into where I've gone wrong?

Do I need to turn Countries and Country into SectionGroup's and then turn Files and Messages into Section's?

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

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

发布评论

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

评论(2

じ违心 2024-10-11 16:50:51

基于这篇文章 http://www.endswithsaurus.com/2008/11 /custom-configuration-section.html 我发现我需要设置 ElementName,因为父级不是节处理程序的嵌套集合将不遵守“AddItemName”来设置子元素。

Based on this article http://www.endswithsaurus.com/2008/11/custom-configuration-section.html I discovered that I needed to set the ElementName because a nested collection who's parent isn't a section handler will not obey the "AddItemName" for setting child elements.

一抹微笑 2024-10-11 16:50:51

您可以使用自定义名称覆盖 ElementName 属性,而不是像您提供的文章中那样设置元素名称的内部成员变量:

public class MySettings : ConfigurationElementCollection
{
    protected override ConfigurationElement CreateNewElement()
    {
        return new MySetting();
    }
    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((MySetting)element).Name;
    }

    protected override string ElementName
    {
        get { return “MySetting”; }
    }

    ...

请参阅 此处的文章了解更多详细信息:

Instead of setting the internal member variable for Element Name like in the article you supplied you can instead override the ElementName property with your custom name:

public class MySettings : ConfigurationElementCollection
{
    protected override ConfigurationElement CreateNewElement()
    {
        return new MySetting();
    }
    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((MySetting)element).Name;
    }

    protected override string ElementName
    {
        get { return “MySetting”; }
    }

    ...

See the article here for more detail:

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