简体   繁体   English

为什么这个集合似乎包含对象?

[英]Why does this collection seem to contain objects?

I have a class "ImageElementCollection : ConfigurationElementCollection" containing elements of a class "ImageElement : ConfigurationElement". 我有一个类“ImageElementCollection:ConfigurationElementCollection”包含类“ImageElement:ConfigurationElement”的元素。

Using the advice of some other very intelligent persons here on StackOverflow, I have figured out how to make use of these items in my program: 在StackOverflow上使用其他一些非常聪明的人的建议,我已经想出如何在我的程序中使用这些项目:

MonitorConfig Config = (MonitorConfig)ConfigurationManager.GetSection("MonitorConfig");

However, when I attempt to access the items in this collection... 但是,当我尝试访问此集合中的项目时...

foreach (var image in Config.Images) Debug.WriteLine(image.Name);

...I end up with squiggly lines under the Name property because "image" has been declared as an object rather than as an ImageElement in spite of my best efforts. ...我最终在Name属性下使用了波浪线,因为尽管我付出了最大的努力,“image”已被声明为对象而不是ImageElement。

Is this something I'm doing wrong in my declarations, or is this just something everyone just deals with by exchanging "var" for "ImageElement" in that foreach up there? 这是我在我的声明中做错了什么,或者这只是每个人只需通过在那个foreach中交换“var”for“ImageElement”来处理的事情?

Code for configuration handler found below: 配置处理程序的代码如下:

public class MonitorConfig : ConfigurationSection
{
    [ConfigurationProperty("Frequency", DefaultValue = 5D, IsRequired = false)]
    public double Frequency
    {
        get { return (double)this["Frequency"]; }
    }

    [ConfigurationProperty("Images", IsRequired = false)]
    public ImageElementCollection Images
    {
        get { return (ImageElementCollection)this["Images"]; }
    }
}

[ConfigurationCollection(typeof(ImageElement), AddItemName = "Image")]
public class ImageElementCollection : ConfigurationElementCollection
{
    public ImageElement this[object elementKey]
    {
        get { return (ImageElement)BaseGet(elementKey); }
    }

    public void Add(ImageElement element)
    {
        base.BaseAdd(element);
    }

    protected override ConfigurationElement CreateNewElement()
    {
        return new ImageElement();
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((ImageElement)element).Name;
    }
}

public class ImageElement : ConfigurationElement
{
    [ConfigurationProperty("Name", IsRequired = true, IsKey = true)]
    public string Name 
    { 
        get { return (string)this["Name"]; }
    }
}

Andrew Kennan provided the answer in the comments above: this collection seems to contain only objects because it doesn't implement IEnumerable<T>. Andrew Kennan在上面的评论中提供了答案:这个集合似乎只包含对象,因为它没有实现IEnumerable <T>。

Furthermore, it is possible to fix that problem by slightly adjusting the Configuration Handler. 此外,可以通过稍微调整配置处理程序来解决该问题。 Simply add the IEnumerable interface as follows... 只需添加IEnumerable接口,如下所示......

public class ImageElementCollection : ConfigurationElementCollection, IEnumerable<ImageElement>

...And then stick a method somewhat like this in the body of the class: ...然后在类的主体中粘贴一个类似这样的方法:

public new IEnumerator<ImageElement> GetEnumerator()
{
    var iter = base.GetEnumerator();
    while (iter.MoveNext()) yield return (ImageElement)iter.Current;
}

Thank you, Andrew. 谢谢你,安德鲁。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Task 对象内部是否包含 ContinueWith 任务的集合? - Does Task objects internally contain a collection of ContinueWith tasks? 检查每个包含另一个对象的对象的集合是否包含List的所有值 <string> 通过LINQ - check if collection of objects where each contain another collection of objects contain all values of List<string> via LINQ 实体框架数据上下文不包含我使用 inheritance 的实体的集合。 为什么? - Entity framework data context does not contain a collection of my entity that uses inheritance. Why? dll似乎没有被阅读,为什么? - Dll does not seem to be read, why? 测试集合不包含多个成员 - Test collection does not contain several members 网站集上的列查询不包含内容 - Lookup to column on site collection does not contain contents 带有对象的组合框selectedItem…似乎不起作用 - Combo box selectedItem with objects… does not seem to work C# HashSet 不仅包含该类型的唯一对象。 为什么? - C# HashSet does not contain only unique objects of the type. Why? 如果集合还没有通过比较项目的属性将项目添加到集合中? - Add items to a collection if the collection does NOT already contain it by comparing a property of the items? 断言一个集合不包含另一个集合中的任何项目 - Assert that a collection does not contain any item in another collection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM