Fix bugs in sizing TableLayoutPanel (Xamarin bug 18638)
[mono.git] / mcs / class / System.ComponentModel.Composition / Tests / UnitTestFramework / System / Runtime / Serialization / SerializationTestServices.cs
1 // -----------------------------------------------------------------------\r
2 // Copyright (c) Microsoft Corporation.  All rights reserved.\r
3 // -----------------------------------------------------------------------\r
4 #if !SILVERLIGHT\r
5 \r
6 using System;\r
7 using System.IO;\r
8 using System.Runtime.Serialization.Formatters.Binary;\r
9 using System.Reflection;\r
10 using Microsoft.VisualStudio.TestTools.UnitTesting;\r
11 \r
12 namespace System.Runtime.Serialization\r
13 {\r
14     public static class SerializationTestServices\r
15     {\r
16         /// <summary>\r
17         ///     Serializes and then deserializes the specified value.\r
18         /// </summary>\r
19         public static T RoundTrip<T>(T value)\r
20         {\r
21             Assert.IsNotNull(value);\r
22 \r
23             using (MemoryStream stream = new MemoryStream())\r
24             {\r
25                 BinaryFormatter formatter = new BinaryFormatter();\r
26                 formatter.Serialize(stream, value);\r
27 \r
28                 stream.Seek(0, SeekOrigin.Begin);\r
29                 return (T)formatter.Deserialize(stream);\r
30             }\r
31         }\r
32 \r
33         /// <summary>\r
34         ///     Creates an instance of a type using the serialization constructor.\r
35         /// </summary>\r
36         public static T Create<T>(SerializationInfo info, StreamingContext context)\r
37         {\r
38             ConstructorInfo constructor = typeof(T).GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null,\r
39                                                                    new Type[] { typeof(SerializationInfo), typeof(StreamingContext) },\r
40                                                                    (ParameterModifier[])null);\r
41 \r
42             Assert.IsNotNull(constructor, "Type does not have a private or protected serialization constructor.");\r
43 \r
44             try\r
45             {\r
46                 return (T)constructor.Invoke(new object[] { info, context });\r
47             }\r
48             catch (TargetInvocationException ex)\r
49             {\r
50                 throw ex.InnerException;\r
51             }\r
52         }\r
53 \r
54         /// <summary>\r
55         ///     Returns a new instance of <see cref="SerializationInfo"/> replacing the specified member name with the specified value.\r
56         /// </summary>\r
57         public static SerializationInfo CreateSerializationInfoReplacingMember<T>(string memberName, object value)\r
58             where T : ISerializable, new()\r
59         {\r
60             return CreateSerializationInfoReplacingMember(memberName, value, () => new T()); \r
61         }\r
62 \r
63         /// <summary>\r
64         ///     Returns a new instance of <see cref="SerializationInfo"/> replacing the specified member name with the specified value.\r
65         /// </summary>\r
66         public static SerializationInfo CreateSerializationInfoReplacingMember<T>(string memberName, object value, Func<T> creator)\r
67             where T : ISerializable\r
68         {\r
69             T serializableObject = creator();\r
70 \r
71             var info = GetObjectDataFrom(serializableObject);\r
72 \r
73             return CloneReplacingMember<T>(info, memberName, value);\r
74         }\r
75 \r
76         /// <summary>\r
77         ///     Returns a new instance of <see cref="SerializationInfo"/> removing the specified member name.\r
78         /// </summary>\r
79         public static SerializationInfo CreateSerializationInfoRemovingMember<T>(string memberName)\r
80             where T : ISerializable, new()\r
81         {\r
82             return CreateSerializationInfoRemovingMember(memberName, () => new T());\r
83         }\r
84 \r
85         /// <summary>\r
86         ///     Returns a new instance of <see cref="SerializationInfo"/> removing the specified member name.\r
87         /// </summary>\r
88         public static SerializationInfo CreateSerializationInfoRemovingMember<T>(string memberName, Func<T> creator)\r
89             where T : ISerializable\r
90         {\r
91             T serializableObject = creator();\r
92 \r
93             var info = GetObjectDataFrom(serializableObject);\r
94 \r
95             return CloneRemovingMember<T>(info, memberName);\r
96         }\r
97 \r
98         private static SerializationInfo CloneReplacingMember<T>(SerializationInfo info, string memberName, object value)\r
99         {\r
100             return Clone<T>(info, (entry, clone) =>\r
101             {\r
102                 if (entry.Name != memberName)\r
103                 {\r
104                     return true;\r
105                 }\r
106 \r
107                 // Replace the entry\r
108                 clone.AddValue(entry.Name, value, value == null ? entry.ObjectType : value.GetType());\r
109                 return false;\r
110             });\r
111         }\r
112 \r
113         private static SerializationInfo CloneRemovingMember<T>(SerializationInfo info, string memberName)\r
114         {\r
115             return Clone<T>(info, (entry, clone) =>\r
116             {\r
117                 // Add everything except the member we want to remove\r
118                 return entry.Name != memberName;\r
119             });\r
120         }\r
121 \r
122         private static SerializationInfo Clone<T>(SerializationInfo info, Func<SerializationEntry, SerializationInfo, bool> predicate)\r
123         {\r
124             var clone = GetEmptySerializationInfo<T>();\r
125 \r
126             foreach (var entry in info)\r
127             {\r
128                 if (predicate(entry, clone))\r
129                 {\r
130                     clone.AddValue(entry.Name, entry.Value, entry.ObjectType);\r
131                 }\r
132             }\r
133 \r
134             return clone;\r
135         }\r
136 \r
137         private static SerializationInfo GetObjectDataFrom<T>(T serializableObject) where T : ISerializable\r
138         {\r
139             var info = GetEmptySerializationInfo<T>();\r
140 \r
141             serializableObject.GetObjectData(info, new StreamingContext());\r
142 \r
143             return info;\r
144         }\r
145 \r
146         private static SerializationInfo GetEmptySerializationInfo<T>()\r
147         {\r
148             StrictFormatterConverter converter = new StrictFormatterConverter();\r
149 \r
150             return new SerializationInfo(typeof(T), converter);\r
151         }\r
152     }\r
153 }\r
154 \r
155 #endif // !SILVERLIGHT