如何包括配置节的简单集合 [英] How to include simple collections in ConfigurationSection

查看:113
本文介绍了如何包括配置节的简单集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法对我来说,包括简单的字符串数组,或列表<字符串>在配置节我的自定义子类? (或数组或简单的数据对象的泛型列表,对于这个问题?)

Is there a way for me to include a simple array of strings, or List<string> on my custom subclass of ConfigurationSection? (Or an array or generic list of simple data objects, for that matter?)

我熟悉新的(非常详细)的ConfigurationSection,的ConfigurationElement和ConfigurationElementCollection类,但我绝不是专家呢。

I'm becoming familiar with the new (and VERY verbose) ConfigurationSection, ConfigurationElement, and ConfigurationElementCollection classes, but I'm by no means an expert yet.

这似乎是配置节应该处理简单的集合/列表自己,没有我不必创建一个自定义ConfigurationElementCollection子类的每一个人。但是我还没有发现任何提及这种能力在网上。

It seems like ConfigurationSection should handle simple collections/lists on its own, without me having to create a custom ConfigurationElementCollection subclass for each and every one. But I haven't found any reference to this ability online.

编辑:接受丹的回答的答案,因为它可能是最接近我会去的旧式configSections。我一直觉得很轻松,灵活,优雅,任何XmlSerializable对象很可能会成为一个configSection。我敢肯定,新的框架更强大;然而,这是可悲的,它是如此繁琐简单配置contructs,我们正在减少到回去String.Split()。

accepting Dan's response as the answer, since it's probably the closest thing I'm going to get to the "old style" configSections. I always found it easy, flexible, and elegant that any XmlSerializable object could easily become a configSection. I'm sure the new framework is more powerful; however it's sad that it is SO cumbersome for simple configuration contructs, that we're reduced to going back to String.Split().

推荐答案

OK,你问的很简单。好吧,存储一系列字符串的最简单的方法是使用一个分隔的列表(例如,逗号分隔)。这样,你可以将其存储在只有一个元素(在的appSettings说)。

OK, you asked for simple. Well, the simplest way to store a series of strings would be to use a delimited list (say, comma separated). That way you can store it in just one element (say in appSettings).

<add key="weekDays" value="Monday,Tuesday,Wednesday,Thursday,Friday"/>

当然,这也有缺点,但在大多数情况下,可以很好地用于一个简单的列表。然后,您可以使用 String.Split()将其转换回一个数组/列表。

Of course, this has drawbacks but in most cases works well for a simple list. You can then use String.Split() to convert it back to an array/list.

否则你回的ConfigurationSection元素当中,我同意,非常繁琐和笨拙的工作。但我不知道任何其他方式,我怕(但我很高兴能证明是错误的!)。

Otherwise you are back to ConfigurationSection elements which, I agree, are very verbose and clumsy to work with. But I don't know of any other way, I'm afraid (but am happy to be proved wrong!).

这篇关于如何包括配置节的简单集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆