public static void DataContractSerializationSurrogateTest()
    {
        OperationDescription od = null;
        DataContractSerializerOperationBehavior behavior = new DataContractSerializerOperationBehavior(od);

        behavior.SerializationSurrogateProvider = new MySerializationSurrogateProvider();

        DataContractSerializer dcs = (DataContractSerializer)behavior.CreateSerializer(typeof(SurrogateTestType), nameof(SurrogateTestType), "ns", new List <Type>());

        var members = new NonSerializableType[2];

        members[0] = new NonSerializableType("name1", 1);
        members[1] = new NonSerializableType("name2", 2);

        using (MemoryStream ms = new MemoryStream())
        {
            SurrogateTestType obj = new SurrogateTestType {
                Members = members
            };

            dcs.WriteObject(ms, obj);
            ms.Position = 0;
            var deserialized = (SurrogateTestType)dcs.ReadObject(ms);

            Assert.True(((MySerializationSurrogateProvider)behavior.SerializationSurrogateProvider).mySurrogateProviderIsUsed);

            for (int i = 0; i < 2; i++)
            {
                Assert.StrictEqual(obj.Members[i].Name, deserialized.Members[i].Name);
                Assert.StrictEqual(obj.Members[i].Index, deserialized.Members[i].Index);
            }
        }
    }
Ejemplo n.º 2
0
        XmlObjectSerializer GetSerializer(MessagePartDescription partDesc)
        {
            if (!serializers.ContainsKey(partDesc))
#if !MOBILE
            { if (serializerBehavior != null)
              {
                  serializers [partDesc] = serializerBehavior.CreateSerializer(
                      partDesc.Type, partDesc.Name, partDesc.Namespace, OperationKnownTypes as IList <Type>);
              }
              else
#endif
            { serializers [partDesc] = new DataContractSerializer(
                  partDesc.Type, partDesc.Name, partDesc.Namespace, OperationKnownTypes); }
            return(serializers [partDesc]);
        }

        object ReadHeaderObject(Type type, XmlObjectSerializer serializer, XmlDictionaryReader reader)
        {
            // FIXME: it's a nasty workaround just to avoid UniqueId output as a string.
            // Seealso MessageHeader.DefaultMessageHeader.OnWriteHeaderContents().
            // Note that msg.Headers.GetHeader<UniqueId> () simply fails (on .NET too) and it is useless. The API is lame by design.
            if (type == typeof(UniqueId))
                return(new UniqueId(reader.ReadElementContentAsString())); }