[英]Getting config element from ConfigurationElementCollection
I have (hopefully) setup ConfigurationElementCollection of my own design with emails as keys. 我(希望)设置我自己的设计的ConfigurationElementCollection,电子邮件作为键。 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. 1。
foreach (X x in config.XCollection)
<code here>
2 . 2。 replace "code here" with 用“替换此处的代码”代替
{
if (x.Y == needle)
{
hasIndeed = true;
break;
}
}
3 . 3。 replace "code here" with 用“替换此处的代码”代替
{ if (x.Y == needle)
cameUpWith = x;
break;
}
Tiny odor. 微小的气味。
What you want is your own generic ConfigurationElementCollection
base class which implements IList<T>
. 你想要的是你自己的通用ConfigurationElementCollection
基类,它实现了IList<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. 然后,您可以从此继承所有配置集合,并减少创建配置集合时需要执行的工作量。
public abstract class BaseConfigurationElementCollection<TConfigurationElementType> : ConfigurationElementCollection, IList<TConfigurationElementType> where TConfigurationElementType : ConfigurationElement, IConfigurationElementCollectionElement, new()
{
protected override ConfigurationElement CreateNewElement()
{
return new TConfigurationElementType();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((TConfigurationElementType)element).ElementKey;
}
public IEnumerator<TConfigurationElement> GetEnumerator()
{
foreach (TConfigurationElement type in this)
{
yield return type;
}
}
public void Add(TConfigurationElementType configurationElement)
{
BaseAdd(configurationElement, true);
}
public void Clear()
{
BaseClear();
}
public bool Contains(TConfigurationElementType configurationElement)
{
return !(IndexOf(configurationElement) < 0);
}
public void CopyTo(TConfigurationElementType[] array, int arrayIndex)
{
base.CopyTo(array, arrayIndex);
}
public bool Remove(TConfigurationElementType configurationElement)
{
BaseRemove(GetElementKey(configurationElement));
return true;
}
bool ICollection<TConfigurationElementType>.IsReadOnly
{
get { return IsReadOnly(); }
}
public int IndexOf(TConfigurationElementType configurationElement)
{
return BaseIndexOf(configurationElement);
}
public void Insert(int index, TConfigurationElementType configurationElement)
{
BaseAdd(index, configurationElement);
}
public void RemoveAt(int index)
{
BaseRemoveAt(index);
}
public TConfigurationElementType this[int index]
{
get
{
return (TConfigurationElementType)BaseGet(index);
}
set
{
if (BaseGet(index) != null)
{
BaseRemoveAt(index);
}
BaseAdd(index, value);
}
}
}
With a little bit more work you can have a dictionary collection as well. 通过更多的工作,您也可以拥有字典集合。
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: 我不完全理解你的问题是什么 - 但基本上,如果你有一个自定义配置元素,你应该能够使用类似的东西从配置文件中检索它:
YourConfigElement config =
ConfigurationManager.GetSection("YourSectionName") as YourConfigElement ;
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" ;-) 您还应该查看Jon Rista关于CodeProject上.NET 2.0配置的三部分系列以获取更多信息 - 也许这些文章将帮助您解锁配置“挑战”;-)
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! 如果你还没有发现它 - 在Codeplex上有一个出色的Configuration Section Designer ,可以直观地设计配置部分和集合,并为你编写所有粘性代码 - 非常方便!
Marc 渣
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.