C# 配置第二节中的自定义IEnumerable
原始问题和代码: 通过@Daniel Hilgarth answer,我将(app.config)更改为:C# 配置第二节中的自定义IEnumerable,c#,configuration,app-config,C#,Configuration,App Config,原始问题和代码: 通过@Daniel Hilgarth answer,我将(app.config)更改为: 尝试将以下属性添加到路径配置部分。路径属性: [ConfigurationCollection(typeof(Paths), AddItemName = "Path")] 现在应该是这样的: [ConfigurationProperty("Paths")] [ConfigurationCollection(typeof(Paths), AddItemName =
尝试将以下属性添加到
路径配置部分。路径属性:
[ConfigurationCollection(typeof(Paths), AddItemName = "Path")]
现在应该是这样的:
[ConfigurationProperty("Paths")]
[ConfigurationCollection(typeof(Paths), AddItemName = "Path")]
public Paths Paths
{
get
{
var o = this["Paths"];
return o as Paths;
}
}
using System.Configuration;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var t = DiskConfigSection.GetConfig();
// Put a breakpoint on the line after this and put a watch on t
}
}
public class Path : ConfigurationElement
{
[ConfigurationProperty("name", IsRequired = true)]
public string Name
{
get
{
return (string)this["name"];
}
}
[ConfigurationProperty("permission", IsRequired = true)]
public string Permission
{
get
{
return (string)this["permission"];
}
}
}
public class Paths : ConfigurationElementCollection
{
public Path this[int index]
{
get
{
return BaseGet(index) as Path;
}
set
{
if (BaseGet(index) != null)
{
BaseRemoveAt(index);
}
BaseAdd(index, value);
}
}
protected override ConfigurationElement CreateNewElement()
{
return new Path();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((Path)element).Name;
}
}
public class DiskConfigSection : ConfigurationSection
{
public static DiskConfigSection GetConfig()
{
var b = ConfigurationManager.GetSection("Disk") != null;
return b ? (DiskConfigSection)ConfigurationManager.GetSection("Disk") : new DiskConfigSection();
}
[ConfigurationProperty("Paths")]
[ConfigurationCollection(typeof(Paths), AddItemName = "Path")]
public Paths Paths
{
get
{
var o = this["Paths"];
return o as Paths;
}
}
}
}
您的配置现在应该如下所示:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="Disk" type="ConsoleApplication1.DiskConfigSection, ConsoleApplication1"/>
</configSections>
<Disk>
<Paths>
<Path name="one" permission="1" />
<Path name="two" permission="2" />
<Path name="three" permission="3" />
</Paths>
</Disk>
</configuration>
我已在控制台应用程序中进行了测试,配置已完全加载。我已编辑了您的标题。请看“”,其中的共识是“不,他们不应该”。这要归功于@DanielHilgarth,正是因为他的评论,我才能够构建这个框架。@Monkieboy:谢谢。但我不知道你为什么重复我的答案。你认为这样做有什么价值?我无法加入评论,而且@Ferpega没有将你的答案标记为完整,我假设他没有找到解决方案。我用你的建议作为一个平台来提供解决方案,如果你觉得我没有给这个问题增加清晰度或价值,我很乐意删除我的答案。@Monkieboy:如果你认为你的答案增加了价值,那就随它去吧,没问题。我想如果它仍然不起作用,费佩加会要求澄清的。你必须给他一些时间来测试一下——或者甚至意识到有答案;)好的,请原谅我,我不想打扰你或其他人。我将删除我的贡献。@Monkieboy:你当然没有惹恼任何人,我也没有要求你删除你的答案。保持冷静:-)
[ConfigurationProperty("Paths")]
[ConfigurationCollection(typeof(Paths), AddItemName = "Path")]
public Paths Paths
{
get
{
var o = this["Paths"];
return o as Paths;
}
}
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="Disk" type="ConsoleApplication1.DiskConfigSection, ConsoleApplication1"/>
</configSections>
<Disk>
<Paths>
<Path name="one" permission="1" />
<Path name="two" permission="2" />
<Path name="three" permission="3" />
</Paths>
</Disk>
</configuration>
using System.Configuration;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var t = DiskConfigSection.GetConfig();
// Put a breakpoint on the line after this and put a watch on t
}
}
public class Path : ConfigurationElement
{
[ConfigurationProperty("name", IsRequired = true)]
public string Name
{
get
{
return (string)this["name"];
}
}
[ConfigurationProperty("permission", IsRequired = true)]
public string Permission
{
get
{
return (string)this["permission"];
}
}
}
public class Paths : ConfigurationElementCollection
{
public Path this[int index]
{
get
{
return BaseGet(index) as Path;
}
set
{
if (BaseGet(index) != null)
{
BaseRemoveAt(index);
}
BaseAdd(index, value);
}
}
protected override ConfigurationElement CreateNewElement()
{
return new Path();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((Path)element).Name;
}
}
public class DiskConfigSection : ConfigurationSection
{
public static DiskConfigSection GetConfig()
{
var b = ConfigurationManager.GetSection("Disk") != null;
return b ? (DiskConfigSection)ConfigurationManager.GetSection("Disk") : new DiskConfigSection();
}
[ConfigurationProperty("Paths")]
[ConfigurationCollection(typeof(Paths), AddItemName = "Path")]
public Paths Paths
{
get
{
var o = this["Paths"];
return o as Paths;
}
}
}
}