为什么我不能将属性转换为嵌套元素?

本文关键字:嵌套 元素 转换 属性 不能 为什么 | 更新日期: 2023-09-27 18:15:48

我正在从'App.config'中读取设置。我刚刚知道如何处理ConfigurationSection, ConfigurationElementCollectionConfigurationelElement

App.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration> 
    <configSections>
        <sectionGroup name="notificationSettingsGroup">
                <section name="mailTemplates" type="Project.Lib.Configuration.MailTemplateSection, Project.Lib"
                    allowDefinition="Everywhere" allowExeDefinition="MachineToApplication" requirePermission="false"/>
        </sectionGroup>         
    </configSections>
    <notificationSettingsGroup>
        <mailTemplates>
            <items>
                <mailTemplate name="actionChain" subject="Subject bla-bla">
                    <body>Body bla-bla</body>
                </mailTemplate>                 
            </items>
        </mailTemplates>
    </notificationSettingsGroup>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
    </startup>  
</configuration>

My c# code:

public class MailTemplateSection : ConfigurationSection
{
    [ConfigurationProperty("items", IsDefaultCollection = false)]
    public MailTemplateCollection MailTemplates
    {
        get { return (MailTemplateCollection)this["items"]; }
        set { this["items"] = value; }
    }
}

[ConfigurationCollection(typeof(MailTemplateElement), AddItemName = "mailTemplate",
    CollectionType = ConfigurationElementCollectionType.AddRemoveClearMap)]
public class MailTemplateCollection : ConfigurationElementCollection
{
    protected override ConfigurationElement CreateNewElement()
    {
        return new MailTemplateElement();
    }
    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((MailTemplateElement) element).Name;
    }
}

public class MailTemplateElement : ConfigurationElement
{
    [ConfigurationProperty("name", DefaultValue = "action", IsKey = true, IsRequired = true)]
    public string Name
    {
        get { return (string)this["name"]; }
        set { this["name"] = value; }
    }
    [ConfigurationProperty("subject", DefaultValue = "Subject", IsKey = false, IsRequired = true)]
    public string Subject
    {
        get { return (string)this["subject"]; }
        set { this["subject"] = value; }
    }
    [ConfigurationProperty("body", DefaultValue = "Body", IsKey = false, IsRequired = true)]
    public string Body
    {
        get { return (string)this["body"]; }
        set { this["body"] = value; }
    }
}

和工作代码:

class Program
{
    static void Main(string[] args)
    {
        Configuration config =
            ConfigurationManager.OpenExeConfiguration(
            ConfigurationUserLevel.None);
        var mailTemplatesSection =
           config.GetSection("notificationSettingsGroup/mailTemplates") as MailTemplateSection;
    }
}

所有工作,当我在xml中声明字段作为属性。但是当我尝试将属性转换为嵌套元素时- "Property 'Body' is not a ConfigurationElement"发生错误。

我做错了什么?

为什么我不能将属性转换为嵌套元素?

因为您必须创建自定义类型并从ConfigurationElement中派生它们,以便将它们用作配置文件中的元素。所有简单类型总是写成属性。例如:

public class Body : ConfigurationElement
{
    [ConfigurationProperty("value", DefaultValue = "Body", IsKey = true, IsRequired = true)]
    public string Value{get;set;}
}

这将允许你写

<body value="some val"/>
相关文章: