1

我有一个自定义ConfigurationElementCollection调用EnvironmentElementCollection来配置我正在编写的类库中的不同环境。我在一个类中有一个静态属性来获取该部分中列出的所有环境的集合。包含项目 ( 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];
        }
    }
}

这是我获取环境的课程:

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 查询或其他什么,我无法判断哪个部分正在破坏。

4

0 回答 0