C# 从ConfigurationElementCollection获取配置元素
我已经(希望)设置了我自己设计的ConfigurationElement集合,并将电子邮件作为密钥。现在怎么办?很难在网上找到。我如何:C# 从ConfigurationElementCollection获取配置元素,c#,system.configuration,configurationelement,C#,System.configuration,Configurationelement,我已经(希望)设置了我自己设计的ConfigurationElement集合,并将电子邮件作为密钥。现在怎么办?很难在网上找到。我如何: 反复浏览它 查看是否存在特定元素 获取特定元素 …鉴于: YourConfigElement config = ConfigurationManager.GetSection("YourSectionName") as YourConfigElement; 部分答案 一, 三,。将“此处代码”替换为 微小的气味。我不完全理解您的问题是什么-但
YourConfigElement config =
ConfigurationManager.GetSection("YourSectionName") as YourConfigElement;
部分答案
一,
三,。将“此处代码”替换为
微小的气味。我不完全理解您的问题是什么-但基本上,如果您有一个自定义配置元素,您应该能够使用以下内容从配置文件中检索该元素:
YourConfigElement config =
ConfigurationManager.GetSection("YourSectionName") as YourConfigElement ;
一旦您拥有了配置元素,您就可以随心所欲地使用它——您可以实现所有您要求的事情——检查元素的存在性,获取特定元素等等
您还应该查看Jon Rista在CodeProject上的.NET 2.0配置上的三部分系列文章,以了解更多信息-也许这些文章将帮助您解锁配置“挑战”;-)
Marc您需要的是您自己的通用
ConfigurationElementCollection
基类,它实现了IList
。然后,您可以从中继承所有配置集合,并减少创建配置集合时所需的工作量
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);
}
}
}
公共抽象类BaseConfigurationElementCollection:ConfigurationElementCollection,IList其中TConfigurationElementType:ConfigurationElement,IConfigurationElementCollectionElement,new()
{
受保护的覆盖ConfigurationElement CreateNewElement()
{
返回新的TConfigurationElementType();
}
受保护的覆盖对象GetElementKey(ConfigurationElement元素)
{
返回((TConfigurationElementType)元素).ElementKey;
}
公共IEnumerator GetEnumerator()
{
foreach(此中的TConfigurationElement类型)
{
收益型;
}
}
公共无效添加(TConfigurationElementType configurationElement)
{
BaseAdd(configurationElement,true);
}
公共空间清除()
{
BaseClear();
}
公共bool包含(tconfigurationelement类型configurationElement)
{
返回!(IndexOf(configurationElement)<0);
}
public void CopyTo(TConfigurationElementType[]数组,int-arrayIndex)
{
base.CopyTo(数组,arrayIndex);
}
公共bool删除(tconfigurationelement类型configurationElement)
{
BaseRemove(GetElementKey(configurationElement));
返回true;
}
bool ICollection.IsReadOnly
{
获取{return IsReadOnly();}
}
public int IndexOf(tconfigurationelement类型configurationElement)
{
返回BaseIndexOf(配置元素);
}
public void Insert(int索引,tconfigurationelement类型configurationElement)
{
BaseAdd(索引、配置元素);
}
公共无效删除(整数索引)
{
BaseRemoveAt(索引);
}
公共TConfigurationElement键入此[int索引]
{
得到
{
返回(TConfigurationElementType)BaseGet(索引);
}
设置
{
if(BaseGet(index)!=null)
{
BaseRemoveAt(索引);
}
BaseAdd(索引、值);
}
}
}
再多做一点工作,你就可以拥有一本字典了。谢谢你的回复。对我来说,最难的部分是收集类型。我已经解决了#1,但如果您正在寻找一把钥匙,似乎应该有一种更漂亮、更快的方法。我来看看你的链接。我真的很开心。您推荐的页面不是我访问过的第一个类似内容。到目前为止,我已经熟练地定义了集合类型,在配置中添加了元素,但实际上在代码中使用了集合。它公开了AsQueryable()方法。我相信这是一个线索,但是queryable反过来不会公开任何有用的东西,例如get或contains。
{ if (x.Y == needle)
cameUpWith = x;
break;
}
YourConfigElement config =
ConfigurationManager.GetSection("YourSectionName") as YourConfigElement ;
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);
}
}
}