FormatterServices.cs 6.1 KB

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