我有自定义配置设置。
public class GalleryResizeOptionsElement : ConfigurationElement
{
[ConfigurationProperty("name")]
public string Name
{
get { return (string)this["name"]; }
}
[ConfigurationProperty("width")]
public int Width
{
get { return (int)this["width"]; }
}
[ConfigurationProperty("height")]
public int Height
{
get { return (int)this["height"]; }
}
}
public class GalleryResizeOptionsCollection : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new GalleryResizeOptionsElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((GalleryResizeOptionsElement)element).Name;
}
public override ConfigurationElementCollectionType CollectionType
{
get { return ConfigurationElementCollectionType.BasicMap; }
}
/// <summary>
/// The element name of the configuration elements in the config file,
/// as set at UserCommandConfigurationConstants.ElementName
/// </summary>
protected override string ElementName
{
get { return "add"; }
}
/// <summary>
/// This is a convenience added to allow selection from the collection
/// via the commandkey as an index
/// </summary>
/// <returns></returns>
public GalleryResizeOptionsElement this[string name]
{
get { return (GalleryResizeOptionsElement)this.BaseGet(name); }
}
}
public class GalleryConfigurationSection : ConfigurationSection
{
public virtual GalleryResizeOptionsCollection ResizeOptions
{
get { return (GalleryResizeOptionsCollection) base["resizeOptions"]; }
}
}
在web.config文件中添加一个包含此xml配置的部分。
<resizeOptions>
<add name="Square" width="100" height="100" />
<add name="Rectangle" width="200" height="100" />
<add name="Hero" width="600" height="400" />
</resizeOptions>
为了简洁起见,我已经遗漏了很多其他代码,但我认为我已经包含了提出问题所需的所有内容。
如何更改GalleryResizeOptionsCollection以便我可以返回所有GalleryResizeOptionsElements的列表?或者更清楚地说,我希望能够返回所有“添加”元素的列表。
答案 0 :(得分:0)
您可以使用System.Linq
返回元素列表:
using System.Linq;
public class GalleryResizeOptionsCollection : ConfigurationElementCollection
{
//...
public IList<GalleryResizeOptionsElement> ToList()
{
return this.Cast<GalleryResizeOptionsElement>().ToList();
}
}