从 ConfigurationElementCollection 获取配置元素
我(希望)已经设置了我自己设计的 ConfigurationElementCollection,并以电子邮件作为键。现在怎么办?其实网上很难找到。我如何:
迭代它?
查看特定元素是否存在?
获取特定元素?
...给出:
YourConfigElement config =
ConfigurationManager.GetSection("YourSectionName") as YourConfigElement;
部分答案
1.
foreach (X x in config.XCollection)
<code here>
2 .将“此处的代码”替换为
{
if (x.Y == needle)
{
hasIndeed = true;
break;
}
}
3 。将“此处的代码”替换为“
{ if (x.Y == needle)
cameUpWith = x;
break;
}
微小的气味”。
I have (hopefully) setup ConfigurationElementCollection of my own design with emails as keys. Now what? Hard to find actually on the web. How do I:
iterate through it?
See if a specific element exists?
get a specific element?
...given:
YourConfigElement config =
ConfigurationManager.GetSection("YourSectionName") as YourConfigElement;
Partial answer
1.
foreach (X x in config.XCollection)
<code here>
2 . replace "code here" with
{
if (x.Y == needle)
{
hasIndeed = true;
break;
}
}
3 . replace "code here" with
{ if (x.Y == needle)
cameUpWith = x;
break;
}
Tiny odor.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您想要的是自己的通用
ConfigurationElementCollection
基类,它实现了IList
。然后,您可以为所有配置集合继承此集合,并减少创建配置集合时需要执行的工作量。再多做一点工作,您也可以拥有一个字典集。
What you want is your own generic
ConfigurationElementCollection
base class which implementsIList<T>
. You can then inherit from this for all your configuration collections and cut down on the amount of work you need to do when creating configuration collections.With a little bit more work you can have a dictionary collection as well.
我不完全理解你的问题是什么 - 但基本上,如果你有一个自定义配置元素,你应该能够使用以下内容从配置文件中检索它:
一旦你有了配置元素,你就可以用它做任何事情你喜欢 - 你可以实现你要求的所有这些 - 检查元素是否存在,获取特定元素等。
你还应该查看 CodeProject 上 Jon Rista 关于 .NET 2.0 配置的三部分系列,以获取更多信息 - 也许那些文章将帮助您解锁配置“挑战”;-)
强烈推荐,写得很好,非常有帮助!
如果您还没有发现它 - Codeplex 上有一个出色的配置部分设计器,它可以直观地设计配置快速编辑部分和集合,并为您编写所有粘稠的粘合代码 - 确实非常方便!
马克
I don't totally understand what your issues are - but basically, if you have a custom configuration element, you should be able to retrieve that from the config file using something like:
Once you have your configuration element, you can do with it whatever you like - you can implement all those things you asked for - check existance of an element, get a specific element etc.
You should also check out Jon Rista's three-part series on .NET 2.0 configuration up on CodeProject for more information - maybe those articles will help you unlock your config "challenge" ;-)
Highly recommended, well written and extremely helpful!
And if you haven't discovered it already - there's an excellent Configuration Section Designer up on Codeplex which makes visually designing configuration sections and collections a snap and writes all the gooey glue code for you - very handy indeed!
Marc