ConfigurationElementCollection和Linq

18 浏览
0 Comments

ConfigurationElementCollection和Linq

我编写了一些自定义的配置集合、元素等。现在,我想做一个简单的Linq语句:

ServerDetails servers = ConfigurationManager.GetSection("serverDetails") as ServerDetails;
var server = from s in servers
             where s.Name == serverName
             select s;

我收到了错误信息:

找不到源类型 'MyNamespace.ServerDetails' 的查询模式实现。找不到 'Where'。

ServerElement 有两个属性:

public class ServerElement : ConfigurationElement
{
    [ConfigurationProperty("ip")]
    public string IP
    {
        get { return (string)base["ip"]; }
        set { base["ip"] = value; }
    }
    [ConfigurationProperty("name", IsKey = true, IsRequired = true)]
    public string Name
    {
        get { return (string)base["name"]; }
        set { base["name"] = value; }
    }
}

ServerDetails

public sealed class ServerDetails : ConfigurationSection
{
    [ConfigurationProperty("ServerCollection")]
    [ConfigurationCollection(typeof(ServerCollection), AddItemName = "add")]
    public ServerCollection ServerCollection
    {
        get { return this["ServerCollection"] as ServerCollection; }
    }
}

ServerCollection

public sealed class ServerCollection : ConfigurationElementCollection
{
    public void Add(ServerElement ServerElement)
    {
        this.BaseAdd(ServerElement);
    }
    public override ConfigurationElementCollectionType CollectionType
    {
        get { return ConfigurationElementCollectionType.AddRemoveClearMap; }
    }
    protected override ConfigurationElement CreateNewElement()
    {
        return new ServerElement();
    }
    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((ServerElement)element).Name;
    }
}

我漏掉了什么吗?我需要添加些什么才能在自定义配置元素中使用Linq?顺便说一下,我已经在同一个类中定义了 using System.Linq;

0