简体   繁体   中英

Why does this collection seem to contain objects?

I have a class "ImageElementCollection : ConfigurationElementCollection" containing elements of a class "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:

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.

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?

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>.

Furthermore, it is possible to fix that problem by slightly adjusting the Configuration Handler. Simply add the IEnumerable interface as follows...

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM