简体   繁体   English

DataContractSerializer以与KnownTypes和Generics混淆的方式运行

[英]DataContractSerializer behaves in confusing ways with KnownTypes and Generics

I'm trying to generate KnownType lists for arbitrary objects, and I'm running into problems when trying to serialize types with generic members, such as DbEntityValidationException which has a list property of type IEnumerable<DbEntityValidationResult> . 我正在尝试为任意对象生成KnownType列表,并且在尝试使用泛型成员序列化类型时遇到问题,例如DbEntityValidationException ,它具有类型为IEnumerable<DbEntityValidationResult>的list属性。

When calling the constructor for DataContractSerializer, I feed it a list of KnownTypes consisting of the following types, constructed by using reflection to grab the types of all of its properties, as well as any generic type arguments: 在调用DataContractSerializer的构造函数时,我给它提供了一个由以下类型组成的KnownTypes列表,通过使用反射来获取它的所有属性的类型,以及任何泛型类型参数:

var serializer = new DataContractSerializer(source.GetType(), knownsTypesPlusGenerics);
var stringWriter = new StringWriter(CultureInfo.InvariantCulture);
using (var xmlTextWriter = XmlWriter.Create(stringWriter))
{
    serializer.WriteObject(xmlTextWriter, source);
}

This list consists of the following types, captured via tracing: 此列表包含以下类型,通过跟踪捕获:

System.Data.Entity.Validation.DbEntityValidationException
System.Collections.Generic.List`1[[System.Data.Entity.Validation.DbEntityValidationResult, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]
System.Collections.ListDictionaryInternal
System.Data.Entity.Validation.DbEntityValidationResult

Yet, for some reason, DataContractSerializer throws on missing types, as if the type of the list property was List<object> instead of List<DbEntityValidationResult> : 但是,由于某种原因,DataContractSerializer会抛出缺少的类型,就好像list属性的类型是List<object>而不是List<DbEntityValidationResult>

System.Runtime.Serialization.SerializationException: Type 'System.Collections.Generic.List``1[[System.Object, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' with data contract name 'ArrayOfanyType:http://schemas.microsoft.com/2003/10/Serialization/Arrays' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.

Why would DataContractSerializer complain about this type when that type isn't used, and when I've provided the type that actually is? 为什么DataContractSerializer在没有使用该类型时会抱怨这种类型,当我提供实际的类型时?

It is possible that somewhere in your object graph, List<{object}> needs to be serialized. 您的对象图中的某个位置List <{object}>可能需要序列化。

DataContractSerializer sees List<{object}> as being different than List<{any other type}> so typeof(List<{object}>) must be one of the Type's passed on to the DataContractSerializer constructor. DataContractSerializer将List <{object}>视为与List <{any other type}>不同,因此typeof(List <{object}>)必须是传递给DataContractSerializer构造函数的Type之一。

Try that and see if it helps. 试试看,看看是否有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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