可扩展的C#code从配置文件创建数组 [英] Scalable C# Code for Creating Array from Config File

查看:90
本文介绍了可扩展的C#code从配置文件创建数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有配置值,如下图所示。

 添加键=屏蔽1值=管理员
添加键=画面2值=登陆
 

在未来的新画面将得到补充。在C#中,我需要创建一个字符串,与这些网名的数组。我们怎样才能做到这一点(记住,在code需要,即使我们增加新的屏幕工作)?

注1:我在寻找,做的方法的不可以使用自定义配置

注2:我会有最大的有起屏幕这个名字10配置项。但我有10,000个其他配置项。

参考

  1. <一个href="http://stackoverflow.com/questions/11778979/better-$c$c-pattern-for-checking-existence-of-value">Better code /模式用于检查存在的价值
  2. 通用读取配置部分方法
  3. 生成号码列表在C#
  4. <一个href="http://stackoverflow.com/questions/3844611/c-detecting-sequence-of-at-least-3-sequential-numbers-from-a-given-list">C#:至少检测3连续数字的顺序,从给定的列表
  5. 是否LINQ缓存计算的值?
  6. <一个href="http://stackoverflow.com/questions/2731384/is-there-an-easy-method-to-combine-two-relative-paths-in-c">Is有一个简单的方法来组合两个相对路径在C#?
解决方案

  ConfigurationManager.AppSettings.AllKeys
    。凡(键=&GT; key.StartsWith(屏幕))
    。选择(键=&GT; ConfigurationManager.AppSettings [关键])
 

如果你有很多的设置(例如,10K,就像你在评论中规定的),你可以从一个事实,即AppSettings的集合进行查找通过关键字优化中获益。对于这一点,你必须反复尝试屏蔽1,画面2,屏幕3等,并在未找到任何值停止:

  Enumerable.Range(1,int.MaxValue)
    。选择(I =&GT; ConfigurationManager.AppSettings [屏幕+ I])
    .TakeWhile(值=&GT;值!= NULL)
 

这个方法,但是,正是那种premature优化的克努特先生警告我们。配置文件中根本不应该含有许多设置,期。

另一个缺点:记住,这种方法假定有在屏幕*设置编号之间没有间隙。也就是说,如果你有屏蔽1,画面2,和Screen4,也不会拾取最后一个。如果你计划在有很多这样的设置,它会变得非常不方便移动的所有每次添加或删除设置的数字。

I have config values as shown below

add key="Screen1" value ="Admin"
add key="Screen2" value ="Log"

In future new screens will get added. In C#, I need to create an array of string with these screen names. How can we do this (keeping in mind that the code need to work even if we add new screens)?

Note 1: I am looking for an approach that does not use custom configuration.

Note 2: I will have maximum of 10 config items with the name starting as "Screen". But I will have 10,000 other config items.

REFERENCE

  1. Better code/ Pattern for checking existence of value
  2. Generic method for reading config sections
  3. Generating numbers list in C#
  4. C#: Detecting sequence of at least 3 sequential numbers from a given list
  5. Does LINQ cache computed values?
  6. Is there an easy method to combine two relative paths in C#?

解决方案

ConfigurationManager.AppSettings.AllKeys
    .Where( key => key.StartsWith( "Screen" ) )
    .Select( key => ConfigurationManager.AppSettings[key] )

If you have a lot of settings (say, 10K, like you specified in the comments), you may benefit from the fact that the AppSettings collection is optimized for lookup by key. For this, you'll have to repeatedly try "Screen1", "Screen2", "Screen3", etc., and stop when no value is found:

Enumerable.Range( 1, int.MaxValue )
    .Select( i => ConfigurationManager.AppSettings[ "Screen" + i ] )
    .TakeWhile( value => value != null )

This approach, however, is exactly the kind of "premature optimization" that Mr. Knuth warned us about. The config file simply shouldn't contain that many settings, period.

Another disadvantage: keep in mind that this approach assumes that there are no gaps in the numbering of "Screen*" settings. That is, if you have "Screen1", "Screen2", and "Screen4", it will not pickup the last one. If you're planning on having a lot of these settings, it will become very inconvenient to "shift" all the numbers every time you add or remove a setting.

这篇关于可扩展的C#code从配置文件创建数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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