我正在尝试创建一个看起来像这样的自定义配置。
<configuration>
<configSections>
<section name="actions" type="ConfigurationTest.ActionsConfig, UnitTestExperiments" />
<section name="action" type="ConfigurationTest.ActionConfig, UnitTestExperiments"/>
</configSections>
<actions poolSize="100">
<action name="a1" impl="some.class1">
<add name="key11" value="value11"/>
<add name="key12" value="key12"/>
</action>
<action name="a2" impl="some.class2">
<add name="key21" value="value21"/>
<add name="key22" value="key22"/>
</action>
</actions>
</configuration>
此处<action>
标记可以出现N次,每个操作都有一组唯一的键。我尝试了几种不同的解决方案和选项,但似乎我无法使类结构/映射正确。我希望得到一个我可以这样称呼的结构。
using System;
using System.Configuration;
namespace ConfigurationTest
{
public class ActionsConfig : ConfigurationSection
{
[ConfigurationProperty("poolSize")]
public string PoolSize
{
get { return (string)this["poolSize"]; }
}
[ConfigurationProperty("", IsRequired = true, IsDefaultCollection = true)]
public ActionList Instances
{
get { return (ActionList)this[""]; }
set { this[""] = value; }
}
}
public class ActionList : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new ActionConfig();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((ActionConfig)element).Name;
}
}
public class ActionConfig : ConfigurationSection
{
[ConfigurationProperty("name")]
public string Name
{
get { return (string)this["name"]; }
}
[ConfigurationProperty("impl")]
public string Impl
{
get { return (string)this["impl"]; }
}
[ConfigurationProperty("", IsDefaultCollection = true)]
public NameValueConfigurationCollection Settings
{
get
{
return (NameValueConfigurationCollection)base[""];
}
}
}
public class Program
{
static void Main(string[] args)
{
ActionsConfig ac = ConfigurationManager.GetSection("actions") as ActionsConfig;
Console.WriteLine(ac.PoolSize);
ActionConfig nameValueSection = ac.CurrentConfiguration.GetSection("action") as ActionConfig;
NameValueConfigurationCollection settings = nameValueSection.Settings;
foreach (var key in settings.AllKeys)
{
Console.WriteLine(settings[key].Name + ": " + settings[key].Value);
}
}
}
}
异常跟踪如下。
System.Configuration.ConfigurationErrorsException was unhandled
Message=Unrecognized element 'action'. (UnitTestExperiments.vshost.exe.Config line 8)
Source=System.Configuration
BareMessage=Unrecognized element 'action'.
Filename=UnitTestExperiments.vshost.exe.Config
Line=8
StackTrace:
at System.Configuration.BaseConfigurationRecord.EvaluateOne(String[] keys, SectionInput input, Boolean isTrusted, FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentResult)
at System.Configuration.BaseConfigurationRecord.Evaluate(FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentResult, Boolean getLkg, Boolean getRuntimeObject, Object& result, Object& resultRuntimeObject)
at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject)
at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject)
at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject)
at System.Configuration.BaseConfigurationRecord.GetSection(String configKey)
at System.Configuration.ConfigurationManager.GetSection(String sectionName)
at ConfigurationTest.Program.Main(String[] args) in C:\Users\me\Documents\Visual Studio 2010\Projects\UnitTestExperiments\Program.cs:line 63
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
答案 0 :(得分:3)
看一下使用Custom Configuration Settings。这是一个非常好的例子。
<强>更新强>
自定义ADD部分会查看示例Custom ConfigurationSections in .NET 2.0 .config Files
答案 1 :(得分:-4)
我从演示项目Here
得到了确切的答案我不得不将代码扩展到一个额外的层,以使其适用于我。