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

时间:2011-11-25 12:36:01

标签: c# .net xml app-config

我正在从“App.config”中读取设置。我刚刚弄清楚如何使用ConfigurationSectionConfigurationElementCollectionConfigurationelElement

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>

我的C#代码:

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中将字段声明为属性时,一切正常。但是当我尝试将属性转换为嵌套元素时 - “属性'Body'不是ConfigurationElement”错误发生。

我做错了什么?

1 个答案:

答案 0 :(得分:3)

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

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

这将允许你写

<body value="some val"/>

在你的配置中。