C# Linq查询返回null,自定义ConfigurationElementCollection实现IEnumerable
我有一个名为C# Linq查询返回null,自定义ConfigurationElementCollection实现IEnumerable,c#,asp.net,linq,configuration,C#,Asp.net,Linq,Configuration,我有一个名为EnvironmentElementCollection的自定义ConfigurationElementCollection来配置我正在编写的类库中的不同环境。我在一个类中有一个静态属性来获取该部分中列出的所有环境的集合。所包含项的属性(EnvironmentElement)指示该环境是“登录”环境。我的目标是能够使用Linq过滤集合,以仅获取“登录”环境,但Linq查询始终返回null。我使用教程实现了IEnumerable,但我不知道我做错了什么 配置类 public class
EnvironmentElementCollection
的自定义ConfigurationElementCollection
来配置我正在编写的类库中的不同环境。我在一个类中有一个静态属性来获取该部分中列出的所有环境的集合。所包含项的属性(EnvironmentElement
)指示该环境是“登录”环境。我的目标是能够使用Linq过滤集合,以仅获取“登录”环境,但Linq查询始终返回null。我使用教程实现了IEnumerable
,但我不知道我做错了什么
配置类
public class EnvironmentSection : ConfigurationSection {
private static ConfigurationPropertyCollection properties;
private static ConfigurationProperty propEnvironments;
static EnvironmentSection() {
propEnvironments = new ConfigurationProperty(null, typeof(EnvironmentElementCollection), null, ConfigurationPropertyOptions.IsDefaultCollection);
properties = new ConfigurationPropertyCollection { propEnvironments };
}
protected override ConfigurationPropertyCollection Properties {
get {
return properties;
}
}
public EnvironmentElementCollection Environments {
get {
return this[propEnvironments] as EnvironmentElementCollection;
}
}
}
public class EnvironmentElementCollection : ConfigurationElementCollection, IEnumerable<EnvironmentElement> {
private static ConfigurationPropertyCollection properties;
public EnvironmentElementCollection() {
properties = new ConfigurationPropertyCollection();
}
protected override ConfigurationPropertyCollection Properties {
get {
return properties;
}
}
public override ConfigurationElementCollectionType CollectionType {
get {
return ConfigurationElementCollectionType.BasicMap;
}
}
public EnvironmentElement this[int index] {
get {
return (EnvironmentElement)base.BaseGet(index);
}
}
public EnvironmentElement this[string name] {
get {
return (EnvironmentElement)base.BaseGet(name);
}
}
protected override string ElementName {
get {
return "add";
}
}
protected override ConfigurationElement CreateNewElement() {
return new EnvironmentElement();
}
protected override object GetElementKey(ConfigurationElement element) {
var elm = element as EnvironmentElement;
if (elm == null) throw new ArgumentNullException();
return elm.Name;
}
//for implementing IEnumerable
public new IEnumerator<EnvironmentElement> GetEnumerator() {
int count = base.Count;
for (int i = 0; i < count; i++) {
yield return (EnvironmentElement)base.BaseGet(i);
}
}
}
public class EnvironmentElement : ConfigurationElement {
private static ConfigurationPropertyCollection properties;
private static ConfigurationProperty propLoginEnabled;
public EnvironmentElement() {
propLoginEnabled = new ConfigurationProperty("loginEnabled", typeof(bool), null, ConfigurationPropertyOptions.None);
properties = new ConfigurationPropertyCollection { propLoginEnabled };
}
[ConfigurationProperty("loginEnabled")]
public bool LoginEnabled {
get {
return (bool)base[propLoginEnabled];
}
}
}
公共类环境部分:配置部分{
私有静态配置属性集合属性;
私有静态配置属性属性环境;
静态环境节(){
propEnvironments=新配置属性(null,typeof(EnvironmentElementCollection),null,ConfigurationPropertyOptions.IsDefaultCollection);
properties=新配置PropertyCollection{PropertenEnvironments};
}
受保护的覆盖配置属性集合属性{
得到{
归还财产;
}
}
公共环境元素集合环境{
得到{
将此[PropertenEnvironments]作为EnvironmentElementCollection返回;
}
}
}
公共类EnvironmentElementCollection:ConfigurationElementCollection,IEnumerable{
私有静态配置属性集合属性;
公共环境元素集合(){
properties=新配置PropertyCollection();
}
受保护的覆盖配置属性集合属性{
得到{
归还财产;
}
}
公共覆盖配置ElementCollectionType CollectionType{
得到{
返回ConfigurationElementCollectionType.BasicMap;
}
}
公共环境元素此[int索引]{
得到{
return(EnvironmentElement)base.BaseGet(index);
}
}
公共环境元素此[字符串名称]{
得到{
return(EnvironmentElement)base.BaseGet(name);
}
}
受保护的重写字符串ElementName{
得到{
返回“添加”;
}
}
受保护的覆盖ConfigurationElement CreateNewElement(){
返回新的EnvironmentElement();
}
受保护的覆盖对象GetElementKey(ConfigurationElement元素){
var elm=元素作为环境元素;
如果(elm==null)抛出新的ArgumentNullException();
返回elm.Name;
}
//用于实现IEnumerable
公共新IEnumerator GetEnumerator(){
int count=base.count;
for(int i=0;i
下面是我的课程,以获取环境:
public class Environment {
//Works fine to get all environments
public static EnvironmentElementCollection All {
get {
EnvironmentSection dls = ConfigurationManager.GetSection("environments") as EnvironmentSection;
return dls.Environments;
}
}
//Returns null
public static EnvironmentElementCollection Login {
get {
EnvironmentSection dls = ConfigurationManager.GetSection("environments") as EnvironmentSection;
EnvironmentElementCollection envs = dls.Environments.Where<EnvironmentElement>(e => e.LoginEnabled == true) as EnvironmentElementCollection;
return envs;
}
}
}
公共类环境{
//适用于所有环境
公共静态环境元素集合所有{
得到{
EnvironmentSection dls=ConfigurationManager.GetSection(“环境”)作为EnvironmentSection;
返回dls.Environments;
}
}
//返回空值
公共静态环境元素集合登录{
得到{
EnvironmentSection dls=ConfigurationManager.GetSection(“环境”)作为EnvironmentSection;
EnvironmentElementCollection envs=dls.Environments.Where(e=>e.LoginEnabled==true)作为EnvironmentElementCollection;
返回环境;
}
}
}
因此,如果我的IEnumerable实现不好,或是我的Linq查询,或是其他什么,我无法判断哪一部分出现了问题。这看起来像是你最后的演员阵容出现了问题。尝试将
作为EnvironmentElementCollection
删除,并将EnvironmentElementCollection环境=…
替换为变量环境=…
。它试图解析为什么类型?envs={System.Linq.Enumerable.WhereEnumerableIterator}
我想这是有意义的,尝试在as EnvironmentElementCollection
之前添加一个.ToEnumerable()
。看起来我没有这个选项。您能在Login属性中获取EnvironmentSection列表(dls)吗?