2 // System.Runtime.Serialization.FormatterServices
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // (C) 2002 Ximian, Inc (http://www.ximian.com)
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
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.
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;
40 namespace System.Runtime.Serialization
42 [System.Runtime.InteropServices.ComVisibleAttribute (true)]
43 public sealed class FormatterServices
45 private const BindingFlags fieldFlags = BindingFlags.Public |
46 BindingFlags.Instance |
47 BindingFlags.NonPublic |
48 BindingFlags.DeclaredOnly;
50 private FormatterServices ()
54 public static object [] GetObjectData (object obj, MemberInfo [] members)
57 throw new ArgumentNullException ("obj");
60 throw new ArgumentNullException ("members");
62 int n = members.Length;
63 object [] result = new object [n];
64 for (int i = 0; i < n; i++) {
65 MemberInfo member = members [i];
67 throw new ArgumentNullException (String.Format ("members[{0}]", i));
69 if (member.MemberType != MemberTypes.Field)
70 throw new SerializationException (
71 String.Format ("members [{0}] is not a field.", i));
73 FieldInfo fi = member as FieldInfo; // members must be fields
74 result [i] = fi.GetValue (obj);
80 public static MemberInfo [] GetSerializableMembers (Type type)
82 StreamingContext st = new StreamingContext (StreamingContextStates.All);
83 return GetSerializableMembers (type, st);
86 public static MemberInfo [] GetSerializableMembers (Type type, StreamingContext context)
89 throw new ArgumentNullException ("type");
92 ArrayList fields = new ArrayList ();
95 if (!t.IsSerializable) {
96 string msg = String.Format ("Type {0} in assembly {1} is not " +
97 "marked as serializable.",
98 t, t.Assembly.FullName);
100 throw new SerializationException (msg);
103 GetFields (type, t, fields);
107 MemberInfo [] result = new MemberInfo [fields.Count];
108 fields.CopyTo (result);
112 private static void GetFields (Type reflectedType, Type type, ArrayList fields)
114 FieldInfo [] fs = type.GetFields (fieldFlags);
115 foreach (FieldInfo field in fs)
116 if (!(field.IsNotSerialized)) {
117 MonoField mf = field as MonoField;
118 if (mf != null && reflectedType != type && !mf.IsPublic) {
119 string fname = type.Name + "+" + mf.Name;
120 fields.Add (mf.Clone (fname));
127 public static Type GetTypeFromAssembly (Assembly assem, string name)
130 throw new ArgumentNullException ("assem");
133 throw new ArgumentNullException ("name");
135 return assem.GetType (name);
138 public static object GetUninitializedObject (Type type)
141 throw new ArgumentNullException ("type");
143 if (type == typeof (string))
144 throw new ArgumentException ("Uninitialized Strings cannot be created.");
146 return System.Runtime.Remoting.Activation.ActivationServices.AllocateUninitializedClassInstance (type);
149 public static object PopulateObjectMembers (object obj, MemberInfo [] members, object [] data)
152 throw new ArgumentNullException ("obj");
155 throw new ArgumentNullException ("members");
158 throw new ArgumentNullException ("data");
160 int length = members.Length;
161 if (length != data.Length)
162 throw new ArgumentException ("different length in members and data");
164 for (int i = 0; i < length; i++) {
165 MemberInfo member = members [i];
167 throw new ArgumentNullException (String.Format ("members[{0}]", i));
169 if (member.MemberType != MemberTypes.Field)
170 throw new SerializationException (
171 String.Format ("members [{0}] is not a field.", i));
173 FieldInfo fi = member as FieldInfo; // members must be fields
174 fi.SetValue (obj, data [i]);
181 public static void CheckTypeSecurity (Type t, TypeFilterLevel securityLevel)
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);
190 static void CheckNotAssignable (Type basetype, Type type)
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);
199 public static object GetSafeUninitializedObject (Type type)
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.
205 // In concrete, the it will request a SecurityPermission of
206 // type "Infrastructure".
208 return GetUninitializedObject (type);
212 // This method was introduced in .Net due to a bug serializing objects with circular references
213 // which we don't appear to have, so we just return the same object.
214 // See http://support.microsoft.com/kb/927495/en-us/ in case of doubt.
216 public static ISerializationSurrogate GetSurrogateForCyclicalReference (ISerializationSurrogate innerSurrogate)
218 return innerSurrogate;