Normalize line endings.
[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.Serialization.Formatters;
37 using System.Globalization;
38
39 namespace System.Runtime.Serialization
40 {
41         [System.Runtime.InteropServices.ComVisibleAttribute (true)]
42         public sealed class FormatterServices
43         {
44                 private const BindingFlags fieldFlags = BindingFlags.Public |
45                                                         BindingFlags.Instance |
46                                                         BindingFlags.NonPublic |
47                                                         BindingFlags.DeclaredOnly;
48
49                 private FormatterServices ()
50                 {
51                 }
52
53                 public static object [] GetObjectData (object obj, MemberInfo [] members)
54                 {
55                         if (obj == null)
56                                 throw new ArgumentNullException ("obj");
57
58                         if (members == null)
59                                 throw new ArgumentNullException ("members");
60
61                         int n = members.Length;
62                         object [] result = new object [n];
63                         for (int i = 0; i < n; i++) {
64                                 MemberInfo member = members [i];
65                                 if (member == null)
66                                         throw new ArgumentNullException (String.Format ("members[{0}]", i));
67
68                                 if (member.MemberType != MemberTypes.Field)
69                                         throw new SerializationException (
70                                                         String.Format ("members [{0}] is not a field.", i));
71
72                                 FieldInfo fi = member as FieldInfo; // members must be fields
73                                 result [i] = fi.GetValue (obj);
74                         }
75
76                         return result;
77                 }
78
79                 public static MemberInfo [] GetSerializableMembers (Type type)
80                 {
81                         StreamingContext st = new StreamingContext (StreamingContextStates.All);
82                         return GetSerializableMembers (type, st);
83                 }
84
85                 public static MemberInfo [] GetSerializableMembers (Type type, StreamingContext context)
86                 {
87                         if (type == null)
88                                 throw new ArgumentNullException ("type");
89
90                         //FIXME: context?
91                         ArrayList fields = new ArrayList ();
92                         Type t = type;
93                         while (t != null) {
94                                 if (!t.IsSerializable) {
95                                         string msg = String.Format ("Type {0} in assembly {1} is not " +
96                                                                     "marked as serializable.",
97                                                                     t, t.Assembly.FullName);
98
99                                         throw new SerializationException (msg);
100                                 }
101
102                                 GetFields (type, t, fields);
103                                 t = t.BaseType;
104                         }
105
106                         MemberInfo [] result = new MemberInfo [fields.Count];
107                         fields.CopyTo (result);
108                         return result;
109                 }
110
111                 private static void GetFields (Type reflectedType, Type type, ArrayList fields)
112                 {
113                         FieldInfo [] fs = type.GetFields (fieldFlags);
114                         foreach (FieldInfo field in fs)
115                                 if (!(field.IsNotSerialized)) {
116                                         MonoField mf = field as MonoField;
117                                         if (mf != null && reflectedType != type && !mf.IsPublic) {
118                                                 string fname = type.Name + "+" + mf.Name;
119                                                 fields.Add (mf.Clone (fname));
120                                         }
121                                         else
122                                                 fields.Add (field);
123                                 }
124                 }
125
126                 public static Type GetTypeFromAssembly (Assembly assem, string name)
127                 {
128                         if (assem == null)
129                                 throw new ArgumentNullException ("assem");
130
131                         if (name == null)
132                                 throw new ArgumentNullException ("name");
133
134                         return assem.GetType (name);
135                 }
136
137                 public static object GetUninitializedObject (Type type)
138                 {
139                         if (type == null)
140                                 throw new ArgumentNullException ("type");
141
142                         if (type == typeof (string))
143                                 throw new ArgumentException ("Uninitialized Strings cannot be created.");
144
145                         return System.Runtime.Remoting.Activation.ActivationServices.AllocateUninitializedClassInstance (type);
146                 }
147
148                 public static object PopulateObjectMembers (object obj, MemberInfo [] members, object [] data)
149                 {
150                         if (obj == null)
151                                 throw new ArgumentNullException ("obj");
152
153                         if (members == null)
154                                 throw new ArgumentNullException ("members");
155
156                         if (data == null)
157                                 throw new ArgumentNullException ("data");
158
159                         int length = members.Length;
160                         if (length != data.Length)
161                                 throw new ArgumentException ("different length in members and data");
162
163                         for (int i = 0; i < length; i++) {
164                                 MemberInfo member = members [i];
165                                 if (member == null)
166                                         throw new ArgumentNullException (String.Format ("members[{0}]", i));
167                                         
168                                 if (member.MemberType != MemberTypes.Field)
169                                         throw new SerializationException (
170                                                         String.Format ("members [{0}] is not a field.", i));
171
172                                 FieldInfo fi = member as FieldInfo; // members must be fields
173                                 fi.SetValue (obj, data [i]);
174                         }
175
176                         return obj;
177                 }
178                 
179 #if NET_1_1
180
181                 public static void CheckTypeSecurity (Type t, TypeFilterLevel securityLevel)
182                 {
183                         if (securityLevel == TypeFilterLevel.Full) return;
184                         CheckNotAssignable (typeof(System.DelegateSerializationHolder), t);
185                         CheckNotAssignable (typeof(System.Runtime.Remoting.Lifetime.ISponsor), t);
186                         CheckNotAssignable (typeof(System.Runtime.Remoting.IEnvoyInfo), t);
187                         CheckNotAssignable (typeof(System.Runtime.Remoting.ObjRef), t);
188                 }
189                 
190                 static void CheckNotAssignable (Type basetype, Type type)
191                 {
192                         if (basetype.IsAssignableFrom (type)) {
193                                 string msg = "Type " + basetype + " and the types derived from it";
194                                 msg += " (such as " + type + ") are not permitted to be deserialized at this security level";
195                                 throw new System.Security.SecurityException (msg);
196                         }
197                 }
198
199                 public static object GetSafeUninitializedObject (Type type)
200                 {
201                         // FIXME: MS.NET uses code access permissions to check if the caller is
202                         // allowed to create an instance of this type. We can't support this
203                         // because it is not implemented in mono.
204                         
205                         // In concrete, the it will request a SecurityPermission of 
206                         // type "Infrastructure".
207                         
208                         return GetUninitializedObject (type);
209                 }
210 #endif
211         }
212 }