如何在ConfigurationSection中包含简单集合 [英] How to include simple collections in ConfigurationSection

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

问题描述

有没有办法让我包含一个简单的字符串数组,或List< string>在我的自定义子类ConfigurationSection? (或者一个数组或通用的简单数据对象列表,对于这个问题?)

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?)

我熟悉新的(和VERY verbose)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.

看起来ConfigurationSection应该自己处理简单的集合/列表,而无需创建一个自定义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.

编辑:接受Dan的回答作为答案,因为它可能是最接近的事我要去的旧风格configSections。我总是发现它容易,灵活和优雅,任何XmlSerializable对象可以轻松地成为一个configSection。我相信新框架更强大;然而,令人遗憾的是,对于简单的配置结构来说这是很麻烦的,我们减少到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, 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!).

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

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