正确实现带有嵌套集合的自定义配置部分?
|
在Web应用程序中,我希望能够使用config这样的部分来定义一些映射:
<configuration>
<configSections>
<sectionGroup name=\"MyCustomer\">
<section name=\"CatalogMappings\" type=\"MyCustom.MyConfigSection\" />
</sectionGroup>
</configSections>
<MyCustomer>
<catalogMappings>
<catalog name=\"toto\">
<mapping value=\"1\" displayText=\"titi\" />
<mapping value=\"2\" displayText=\"tata\" />
</catalog>
<catalog name=\"toto2\">
<mapping value=\"1\" displayText=\"titi2\" />
<mapping value=\"2\" displayText=\"tata2\" />
</catalog>
</catalogMappings>
</MyCustomer>
</configuration>
我正在努力实现这一目标,尤其是在定义收藏夹时。我需要实现哪些类才能实现这一目标?
目前,我有:
public class CatalogMappingSection : System.Configuration.ConfigurationSection
{
public class Mapping : ConfigurationElement
{
[ConfigurationProperty(\"externalKey\")]
public string ExternalKey { get; set; }
[ConfigurationProperty(\"displayText\", IsRequired=true)]
public string DisplayText { get; set; }
[ConfigurationProperty(\"value\", IsRequired=true, IsKey=true)]
public int Value { get; set; }
}
public class Catalog : ConfigurationElementCollection
{
[ConfigurationProperty(\"name\", IsRequired=true, IsKey=true)]
public string Name { get; set; }
protected override ConfigurationElement CreateNewElement()
{
return new Mapping();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((Mapping)element).Value;
}
}
public class CatalogCollection : ConfigurationElementCollection
{
[ConfigurationProperty(\"catalog\")]
[ConfigurationCollection(typeof(Catalog))]
public Catalog CatalogMappingCollection
{
get
{
return (Catalog)base[\"catalog\"];
}
}
protected override ConfigurationElement CreateNewElement()
{
return new Catalog();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((Catalog)element).Name;
}
}
[ConfigurationProperty(\"catalogMappings\")]
[ConfigurationCollection(typeof(CatalogCollection))]
public CatalogCollection CatalogMappings
{
get
{
return (CatalogCollection)base[\"catalogMappings\"];
}
}
}
但是,这没有按预期工作。
没有找到相关结果
已邀请:
2 个回复
姬第柔炒
和配置代码
然后“打电话”
骂陋冠
\“ app.config \”
现在是“代码”