gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 01:51:56 24 4
gpt4 key购买 nike

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

应用程序配置:

<?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 中将字段声明为属性时,一切正常。但是当我尝试将属性转换为嵌套元素时 - “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"/>

在您的配置中。

关于c# - 为什么我不能将属性转换为嵌套元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8269220/

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