I'm trying to create KnownType lists for arbitrary objects, and I'm having trouble trying to serialize types using generic elements, such as DbEntityValidationExceptionone that has a type list property IEnumerable<DbEntityValidationResult>.
When I call the constructor for the DataContractSerializer, I give it a KnownTypes list, which consists of the following types constructed using reflection to capture the types of all its properties, as well as any arguments of a general type:
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 recorded using trace:
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
However, for some reason, the DataContractSerializer throws out the missing types, as if the type of the list property was List<object>instead 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.
DataContractSerializer , , , ?