简体   繁体   English

ConfigurationElementCollection中的无键元素

[英]Key-less elements in ConfigurationElementCollection

When you inherit ConfigurationElementCollection: 当您继承ConfigurationElementCollection时:

public class Directories : ConfigurationElementCollection
{
    ...
}

ConfigurationElementCollection requires an implementation for GetElementKey(System.Configuration.ConfigurationElement) . ConfigurationElementCollection需要GetElementKey(System.Configuration.ConfigurationElement)

But I don't care about keys since I have a custom configuration section like this: 但是我不在乎密钥,因为我有一个自定义配置部分,如下所示:

<directorySection>
  <directories>
    <directory pickUpDirectory="C:\Users\User\Documents\PickUp\0" dropOffDirectory="C:\Users\User\Documents\DropOff\0"/>
    <directory pickUpDirectory="C:\Users\User\Documents\PickUp\0" dropOffDirectory="C:\Users\User\Documents\DropOff\0"/>
    <directory pickUpDirectory="C:\Users\User\Documents\PickUp\1" dropOffDirectory="C:\Users\User\Documents\DropOff\1"/>
    <directory pickUpDirectory="C:\Users\User\Documents\PickUp\2" dropOffDirectory="C:\Users\User\Documents\DropOff\2"/>
  </directories>
</directorySection>

It can have multiple elements, and each element is key-less, and this structure should allow for duplicates (as above). 它可以具有多个元素,并且每个元素都是无密钥的,并且此结构应允许重复(如上所述)。 So what should I do in this case? 那么在这种情况下我该怎么办?

Try to use ObjectIDGenerator like this 尝试像这样使用ObjectIDGenerator

[ConfigurationCollection(typeof(SchedulerTaskItem), AddItemName = "Task")]
public class SchedulerTaskCollection: ConfigurationElementCollection
{
    private static readonly ObjectIDGenerator idGenerator = new ObjectIDGenerator();

    protected override ConfigurationElement CreateNewElement()
    {
        return new SchedulerTaskItem();
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
        bool firstTime;
        return idGenerator.GetId(element, out firstTime);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ConfigurationElementCollection对象是否可以为其元素包含另一个ConfigurationElementCollection对象? - Can ConfigurationElementCollection object contain for its elements another ConfigurationElementCollection objects? 如何在ConfigurationElementCollection中按键查找 - how do I find by key in ConfigurationElementCollection ConfigurationElementCollection 和 Linq - ConfigurationElementCollection and Linq LINQ的ConfigurationElementCollection - ConfigurationElementCollection LINQ 从ConfigurationPropertyAttribute中获取ConfigurationElementCollection中的GetKey? - GetKey in ConfigurationElementCollection from ConfigurationPropertyAttribute? 具有基本类型的ConfigurationElementCollection - ConfigurationElementCollection with primitive types 如何使用ConfigurationElementCollection实现ConfigurationSection - How to implement a ConfigurationSection with a ConfigurationElementCollection 隐式ConfigurationElementCollection部分 - Implicit ConfigurationElementCollection sections 自定义 ConfigurationElementCollection 抛出 TargetInvocationException - Custom ConfigurationElementCollection Throw TargetInvocationException 在c#中检索键小于指定值的元素集合 - Retrieve collection of elements whose key is less than specified value in c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM