如何创建多个不同类型的配置项
How to create multiple configuration items of different types
是否可以有如下所示的不同类型的配置项列表?
<exceptions>
<exception name ="exception1" host="myhostname">
<block user="joe"/>
<block user="peter"/>
<allow user="admin"/>
</exception>
<exception name ="exception2" host="anotherhostname">
<block user="sue"/>
<block user="danny"/>
<allow user="johnny"/>
</exception>
</exceptions>
我正在尝试定义一个配置,让我可以阻止或允许给定主机名上的用户。如果我只有 <allow>
类型的元素,那会很简单:
[ConfigurationCollection(typeof(AllowElement))]
class AllowCollection : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new AllowElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((AllowElement) element).User;
}
public AllowElement this[int index]
{
get { return (AllowElement) BaseGet(index); }
}
public new AllowElement this[string key]
{
get { return (AllowElement) BaseGet(key); }
}
}
class AllowElement : ConfigurationElement
{
[ConfigurationProperty("user", IsKey = true, IsRequired = true)]
public string User
{
get { return (string) base["user"]; }
}
}
如何同时添加 <block>
配置项?真的可以吗?
基本上,您必须覆盖 IsElementName 以提供对有效项目名称的检查(在您的情况下是阻止和允许)。您还需要将这些有效值映射到 ConfigurationCollection 的 AddItem 名称 属性,作为逗号分隔的字符串。
您还必须将 CollectionType
覆盖为 return ConfigurationElementCollectionType.BasicMapAlternate
。
然后您需要将您的 AllowElement 更改为支持允许和阻止的通用元素。 link 示例使用枚举,但您可以随心所欲。在 CreateNewElement 上,您将处理 elementName 和 return 具有正确类型标识符的通用元素集。
这是 link to Derek says,您可以在其中找到我在名为 具有不同元素名称的集合项目 [=13] 部分的建议的实现=]
很抱歉复制粘贴 link,但我想 link 看起来正是您需要的解决方案比只有 [=25 什么都没有的巨大代码块更好=] 名称不同。
是否可以有如下所示的不同类型的配置项列表?
<exceptions>
<exception name ="exception1" host="myhostname">
<block user="joe"/>
<block user="peter"/>
<allow user="admin"/>
</exception>
<exception name ="exception2" host="anotherhostname">
<block user="sue"/>
<block user="danny"/>
<allow user="johnny"/>
</exception>
</exceptions>
我正在尝试定义一个配置,让我可以阻止或允许给定主机名上的用户。如果我只有 <allow>
类型的元素,那会很简单:
[ConfigurationCollection(typeof(AllowElement))]
class AllowCollection : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new AllowElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((AllowElement) element).User;
}
public AllowElement this[int index]
{
get { return (AllowElement) BaseGet(index); }
}
public new AllowElement this[string key]
{
get { return (AllowElement) BaseGet(key); }
}
}
class AllowElement : ConfigurationElement
{
[ConfigurationProperty("user", IsKey = true, IsRequired = true)]
public string User
{
get { return (string) base["user"]; }
}
}
如何同时添加 <block>
配置项?真的可以吗?
基本上,您必须覆盖 IsElementName 以提供对有效项目名称的检查(在您的情况下是阻止和允许)。您还需要将这些有效值映射到 ConfigurationCollection 的 AddItem 名称 属性,作为逗号分隔的字符串。
您还必须将 CollectionType
覆盖为 return ConfigurationElementCollectionType.BasicMapAlternate
。
然后您需要将您的 AllowElement 更改为支持允许和阻止的通用元素。 link 示例使用枚举,但您可以随心所欲。在 CreateNewElement 上,您将处理 elementName 和 return 具有正确类型标识符的通用元素集。
这是 link to Derek says,您可以在其中找到我在名为 具有不同元素名称的集合项目 [=13] 部分的建议的实现=]
很抱歉复制粘贴 link,但我想 link 看起来正是您需要的解决方案比只有 [=25 什么都没有的巨大代码块更好=] 名称不同。