FormatterServices.cs 6.4 KB

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