Welcome to mirror list, hosted at ThFree Co, Russian Federation.

SerializationTestServices.cs « Serialization « Runtime « System « UnitTestFramework « Tests « System.ComponentModel.Composition « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 338b520cf940f00969a513c8f23401b09780e521 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// -----------------------------------------------------------------------
// Copyright (c) Microsoft Corporation.  All rights reserved.
// -----------------------------------------------------------------------
#if !SILVERLIGHT

using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Reflection;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace System.Runtime.Serialization
{
    public static class SerializationTestServices
    {
        /// <summary>
        ///     Serializes and then deserializes the specified value.
        /// </summary>
        public static T RoundTrip<T>(T value)
        {
            Assert.IsNotNull(value);

            using (MemoryStream stream = new MemoryStream())
            {
                BinaryFormatter formatter = new BinaryFormatter();
                formatter.Serialize(stream, value);

                stream.Seek(0, SeekOrigin.Begin);
                return (T)formatter.Deserialize(stream);
            }
        }

        /// <summary>
        ///     Creates an instance of a type using the serialization constructor.
        /// </summary>
        public static T Create<T>(SerializationInfo info, StreamingContext context)
        {
            ConstructorInfo constructor = typeof(T).GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null,
                                                                   new Type[] { typeof(SerializationInfo), typeof(StreamingContext) },
                                                                   (ParameterModifier[])null);

            Assert.IsNotNull(constructor, "Type does not have a private or protected serialization constructor.");

            try
            {
                return (T)constructor.Invoke(new object[] { info, context });
            }
            catch (TargetInvocationException ex)
            {
                throw ex.InnerException;
            }
        }

        /// <summary>
        ///     Returns a new instance of <see cref="SerializationInfo"/> replacing the specified member name with the specified value.
        /// </summary>
        public static SerializationInfo CreateSerializationInfoReplacingMember<T>(string memberName, object value)
            where T : ISerializable, new()
        {
            return CreateSerializationInfoReplacingMember(memberName, value, () => new T()); 
        }

        /// <summary>
        ///     Returns a new instance of <see cref="SerializationInfo"/> replacing the specified member name with the specified value.
        /// </summary>
        public static SerializationInfo CreateSerializationInfoReplacingMember<T>(string memberName, object value, Func<T> creator)
            where T : ISerializable
        {
            T serializableObject = creator();

            var info = GetObjectDataFrom(serializableObject);

            return CloneReplacingMember<T>(info, memberName, value);
        }

        /// <summary>
        ///     Returns a new instance of <see cref="SerializationInfo"/> removing the specified member name.
        /// </summary>
        public static SerializationInfo CreateSerializationInfoRemovingMember<T>(string memberName)
            where T : ISerializable, new()
        {
            return CreateSerializationInfoRemovingMember(memberName, () => new T());
        }

        /// <summary>
        ///     Returns a new instance of <see cref="SerializationInfo"/> removing the specified member name.
        /// </summary>
        public static SerializationInfo CreateSerializationInfoRemovingMember<T>(string memberName, Func<T> creator)
            where T : ISerializable
        {
            T serializableObject = creator();

            var info = GetObjectDataFrom(serializableObject);

            return CloneRemovingMember<T>(info, memberName);
        }

        private static SerializationInfo CloneReplacingMember<T>(SerializationInfo info, string memberName, object value)
        {
            return Clone<T>(info, (entry, clone) =>
            {
                if (entry.Name != memberName)
                {
                    return true;
                }

                // Replace the entry
                clone.AddValue(entry.Name, value, value == null ? entry.ObjectType : value.GetType());
                return false;
            });
        }

        private static SerializationInfo CloneRemovingMember<T>(SerializationInfo info, string memberName)
        {
            return Clone<T>(info, (entry, clone) =>
            {
                // Add everything except the member we want to remove
                return entry.Name != memberName;
            });
        }

        private static SerializationInfo Clone<T>(SerializationInfo info, Func<SerializationEntry, SerializationInfo, bool> predicate)
        {
            var clone = GetEmptySerializationInfo<T>();

            foreach (var entry in info)
            {
                if (predicate(entry, clone))
                {
                    clone.AddValue(entry.Name, entry.Value, entry.ObjectType);
                }
            }

            return clone;
        }

        private static SerializationInfo GetObjectDataFrom<T>(T serializableObject) where T : ISerializable
        {
            var info = GetEmptySerializationInfo<T>();

            serializableObject.GetObjectData(info, new StreamingContext());

            return info;
        }

        private static SerializationInfo GetEmptySerializationInfo<T>()
        {
            StrictFormatterConverter converter = new StrictFormatterConverter();

            return new SerializationInfo(typeof(T), converter);
        }
    }
}

#endif // !SILVERLIGHT