GenericDictionaryCustomKey Sample

Back to summary

Source

using System.Collections.Generic;

namespace XmlSerializersTest.Samples
{
    [Title("Generic dictionary with custom key type")]
    public class GenericDictionaryCustomKey
    {
        public Dictionary<OneSimpleProperty, string> Things { get; set; }
    }
}

Standard Serializer

System.InvalidOperationException: There was an error reflecting type 'XmlSerializersTest.Samples.GenericDictionaryCustomKey'. ---> System.NotSupportedException: Cannot serialize member XmlSerializersTest.Samples.GenericDictionaryCustomKey.Things of type System.Collections.Generic.Dictionary`2[[XmlSerializersTest.Samples.OneSimpleProperty, XmlSerializersTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], because it implements IDictionary.
   at System.Xml.Serialization.TypeScope.GetDefaultIndexer(Type type, String memberInfo)
   at System.Xml.Serialization.TypeScope.ImportTypeDesc(Type type, MemberInfo memberInfo, Boolean directReference)
   at System.Xml.Serialization.TypeScope.GetTypeDesc(Type type, MemberInfo source, Boolean directReference, Boolean throwOnError)
   at System.Xml.Serialization.StructModel.GetPropertyModel(PropertyInfo propertyInfo)
   at System.Xml.Serialization.StructModel.GetFieldModel(MemberInfo memberInfo)
   at System.Xml.Serialization.XmlReflectionImporter.InitializeStructMembers(StructMapping mapping, StructModel model, Boolean openModel, String typeName, RecursionLimiter limiter)
   at System.Xml.Serialization.XmlReflectionImporter.ImportStructLikeMapping(StructModel model, String ns, Boolean openModel, XmlAttributes a, RecursionLimiter limiter)
   at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel model, String ns, ImportContext context, String dataType, XmlAttributes a, Boolean repeats, Boolean openModel, RecursionLimiter limiter)
   --- End of inner exception stack trace ---
   at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel model, String ns, ImportContext context, String dataType, XmlAttributes a, Boolean repeats, Boolean openModel, RecursionLimiter limiter)
   at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel model, String ns, ImportContext context, String dataType, XmlAttributes a, RecursionLimiter limiter)
   at System.Xml.Serialization.XmlReflectionImporter.ImportElement(TypeModel model, XmlRootAttribute root, String defaultNamespace, RecursionLimiter limiter)
   at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(Type type, XmlRootAttribute root, String defaultNamespace)
   at System.Xml.Serialization.XmlSerializer..ctor(Type type, String defaultNamespace)
   at System.Xml.Serialization.XmlSerializer..ctor(Type type)
   at XmlSerializersTest.Serializers.StandardXmlSerializer.Serialize(Object obj, XmlWriter writer) in C:\Ivan\dev\XmlSerializersTest\XmlSerializersTest\Serializers\StandardXmlSerializer.cs:line 13
   at XmlSerializersTest.SampleProcessor.SerializeToString(Object obj, IXmlSerializer serializer) in C:\Ivan\dev\XmlSerializersTest\XmlSerializersTest\SampleProcessor.cs:line 162
   at XmlSerializersTest.SampleProcessor.RecordSerializationResults(Object obj, IXmlSerializer serializer) in C:\Ivan\dev\XmlSerializersTest\XmlSerializersTest\SampleProcessor.cs:line 115

Deserialization skipped

XAML Serializer

System.InvalidOperationException: Key value of type 'XmlSerializersTest.Samples.OneSimpleProperty' cannot be serialized. Keys can only be a string, a type, or an enumeration value.
   at System.Windows.Markup.Primitives.MarkupWriter.WriteItem(MarkupObject item, Scope scope)
   at System.Windows.Markup.Primitives.MarkupWriter.WriteItem(MarkupObject item, Scope scope)
   at System.Windows.Markup.Primitives.MarkupWriter.WriteItem(MarkupObject item)
   at System.Windows.Markup.Primitives.MarkupWriter.SaveAsXml(XmlWriter writer, MarkupObject item)
   at System.Windows.Markup.Primitives.MarkupWriter.SaveAsXml(XmlWriter writer, Object instance)
   at System.Windows.Markup.XamlWriter.Save(Object obj, XmlWriter xmlWriter)
   at XmlSerializersTest.Serializers.XamlSerializer.Serialize(Object obj, XmlWriter writer) in C:\Ivan\dev\XmlSerializersTest\XmlSerializersTest\Serializers\XamlSerializer.cs:line 13
   at XmlSerializersTest.SampleProcessor.SerializeToString(Object obj, IXmlSerializer serializer) in C:\Ivan\dev\XmlSerializersTest\XmlSerializersTest\SampleProcessor.cs:line 162
   at XmlSerializersTest.SampleProcessor.RecordSerializationResults(Object obj, IXmlSerializer serializer) in C:\Ivan\dev\XmlSerializersTest\XmlSerializersTest\SampleProcessor.cs:line 115

Deserialization skipped

WCF Serializer

<GenericDictionaryCustomKey xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/XmlSerializersTest.Samples">
  <Things xmlns:d2p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
    <d2p1:KeyValueOfOneSimplePropertystringwbwuO2LY>
      <d2p1:Key>
        <Value>1</Value>
      </d2p1:Key>
      <d2p1:Value>one</d2p1:Value>
    </d2p1:KeyValueOfOneSimplePropertystringwbwuO2LY>
  </Things>
</GenericDictionaryCustomKey>

WCF-ref Serializer

<GenericDictionaryCustomKey xmlns:i="http://www.w3.org/2001/XMLSchema-instance" z:Id="1" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/" xmlns="http://schemas.datacontract.org/2004/07/XmlSerializersTest.Samples">
  <Things xmlns:d2p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays" z:Id="2" z:Size="1">
    <d2p1:KeyValueOfOneSimplePropertystringwbwuO2LY>
      <d2p1:Key z:Id="3">
        <Value>1</Value>
      </d2p1:Key>
      <d2p1:Value z:Id="4">one</d2p1:Value>
    </d2p1:KeyValueOfOneSimplePropertystringwbwuO2LY>
  </Things>
</GenericDictionaryCustomKey>