[sgen] Write barrier nursery checks might be needed
[mono.git] / mcs / class / corlib / System.Runtime.Serialization / FormatterServices.cs
1 //
2 // System.Runtime.Serialization.FormatterServices
3 //
4 // Authors:
5 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //
7 // (C) 2002 Ximian, Inc (http://www.ximian.com)
8 //
9
10 //
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32 using System;
33 using System.Collections;
34 using System.Reflection;
35 using System.Runtime.CompilerServices;
36 using System.Runtime.InteropServices;
37 using System.Runtime.Serialization.Formatters;
38 using System.Globalization;
39
40 namespace System.Runtime.Serialization
41 {
42         [System.Runtime.InteropServices.ComVisibleAttribute (true)]
43         static
44         public class FormatterServices
45         {
46                 private const BindingFlags fieldFlags = BindingFlags.Public |
47                                                         BindingFlags.Instance |
48                                                         BindingFlags.NonPublic |
49                                                         BindingFlags.DeclaredOnly;
50
51
52                 public static object [] GetObjectData (object obj, MemberInfo [] members)
53                 {
54                         if (obj == null)
55                                 throw new ArgumentNullException ("obj");
56
57                         if (members == null)
58                                 throw new ArgumentNullException ("members");
59
60                         int n = members.Length;
61                         object [] result = new object [n];
62                         for (int i = 0; i < n; i++) {
63                                 MemberInfo member = members [i];
64                                 if (member == null)
65                                         throw new ArgumentNullException (String.Format ("members[{0}]", i));
66
67                                 if (member.MemberType != MemberTypes.Field)
68                                         throw new SerializationException (
69                                                         String.Format ("members [{0}] is not a field.", i));
70
71                                 FieldInfo fi = member as FieldInfo; // members must be fields
72                                 result [i] = fi.GetValue (obj);
73                         }
74
75                         return result;
76                 }
77
78                 public static MemberInfo [] GetSerializableMembers (Type type)
79                 {
80                         StreamingContext st = new StreamingContext (StreamingContextStates.All);
81                         return GetSerializableMembers (type, st);
82                 }
83
84                 public static MemberInfo [] GetSerializableMembers (Type type, StreamingContext context)
85                 {
86                         if (type == null)
87                                 throw new ArgumentNullException ("type");
88
89                         //FIXME: context?
90                         ArrayList fields = new ArrayList ();
91                         Type t = type;
92                         while (t != null) {
93                                 if (!t.IsSerializable) {
94                                         string msg = String.Format ("Type {0} in assembly {1} is not " +
95                                                                     "marked as serializable.",
96                                                                     t, t.Assembly.FullName);
97
98                                         throw new SerializationException (msg);
99                                 }
100
101                                 GetFields (type, t, fields);
102                                 t = t.BaseType;
103                         }
104
105                         MemberInfo [] result = new MemberInfo [fields.Count];
106                         fields.CopyTo (result);
107                         return result;
108                 }
109
110                 private static void GetFields (Type reflectedType, Type type, ArrayList fields)
111                 {
112                         FieldInfo [] fs = type.GetFields (fieldFlags);
113                         foreach (FieldInfo field in fs)
114                                 if (!(field.IsNotSerialized)) {
115                                         MonoField mf = field as MonoField;
116                                         if (mf != null && reflectedType != type && !mf.IsPublic) {
117                                                 string fname = type.Name + "+" + mf.Name;
118                                                 fields.Add (mf.Clone (fname));
119                                         }
120                                         else
121                                                 fields.Add (field);
122                                 }
123                 }
124
125                 public static Type GetTypeFromAssembly (Assembly assem, string name)
126                 {
127                         if (assem == null)
128                                 throw new ArgumentNullException ("assem");
129
130                         if (name == null)
131                                 throw new ArgumentNullException ("name");
132
133                         return assem.GetType (name);
134                 }
135
136                 public static object GetUninitializedObject (Type type)
137                 {
138                         if (type == null)
139                                 throw new ArgumentNullException ("type");
140
141                         if (type == typeof (string))
142                                 throw new ArgumentException ("Uninitialized Strings cannot be created.");
143
144                         return System.Runtime.Remoting.Activation.ActivationServices.AllocateUninitializedClassInstance (type);
145                 }
146
147                 public static object PopulateObjectMembers (object obj, MemberInfo [] members, object [] data)
148                 {
149                         if (obj == null)
150                                 throw new ArgumentNullException ("obj");
151
152                         if (members == null)
153                                 throw new ArgumentNullException ("members");
154
155                         if (data == null)
156                                 throw new ArgumentNullException ("data");
157
158                         int length = members.Length;
159                         if (length != data.Length)
160                                 throw new ArgumentException ("different length in members and data");
161
162                         for (int i = 0; i < length; i++) {
163                                 MemberInfo member = members [i];
164                                 if (member == null)
165                                         throw new ArgumentNullException (String.Format ("members[{0}]", i));
166                                         
167                                 if (member.MemberType != MemberTypes.Field)
168                                         throw new SerializationException (
169                                                         String.Format ("members [{0}] is not a field.", i));
170
171                                 FieldInfo fi = member as FieldInfo; // members must be fields
172                                 fi.SetValue (obj, data [i]);
173                         }
174
175                         return obj;
176                 }
177                 
178
179                 public static void CheckTypeSecurity (Type t, TypeFilterLevel securityLevel)
180                 {
181                         if (securityLevel == TypeFilterLevel.Full) return;
182                         CheckNotAssignable (typeof(System.DelegateSerializationHolder), t);
183                         CheckNotAssignable (typeof(System.Runtime.Remoting.Lifetime.ISponsor), t);
184                         CheckNotAssignable (typeof(System.Runtime.Remoting.IEnvoyInfo), t);
185                         CheckNotAssignable (typeof(System.Runtime.Remoting.ObjRef), t);
186                 }
187                 
188                 static void CheckNotAssignable (Type basetype, Type type)
189                 {
190                         if (basetype.IsAssignableFrom (type)) {
191                                 string msg = "Type " + basetype + " and the types derived from it";
192                                 msg += " (such as " + type + ") are not permitted to be deserialized at this security level";
193                                 throw new System.Security.SecurityException (msg);
194                         }
195                 }
196
197                 public static object GetSafeUninitializedObject (Type type)
198                 {
199                         // FIXME: MS.NET uses code access permissions to check if the caller is
200                         // allowed to create an instance of this type. We can't support this
201                         // because it is not implemented in mono.
202                         
203                         // In concrete, the it will request a SecurityPermission of 
204                         // type "Infrastructure".
205                         
206                         return GetUninitializedObject (type);
207                 }
208
209                 // This method was introduced in .Net due to a bug serializing objects with circular references
210                 // which we don't appear to have, so we just return the same object.
211                 // See http://support.microsoft.com/kb/927495/en-us/ in case of doubt.
212                 [ComVisible (false)]
213                 public static ISerializationSurrogate GetSurrogateForCyclicalReference (ISerializationSurrogate innerSurrogate)
214                 {
215                         return innerSurrogate;
216                 }
217         }
218 }