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