You could create an XmlAttributeOverridesBuilder
class, similar to XmlAttributeOverrides
, which will give you better control over the XmlAttributes it contains. That way, you could work on XmlAttributeOverridesBuilder
instances, combine them if necessary, and call a GetOverrides
method when you need the actual XmlAttributeOverrides
instance.
public class XmlAttributeOverridesBuilder
{
private Dictionary<Type, Dictionary<string, XmlAttributes>> _entries;
public XmlAttributeOverridesBuilder()
{
_entries = new Dictionary<Type, Dictionary<string, XmlAttributes>>();
}
public void Add(Type type, XmlAttributes attributes)
{
Add(type, String.Empty, attributes);
}
public void Add(Type type, string member, XmlAttributes attributes)
{
Dictionary<string, XmlAttributes> typeEntries;
if (!_entries.TryGetValue(type, out typeEntries))
{
typeEntries = new Dictionary<string, XmlAttributes>();
_entries[type] = typeEntries;
}
typeEntries[member] = attributes;
}
public XmlAttributeOverrides GetOverrides()
{
XmlAttributeOverrides overrides = new XmlAttributeOverrides();
foreach(var kvpType in _entries)
{
foreach(var kvpMember in kvpType.Value)
{
overrides.Add(kvpType.Key, kvpMember.Key, kvpMember.Value);
}
}
return overrides;
}
public XmlAttributeOverridesBuilder Combine(XmlAttributeOverridesBuilder other)
{
// combine logic to build a new XmlAttributeOverridesBuilder
// ...
}
}