首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ConfigurationElementCollection中的多个ConfigurationElement类型

ConfigurationElementCollection中的多个ConfigurationElement类型
EN

Stack Overflow用户
提问于 2009-06-11 04:56:19
回答 2查看 3.5K关注 0票数 4

我希望有一个配置部分,如下所示:

代码语言:javascript
复制
<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是由类型值定义的,并且每个元素都有自己的特定属性集。

另一种选择是所有元素都是相同的,但它们从不同的配置节加载值,例如:

代码语言:javascript
复制
<mycollection>
 <add name="myelement" configuration="myothersection" />
 <add name="myelement2" configuration="myothersection2" />
</mycollection>

<myothersection type="class, assembly" var1="value" var2="value" />

这似乎更容易实现,但会导致更详细的配置文件。

第一个选项是否符合.NET配置模式,第二个选项是否可行?

EN

回答 2

Stack Overflow用户

发布于 2011-01-12 23:35:15

你可以做的其他事情是覆盖OnDeserializeUnrecognizedElement。这样,您就可以创建如下所示的ParametersConfigurationElement:

代码语言:javascript
复制
<parameters>
  <string name="Patrick Huizinga" />
  <string country="the Netherlands" />
  <int currentScore="206" />
</parameters>

OnDeserializeUnrecognizedElement接受一个XmlReader,您可以查询该自定义元素的属性。通过使用TypeConverter.ConvertFromInvariantString (您可以从TypeDescriptor.GetConverter获得一个TypeConverter ),您甚至可以不需要任何额外的工作就可以支持DateTime和Uri等类型。

票数 3
EN

Stack Overflow用户

发布于 2021-12-24 06:41:19

来自Adding collections to a custom ConfigurationSection

这样的.config

代码语言:javascript
复制
<configSections>
  <section name="mySpecialSection" type="MyNamespace.MySpecialConfigurationSection, MyAssembly"/>
</configSections>
...
<mySpecialSection>
 <items>
  <apple value="one"/>
  <apple value="two"/>
  <orange value="one"/>
 </items>
</mySpecialSection>

可以通过代码来实现

代码语言:javascript
复制
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; }
 }
}

Adding collections to a custom ConfigurationSection

请注意,在这里,我们使用值" one“指定了两个集合项,这将导致其中一个在前面的示例中覆盖另一个。为了解决这个问题,我们返回元素本身作为唯一键,而不是返回Value属性。

这一次,我们的ConfigurationElementCollection派生类型的ConfigurationCollection属性也指定了一个逗号分隔的AddItemName,例如"allow,deny"。我们重写基本方法,如下所示:

当遇到正确的元素时,重写ElementName返回空string

  • Override IsElementName返回true name

  • Override GetNewElement(elementName)新建项目的实例type

  • Override ElementName为特定的元素名称创建正确项目类型的实例设置相关的properties

  • Override返回唯一标识项目的对象。这可以是一个属性值、某个散列形式的值组合或元素本身的
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/978113

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档