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