最后这对我有用。也许它可以帮助遇到同样问题的其他人。我将发布完整的代码以保持简单。它可能不是一流的解决方案,但它正在发挥作用。如果其他人可以看看它并提出更好的方法,我将不胜感激。
这篇文章帮助我让它工作的是:https ://www.codeproject.com/Articles/16466/Unraveling-the-Mysteries-of-NET-2-0-Configuration
我的代码缺少元素集合(请参阅 ThingElement.cs)。
东西.config
<configuration>
<configSections>
<section name="ExampleSection" type="ConsoleApplication1.Things.ExampleSection, ConsoleApplication1" />
</configSections>
<ExampleSection>
<things>
<thing type="one" name="one-1" color="green" />
<thing type="one" name="one-2" color="red" />
<thing type="two" name="two-1" />
</things>
</ExampleSection>
</configuration>
ExampleSection.cs
public class ExampleSection : ConfigurationSection
{
static ExampleSection() { }
[ConfigurationProperty("things")]
[ConfigurationCollection(typeof(ThingElement), AddItemName = "thing",
CollectionType = ConfigurationElementCollectionType.BasicMap)]
public ExampleThingElementCollection Things
{
get { return (ExampleThingElementCollection)this["things"]; }
set { this["things"] = value; }
}
}
ExampleThingElementCollection.cs
[ConfigurationCollection(typeof(ThingElement), AddItemName = "thing",
CollectionType = ConfigurationElementCollectionType.BasicMap)]
public class ExampleThingElementCollection : ConfigurationElementCollection
{
#region Constructors
public ExampleThingElementCollection()
{
}
#endregion
#region Properties
public override ConfigurationElementCollectionType CollectionType
{
get { return ConfigurationElementCollectionType.BasicMap; }
}
protected override string ElementName
{
get { return "thing"; }
}
#endregion
#region Indexers
public ThingElement this[int index]
{
get { return (ThingElement)base.BaseGet(index); }
set
{
if (base.BaseGet(index) != null)
{
base.BaseRemoveAt(index);
}
base.BaseAdd(index, value);
}
}
new public ThingElement this[string name]
{
get { return (ThingElement)base.BaseGet(name); }
}
#endregion
#region Overrides
protected override ConfigurationElement CreateNewElement()
{
return new ThingElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return (element as ThingElement).Name;
}
#endregion
#region Methods
public void Add(ThingElement thing)
{
base.BaseAdd(thing);
}
public void Remove(string name)
{
base.BaseRemove(name);
}
public void Remove(ThingElement thing)
{
base.BaseRemove(GetElementKey(thing));
}
public void Clear()
{
base.BaseClear();
}
public void RemoveAt(int index)
{
base.BaseRemoveAt(index);
}
public string GetKey(int index)
{
return (string)base.BaseGetKey(index);
}
#endregion
}
ThingElement.cs(这个类作为代理元素)
public class ThingElement : ConfigurationElement
{
#region Constructors
/// <summary>
/// Predefines the valid properties and prepares
/// the property collection.
/// </summary>
static ThingElement()
{
// Predefine properties here
s_propName = new ConfigurationProperty(
"name",
typeof(string),
null,
ConfigurationPropertyOptions.IsRequired
);
}
#endregion
#region Static Fields
private static ConfigurationProperty s_propName;
private static Dictionary<string, SpecialThing> elements = new Dictionary<string, SpecialThing>();
#endregion
#region Properties
/// <summary>
/// Gets the Name setting.
/// </summary>
[ConfigurationProperty("name", IsRequired = true)]
public string Name
{
get { return (string)base[s_propName]; }
}
public SpecialThing Thing { get { return elements[Name]; } }
protected override bool SerializeElement(XmlWriter writer, bool serializeCollectionKey)
{
return Thing.ProxySerializeElement(writer, serializeCollectionKey);
}
protected override void DeserializeElement(XmlReader reader, bool serializeCollectionKey)
{
SpecialThing obj = null;
string name = reader.GetAttribute("name");
string type = reader.GetAttribute("type");
switch (type)
{
case "one":
obj = new One();
break;
case "two":
obj = new Two();
break;
default:
throw new ArgumentException(string.Format("Invalid type: {0}", type));
}
base[s_propName] = name;
if (!elements.ContainsKey(name))
elements.Add(name, obj);
obj.ProxyDeserializeElement(reader, serializeCollectionKey);
}
private Hashtable attributes;
public Hashtable Attributes
{
get
{
if (attributes == null)
attributes = new Hashtable(StringComparer.OrdinalIgnoreCase);
return attributes;
}
}
protected override bool OnDeserializeUnrecognizedAttribute(String name, String value)
{
Attributes.Add(name, value);
return true;
}
protected override void PreSerialize(XmlWriter writer)
{
if (attributes != null)
{
IDictionaryEnumerator e = attributes.GetEnumerator();
while (e.MoveNext())
{
string xmlValue = (string)e.Value;
string xmlName = (string)e.Key;
if ((xmlValue != null) && (writer != null))
{
writer.WriteAttributeString(xmlName, xmlValue);
}
}
}
}
#endregion
}
SpecialThing.cs(父类,例如基类,如果你有其他派生的)
public class SpecialThing : ConfigurationElement
{
[ConfigurationProperty("name", IsRequired = true)]
public string Name
{
get
{
return (string)this["name"];
}
set
{
this["name"] = value;
}
}
[ConfigurationProperty("type", IsRequired = true)]
public string Type
{
get
{
return (string)this["type"];
}
set
{
this["type"] = value;
}
}
public virtual bool ProxySerializeElement(XmlWriter writer, bool serializeCollectionKey)
{
return SerializeElement(writer, serializeCollectionKey);
}
public void ProxyDeserializeElement(XmlReader reader, bool serializeCollectionKey)
{
DeserializeElement(reader, serializeCollectionKey);
}
}
One.cs(父类,例如基类,如果你有其他派生的
public class One : SpecialThing
{
public One() { }
public One(string name, string type, string color)
{
base.Name = name;
base.Type = type;
Color = color;
}
[ConfigurationProperty("color")]
public string Color
{
get { return (string)this["color"]; }
set { this["color"] = value; }
}
}
二.cs
public class Two : SpecialThing
{
public Two() { }
public Two(string name, string type)
{
base.Name = name;
base.Type = type;
}
}