Linq查询返回null,并使用自定义ConfigurationElementCollection实现IEnumerable

时间:2013-08-14 17:44:06

标签: c# asp.net linq configuration

我有一个名为ConfigurationElementCollection的自定义EnvironmentElementCollection来配置我正在编写的类库中的不同环境。我在类中有一个静态属性来获取该部分中列出的所有环境的集合。包含的项(EnvironmentElement)的属性指示环境是“登录”环境。我的目标是能够使用Linq过滤集合以仅获取“登录”环境,但Linq查询始终返回null。我使用this教程实现了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];
        }
    }
}

以下是我的课程:

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;
        }
    }
}

所以我无法判断哪个部分正在破坏,如果我的IEnumerable实现不好,或者我的Linq查询,或其他什么。

0 个答案:

没有答案
相关问题