使用DataContractSerializer
我想序列化从A类继承的对象列表。这些对象在不同的程序集中,让我们说它们属于B,C和D类。我添加了B,C和D到已知类型的数据契约序列化器。我能够序列化列表,但序列化的结果如下:
<SerializedListObjects>
<A i:type="B">
<A i:type="C">
</SerializedListObjects>
我想要的是:
<SerializedListObjects>
<B>
<C>
</SerializedListObjects>
B和C中可能存在一些属性,其中包含那些从A继承的信息。
这是我的基类:
[Serializable]
[DataContract(Name = "A")]
public abstract class A
{
}
这是派生类定义的例子。
[Serializable]
[DataContract(Name = "B")]
public class B : A
{
}
由于派生类在不同的程序集中,我不能将任何属性放在它们的基类或包含派生类名的序列化类中(例如[XmlElement("B", Type = typeof(ChildB))]
) - 我不&# 39; t可以访问那里的派生类。
有可能吗?
当我正在使用DataContractSerializer
时,如果有必要,我愿意切换到另一个XML序列化程序,例如XmlSerializer
。
答案 0 :(得分:1)
首先,DataContractSerializer
没有通过更改集合元素名称来支持集合项多态的机制。它仅支持使用known type mechanism属性的i:type
- 您指出这是不可接受的。
由于您愿意切换到XmlSerializer
,您可以使用属性XmlArrayItemAttribute.Type
为列表中的多态类型指定元素名称:
public class AListObject
{
[XmlArrayItem(typeof(B))]
[XmlArrayItem(typeof(C))]
public List<A> SerializedListObjects { get; set; }
}
但是,您还指出无法在编译类型中静态声明多态子类型,因为它们存在于某些其他程序集中。
因此,您需要使用XmlAttributeOverrides
机制为运行时中的所有List<A>
属性指定所有可能的派生类型,并使用这些覆盖手动construct an XmlSerializer
。
这是一个原型解决方案。首先,假设您有一个根对象引用包含List<A>
的对象,如下所示:
public class RootObject
{
public AListObject AList { get; set; }
}
public class AListObject
{
public List<A> SerializedListObjects { get; set; }
}
(根对象可以是具有List<A>
属性的对象,但不一定是。)我们还假设您知道可能包含AListObject
的所有此类对象,例如List<A>
}属性。
根据这些假设,可以使用以下序列化工厂为任何根对象生成XmlSerializer
,该对象可以引用包含List<A>
属性的已知类型的任何实例:
public interface IXmlSerializerFactory
{
XmlSerializer CreateSerializer(Type rootType);
}
public static class AListSerializerFactory
{
static readonly XmlArrayItemTypeOverrideSerializerFactory instance;
static AListSerializerFactory()
{
// Include here a list of all types that have a List<A> property.
// You could use reflection to find all such public types in your assemblies.
var declaringTypeList = new []
{
typeof(AListObject),
};
// Include here a list of all base types with a corresponding mapping
// to find all derived types in runtime. Here you could use reflection
// to find all such types in your assemblies, as shown in
// https://stackoverflow.com/questions/857705/get-all-derived-types-of-a-type
var derivedTypesList = new Dictionary<Type, Func<IEnumerable<Type>>>
{
{ typeof(A), () => new [] { typeof(B), typeof(C) } },
};
instance = new XmlArrayItemTypeOverrideSerializerFactory(declaringTypeList, derivedTypesList);
}
public static IXmlSerializerFactory Instance { get { return instance; } }
}
public class XmlArrayItemTypeOverrideSerializerFactory : IXmlSerializerFactory
{
// To avoid a memory & resource leak, the serializers must be cached as explained in
// https://stackoverflow.com/questions/23897145/memory-leak-using-streamreader-and-xmlserializer
readonly object padlock = new object();
readonly Dictionary<Type, XmlSerializer> serializers = new Dictionary<Type, XmlSerializer>();
readonly XmlAttributeOverrides overrides;
public XmlArrayItemTypeOverrideSerializerFactory(IEnumerable<Type> declaringTypeList, IEnumerable<KeyValuePair<Type, Func<IEnumerable<Type>>>> derivedTypesList)
{
var completed = new HashSet<Type>();
overrides = declaringTypeList
.SelectMany(d => derivedTypesList.Select(p => new { declaringType = d, itemType = p.Key, derivedTypes = p.Value() }))
.Aggregate(new XmlAttributeOverrides(), (a, d) => a.AddXmlArrayItemTypes(d.declaringType, d.itemType, d.derivedTypes, completed));
}
public XmlSerializer CreateSerializer(Type rootType)
{
lock (padlock)
{
XmlSerializer serializer;
if (!serializers.TryGetValue(rootType, out serializer))
serializers[rootType] = serializer = new XmlSerializer(rootType, overrides);
return serializer;
}
}
}
public static partial class XmlAttributeOverridesExtensions
{
public static XmlAttributeOverrides AddXmlArrayItemTypes(this XmlAttributeOverrides overrides, Type declaringType, Type itemType, IEnumerable<Type> derivedTypes)
{
return overrides.AddXmlArrayItemTypes(declaringType, itemType, derivedTypes, new HashSet<Type>());
}
public static XmlAttributeOverrides AddXmlArrayItemTypes(this XmlAttributeOverrides overrides, Type declaringType, Type itemType, IEnumerable<Type> derivedTypes, HashSet<Type> completedTypes)
{
if (overrides == null || declaringType == null || itemType == null || derivedTypes == null || completedTypes == null)
throw new ArgumentNullException();
XmlAttributes attributes = null;
for (; declaringType != null && declaringType != typeof(object); declaringType = declaringType.BaseType)
{
// Avoid duplicate overrides.
if (!completedTypes.Add(declaringType))
break;
foreach (var property in declaringType.GetProperties(BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Instance))
{
// Skip the property if already ignored
if (property.IsDefined(typeof(XmlIgnoreAttribute), false))
continue;
// See if it is a list property, and if so, get its type.
var propertyItemType = property.PropertyType.GetListType();
if (propertyItemType == null)
continue;
// OK, its a List<itemType>. Add all the necessary XmlElementAttribute declarations.
if (propertyItemType == itemType)
{
if (attributes == null)
{
attributes = new XmlAttributes();
foreach (var derivedType in derivedTypes)
// Here we are assuming all the derived types have unique XML type names.
attributes.XmlArrayItems.Add(new XmlArrayItemAttribute { Type = derivedType });
if (itemType.IsConcreteType())
attributes.XmlArrayItems.Add(new XmlArrayItemAttribute { Type = itemType });
}
overrides.Add(declaringType, property.Name, attributes);
}
}
}
return overrides;
}
}
public static class TypeExtensions
{
public static bool IsConcreteType(this Type type)
{
return !type.IsAbstract && !type.IsInterface;
}
public static Type GetListType(this Type type)
{
while (type != null)
{
if (type.IsGenericType)
{
var genType = type.GetGenericTypeDefinition();
if (genType == typeof(List<>))
return type.GetGenericArguments()[0];
}
type = type.BaseType;
}
return null;
}
}
然后,您可以按如下方式对RootObject
的实例进行序列化和反序列化,如下所示:
var root = new RootObject
{
AList = new AListObject
{
SerializedListObjects = new List<A> { new B(), new C() },
},
};
var serializer = AListSerializerFactory.Instance.CreateSerializer(root.GetType());
var xml = root.GetXml(serializer);
var root2 = xml.LoadFromXml<RootObject>(serializer);
使用扩展方法:
public static class XmlSerializationHelper
{
public static T LoadFromXml<T>(this string xmlString, XmlSerializer serial = null)
{
serial = serial ?? new XmlSerializer(typeof(T));
using (var reader = new StringReader(xmlString))
{
return (T)serial.Deserialize(reader);
}
}
public static string GetXml<T>(this T obj, XmlSerializer serializer = null)
{
using (var textWriter = new StringWriter())
{
var settings = new XmlWriterSettings() { Indent = true }; // For cosmetic purposes.
using (var xmlWriter = XmlWriter.Create(textWriter, settings))
(serializer ?? new XmlSerializer(obj.GetType())).Serialize(xmlWriter, obj);
return textWriter.ToString();
}
}
}
结果是:
<RootObject xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<AList>
<SerializedListObjects>
<B />
<C />
</SerializedListObjects>
</AList>
</RootObject>
注意:
如 Memory Leak using StreamReader and XmlSerializer 中所述,您必须静态缓存由XmlSerializer
构造的任何XmlAttributeOverrides
,以避免严重的内存泄漏。 documentation建议使用Hashtable
,但XmlAttributeOverrides
不会覆盖Equals()
或GetHashCode()
,并且无法为应用程序开发人员提供足够的内部数据访问权限写自己的。因此,只要使用XmlAttributeOverrides
,就必须手工制作某种静态缓存方案。
鉴于查找和覆盖所有XmlArrayItem
属性的List<A>
属性的复杂性,您可能会考虑坚持使用现有的i:type
机制。它很简单,效果很好,得到DataContractSerializer
和XmlSerializer
的支持,并且是standard。
我用通用方式编写了类XmlArrayItemTypeOverrideSerializerFactory
,这增加了明显的复杂性。
工作样本.Net小提琴here。