我希望有一个配置部分,如下所示:
<mycollection>
<add name="myelement" type="class, assembly" var1="value" var2="value" />
<add name="myelement2" type="class2, assembly" var1="value" var3="value" var4="value" />
</mycollection>其思想是,实际创建的ConfigurationElement是由类型值定义的,并且每个元素都有自己的特定属性集。
另一种选择是所有元素都是相同的,但它们从不同的配置节加载值,例如:
<mycollection>
<add name="myelement" configuration="myothersection" />
<add name="myelement2" configuration="myothersection2" />
</mycollection>
<myothersection type="class, assembly" var1="value" var2="value" />这似乎更容易实现,但会导致更详细的配置文件。
第一个选项是否符合.NET配置模式,第二个选项是否可行?
发布于 2011-01-12 23:35:15
你可以做的其他事情是覆盖OnDeserializeUnrecognizedElement。这样,您就可以创建如下所示的ParametersConfigurationElement:
<parameters>
<string name="Patrick Huizinga" />
<string country="the Netherlands" />
<int currentScore="206" />
</parameters>OnDeserializeUnrecognizedElement接受一个XmlReader,您可以查询该自定义元素的属性。通过使用TypeConverter.ConvertFromInvariantString (您可以从TypeDescriptor.GetConverter获得一个TypeConverter ),您甚至可以不需要任何额外的工作就可以支持DateTime和Uri等类型。
发布于 2021-12-24 06:41:19
来自Adding collections to a custom ConfigurationSection
这样的.config
<configSections>
<section name="mySpecialSection" type="MyNamespace.MySpecialConfigurationSection, MyAssembly"/>
</configSections>
...
<mySpecialSection>
<items>
<apple value="one"/>
<apple value="two"/>
<orange value="one"/>
</items>
</mySpecialSection>可以通过代码来实现
public class MySpecialConfigurationSection : ConfigurationSection
{
[ConfigurationProperty("items", IsRequired = false, IsKey = false, IsDefaultCollection = false)]
public ItemCollection Items
{
get { return ((ItemCollection) (base["items"])); }
set { base["items"] = value; }
}
}
[ConfigurationCollection(typeof(Item), AddItemName = "apple,orange", CollectionType = ConfigurationElementCollectionType.BasicMapAlternate)]
public class ItemCollection : ConfigurationElementCollection
{
public override ConfigurationElementCollectionType CollectionType
{
get { return ConfigurationElementCollectionType.BasicMapAlternate; }
}
protected override string ElementName
{
get { return string.Empty; }
}
protected override bool IsElementName(string elementName)
{
return (elementName == "apple" || elementName == "orange");
}
protected override object GetElementKey(ConfigurationElement element)
{
return element;
}
protected override ConfigurationElement CreateNewElement()
{
return new Item();
}
protected override ConfigurationElement CreateNewElement(string elementName)
{
var item = new Item();
if (elementName == "apple")
{
item.Type = ItemType.Apple;
}
else if(elementName == "orange")
{
item.Type = ItemType.Orange;
}
return item;
}
public override bool IsReadOnly()
{
return false;
}
}
public enum ItemType
{
Apple,
Orange
}
public class Item
{
public ItemType Type { get; set; }
[ConfigurationProperty("value")]
public string Value
{
get { return (string)base["value"]; }
set { base["value"] = value; }
}
}请注意,在这里,我们使用值" one“指定了两个集合项,这将导致其中一个在前面的示例中覆盖另一个。为了解决这个问题,我们返回元素本身作为唯一键,而不是返回Value属性。
这一次,我们的ConfigurationElementCollection派生类型的ConfigurationCollection属性也指定了一个逗号分隔的AddItemName,例如"allow,deny"。我们重写基本方法,如下所示:
当遇到正确的元素时,重写ElementName返回空string
IsElementName返回true name
GetNewElement(elementName)新建项目的实例type
ElementName为特定的元素名称创建正确项目类型的实例设置相关的properties
https://stackoverflow.com/questions/978113
复制相似问题