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

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

问题描述

有没有办法让我包含一个简单的字符串数组,或者 List;在我的 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?)

我逐渐熟悉新的(而且非常冗长)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 的回答作为答案,因为这可能是我最接近旧式"配置部分的内容.我总是发现任何 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().

推荐答案

好的,你要求的很简单.嗯,存储一系列字符串的最简单方法是使用分隔列表(例如,逗号分隔).这样你就可以将它存储在一个元素中(比如在 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!).

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

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