mscorlib(4.0.0.0) API with additions
SerializationInfo.cs
2 using System.Reflection;
6 using System.Security;
7 
9 {
11  [ComVisible(true)]
12  public sealed class SerializationInfo
13  {
14  private const int defaultSize = 4;
15 
16  private const string s_mscorlibAssemblySimpleName = "mscorlib";
17 
18  private const string s_mscorlibFileName = "mscorlib.dll";
19 
20  internal string[] m_members;
21 
22  internal object[] m_data;
23 
24  internal Type[] m_types;
25 
26  private Dictionary<string, int> m_nameToIndex;
27 
28  internal int m_currMember;
29 
30  internal IFormatterConverter m_converter;
31 
32  private string m_fullTypeName;
33 
34  private string m_assemName;
35 
36  private Type objectType;
37 
38  private bool isFullTypeNameSetExplicit;
39 
40  private bool isAssemblyNameSetExplicit;
41 
42  private bool requireSameTokenInPartialTrust;
43 
47  public string FullTypeName
48  {
49  get
50  {
51  return m_fullTypeName;
52  }
53  set
54  {
55  if (value == null)
56  {
57  throw new ArgumentNullException("value");
58  }
59  m_fullTypeName = value;
60  isFullTypeNameSetExplicit = true;
61  }
62  }
63 
67  public string AssemblyName
68  {
69  get
70  {
71  return m_assemName;
72  }
73  [SecuritySafeCritical]
74  set
75  {
76  if (value == null)
77  {
78  throw new ArgumentNullException("value");
79  }
80  if (requireSameTokenInPartialTrust)
81  {
82  DemandForUnsafeAssemblyNameAssignments(m_assemName, value);
83  }
84  m_assemName = value;
85  isAssemblyNameSetExplicit = true;
86  }
87  }
88 
91  public int MemberCount => m_currMember;
92 
95  public Type ObjectType => objectType;
96 
100  public bool IsFullTypeNameSetExplicit => isFullTypeNameSetExplicit;
101 
105  public bool IsAssemblyNameSetExplicit => isAssemblyNameSetExplicit;
106 
107  internal string[] MemberNames => m_members;
108 
109  internal object[] MemberValues => m_data;
110 
116  [CLSCompliant(false)]
118  : this(type, converter, requireSameTokenInPartialTrust: false)
119  {
120  }
121 
126  [CLSCompliant(false)]
127  public SerializationInfo(Type type, IFormatterConverter converter, bool requireSameTokenInPartialTrust)
128  {
129  if ((object)type == null)
130  {
131  throw new ArgumentNullException("type");
132  }
133  if (converter == null)
134  {
135  throw new ArgumentNullException("converter");
136  }
137  objectType = type;
138  m_fullTypeName = type.FullName;
139  m_assemName = type.Module.Assembly.FullName;
140  m_members = new string[4];
141  m_data = new object[4];
142  m_types = new Type[4];
143  m_nameToIndex = new Dictionary<string, int>();
144  m_converter = converter;
145  this.requireSameTokenInPartialTrust = requireSameTokenInPartialTrust;
146  }
147 
151  [SecuritySafeCritical]
152  public void SetType(Type type)
153  {
154  if ((object)type == null)
155  {
156  throw new ArgumentNullException("type");
157  }
158  if (requireSameTokenInPartialTrust)
159  {
160  DemandForUnsafeAssemblyNameAssignments(ObjectType.Assembly.FullName, type.Assembly.FullName);
161  }
162  if ((object)objectType != type)
163  {
164  objectType = type;
165  m_fullTypeName = type.FullName;
166  m_assemName = type.Module.Assembly.FullName;
167  isFullTypeNameSetExplicit = false;
168  isAssemblyNameSetExplicit = false;
169  }
170  }
171 
172  private static bool Compare(byte[] a, byte[] b)
173  {
174  if (a == null || b == null || a.Length == 0 || b.Length == 0 || a.Length != b.Length)
175  {
176  return false;
177  }
178  for (int i = 0; i < a.Length; i++)
179  {
180  if (a[i] != b[i])
181  {
182  return false;
183  }
184  }
185  return true;
186  }
187 
188  [SecuritySafeCritical]
189  internal static void DemandForUnsafeAssemblyNameAssignments(string originalAssemblyName, string newAssemblyName)
190  {
191  if (!IsAssemblyNameAssignmentSafe(originalAssemblyName, newAssemblyName))
192  {
193  CodeAccessPermission.Demand(PermissionType.SecuritySerialization);
194  }
195  }
196 
197  internal static bool IsAssemblyNameAssignmentSafe(string originalAssemblyName, string newAssemblyName)
198  {
199  if (originalAssemblyName == newAssemblyName)
200  {
201  return true;
202  }
203  AssemblyName assemblyName = new AssemblyName(originalAssemblyName);
204  AssemblyName assemblyName2 = new AssemblyName(newAssemblyName);
205  if (string.Equals(assemblyName2.Name, "mscorlib", StringComparison.OrdinalIgnoreCase) || string.Equals(assemblyName2.Name, "mscorlib.dll", StringComparison.OrdinalIgnoreCase))
206  {
207  return false;
208  }
209  return Compare(assemblyName.GetPublicKeyToken(), assemblyName2.GetPublicKeyToken());
210  }
211 
215  {
216  return new SerializationInfoEnumerator(m_members, m_data, m_types, m_currMember);
217  }
218 
219  private void ExpandArrays()
220  {
221  int num = m_currMember * 2;
222  if (num < m_currMember && int.MaxValue > m_currMember)
223  {
224  num = int.MaxValue;
225  }
226  string[] array = new string[num];
227  object[] array2 = new object[num];
228  Type[] array3 = new Type[num];
229  Array.Copy(m_members, array, m_currMember);
230  Array.Copy(m_data, array2, m_currMember);
231  Array.Copy(m_types, array3, m_currMember);
232  m_members = array;
233  m_data = array2;
234  m_types = array3;
235  }
236 
243  public void AddValue(string name, object value, Type type)
244  {
245  if (name == null)
246  {
247  throw new ArgumentNullException("name");
248  }
249  if ((object)type == null)
250  {
251  throw new ArgumentNullException("type");
252  }
253  AddValueInternal(name, value, type);
254  }
255 
262  public void AddValue(string name, object value)
263  {
264  if (value == null)
265  {
266  AddValue(name, value, typeof(object));
267  }
268  else
269  {
270  AddValue(name, value, value.GetType());
271  }
272  }
273 
279  public void AddValue(string name, bool value)
280  {
281  AddValue(name, value, typeof(bool));
282  }
283 
289  public void AddValue(string name, char value)
290  {
291  AddValue(name, value, typeof(char));
292  }
293 
299  [CLSCompliant(false)]
300  public void AddValue(string name, sbyte value)
301  {
302  AddValue(name, value, typeof(sbyte));
303  }
304 
310  public void AddValue(string name, byte value)
311  {
312  AddValue(name, value, typeof(byte));
313  }
314 
320  public void AddValue(string name, short value)
321  {
322  AddValue(name, value, typeof(short));
323  }
324 
330  [CLSCompliant(false)]
331  public void AddValue(string name, ushort value)
332  {
333  AddValue(name, value, typeof(ushort));
334  }
335 
341  public void AddValue(string name, int value)
342  {
343  AddValue(name, value, typeof(int));
344  }
345 
351  [CLSCompliant(false)]
352  public void AddValue(string name, uint value)
353  {
354  AddValue(name, value, typeof(uint));
355  }
356 
362  public void AddValue(string name, long value)
363  {
364  AddValue(name, value, typeof(long));
365  }
366 
372  [CLSCompliant(false)]
373  public void AddValue(string name, ulong value)
374  {
375  AddValue(name, value, typeof(ulong));
376  }
377 
383  public void AddValue(string name, float value)
384  {
385  AddValue(name, value, typeof(float));
386  }
387 
393  public void AddValue(string name, double value)
394  {
395  AddValue(name, value, typeof(double));
396  }
397 
403  public void AddValue(string name, decimal value)
404  {
405  AddValue(name, value, typeof(decimal));
406  }
407 
413  public void AddValue(string name, DateTime value)
414  {
415  AddValue(name, value, typeof(DateTime));
416  }
417 
418  internal void AddValueInternal(string name, object value, Type type)
419  {
420  if (m_nameToIndex.ContainsKey(name))
421  {
422  throw new SerializationException(Environment.GetResourceString("Serialization_SameNameTwice"));
423  }
424  m_nameToIndex.Add(name, m_currMember);
425  if (m_currMember >= m_members.Length)
426  {
427  ExpandArrays();
428  }
429  m_members[m_currMember] = name;
430  m_data[m_currMember] = value;
431  m_types[m_currMember] = type;
432  m_currMember++;
433  }
434 
435  internal void UpdateValue(string name, object value, Type type)
436  {
437  int num = FindElement(name);
438  if (num < 0)
439  {
440  AddValueInternal(name, value, type);
441  return;
442  }
443  m_data[num] = value;
444  m_types[num] = type;
445  }
446 
447  private int FindElement(string name)
448  {
449  if (name == null)
450  {
451  throw new ArgumentNullException("name");
452  }
453  if (m_nameToIndex.TryGetValue(name, out int value))
454  {
455  return value;
456  }
457  return -1;
458  }
459 
460  private object GetElement(string name, out Type foundType)
461  {
462  int num = FindElement(name);
463  if (num == -1)
464  {
465  throw new SerializationException(Environment.GetResourceString("Serialization_NotFound", name));
466  }
467  foundType = m_types[num];
468  return m_data[num];
469  }
470 
471  [ComVisible(true)]
472  private object GetElementNoThrow(string name, out Type foundType)
473  {
474  int num = FindElement(name);
475  if (num == -1)
476  {
477  foundType = null;
478  return null;
479  }
480  foundType = m_types[num];
481  return m_data[num];
482  }
483 
492  [SecuritySafeCritical]
493  public object GetValue(string name, Type type)
494  {
495  if ((object)type == null)
496  {
497  throw new ArgumentNullException("type");
498  }
499  RuntimeType runtimeType = type as RuntimeType;
500  if (runtimeType == null)
501  {
502  throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeType"));
503  }
504  Type foundType;
505  object element = GetElement(name, out foundType);
507  {
508  RealProxy realProxy = RemotingServices.GetRealProxy(element);
509  if (RemotingServices.ProxyCheckCast(realProxy, runtimeType))
510  {
511  return element;
512  }
513  }
514  else if ((object)foundType == type || type.IsAssignableFrom(foundType) || element == null)
515  {
516  return element;
517  }
518  return m_converter.Convert(element, type);
519  }
520 
521  [SecuritySafeCritical]
522  [ComVisible(true)]
523  internal object GetValueNoThrow(string name, Type type)
524  {
525  Type foundType;
526  object elementNoThrow = GetElementNoThrow(name, out foundType);
527  if (elementNoThrow == null)
528  {
529  return null;
530  }
531  if (RemotingServices.IsTransparentProxy(elementNoThrow))
532  {
533  RealProxy realProxy = RemotingServices.GetRealProxy(elementNoThrow);
534  if (RemotingServices.ProxyCheckCast(realProxy, (RuntimeType)type))
535  {
536  return elementNoThrow;
537  }
538  }
539  else if ((object)foundType == type || type.IsAssignableFrom(foundType) || elementNoThrow == null)
540  {
541  return elementNoThrow;
542  }
543  return m_converter.Convert(elementNoThrow, type);
544  }
545 
553  public bool GetBoolean(string name)
554  {
555  Type foundType;
556  object element = GetElement(name, out foundType);
557  if ((object)foundType == typeof(bool))
558  {
559  return (bool)element;
560  }
561  return m_converter.ToBoolean(element);
562  }
563 
571  public char GetChar(string name)
572  {
573  Type foundType;
574  object element = GetElement(name, out foundType);
575  if ((object)foundType == typeof(char))
576  {
577  return (char)element;
578  }
579  return m_converter.ToChar(element);
580  }
581 
589  [CLSCompliant(false)]
590  public sbyte GetSByte(string name)
591  {
592  Type foundType;
593  object element = GetElement(name, out foundType);
594  if ((object)foundType == typeof(sbyte))
595  {
596  return (sbyte)element;
597  }
598  return m_converter.ToSByte(element);
599  }
600 
608  public byte GetByte(string name)
609  {
610  Type foundType;
611  object element = GetElement(name, out foundType);
612  if ((object)foundType == typeof(byte))
613  {
614  return (byte)element;
615  }
616  return m_converter.ToByte(element);
617  }
618 
626  public short GetInt16(string name)
627  {
628  Type foundType;
629  object element = GetElement(name, out foundType);
630  if ((object)foundType == typeof(short))
631  {
632  return (short)element;
633  }
634  return m_converter.ToInt16(element);
635  }
636 
644  [CLSCompliant(false)]
645  public ushort GetUInt16(string name)
646  {
647  Type foundType;
648  object element = GetElement(name, out foundType);
649  if ((object)foundType == typeof(ushort))
650  {
651  return (ushort)element;
652  }
653  return m_converter.ToUInt16(element);
654  }
655 
663  public int GetInt32(string name)
664  {
665  Type foundType;
666  object element = GetElement(name, out foundType);
667  if ((object)foundType == typeof(int))
668  {
669  return (int)element;
670  }
671  return m_converter.ToInt32(element);
672  }
673 
681  [CLSCompliant(false)]
682  public uint GetUInt32(string name)
683  {
684  Type foundType;
685  object element = GetElement(name, out foundType);
686  if ((object)foundType == typeof(uint))
687  {
688  return (uint)element;
689  }
690  return m_converter.ToUInt32(element);
691  }
692 
700  public long GetInt64(string name)
701  {
702  Type foundType;
703  object element = GetElement(name, out foundType);
704  if ((object)foundType == typeof(long))
705  {
706  return (long)element;
707  }
708  return m_converter.ToInt64(element);
709  }
710 
718  [CLSCompliant(false)]
719  public ulong GetUInt64(string name)
720  {
721  Type foundType;
722  object element = GetElement(name, out foundType);
723  if ((object)foundType == typeof(ulong))
724  {
725  return (ulong)element;
726  }
727  return m_converter.ToUInt64(element);
728  }
729 
737  public float GetSingle(string name)
738  {
739  Type foundType;
740  object element = GetElement(name, out foundType);
741  if ((object)foundType == typeof(float))
742  {
743  return (float)element;
744  }
745  return m_converter.ToSingle(element);
746  }
747 
755  public double GetDouble(string name)
756  {
757  Type foundType;
758  object element = GetElement(name, out foundType);
759  if ((object)foundType == typeof(double))
760  {
761  return (double)element;
762  }
763  return m_converter.ToDouble(element);
764  }
765 
773  public decimal GetDecimal(string name)
774  {
775  Type foundType;
776  object element = GetElement(name, out foundType);
777  if ((object)foundType == typeof(decimal))
778  {
779  return (decimal)element;
780  }
781  return m_converter.ToDecimal(element);
782  }
783 
791  public DateTime GetDateTime(string name)
792  {
793  Type foundType;
794  object element = GetElement(name, out foundType);
795  if ((object)foundType == typeof(DateTime))
796  {
797  return (DateTime)element;
798  }
799  return m_converter.ToDateTime(element);
800  }
801 
809  public string GetString(string name)
810  {
811  Type foundType;
812  object element = GetElement(name, out foundType);
813  if ((object)foundType == typeof(string) || element == null)
814  {
815  return (string)element;
816  }
817  return m_converter.ToString(element);
818  }
819  }
820 }
virtual Assembly Assembly
Gets the appropriate T:System.Reflection.Assembly for this instance of T:System.Reflection....
Definition: Module.cs:133
void AddValue(string name, sbyte value)
Adds an 8-bit signed integer value into the T:System.Runtime.Serialization.SerializationInfo store.
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
Provides the connection between an instance of T:System.Runtime.Serialization.SerializationInfo and t...
long GetInt64(string name)
Retrieves a 64-bit signed integer value from the T:System.Runtime.Serialization.SerializationInfo sto...
DateTime ToDateTime(object value)
Converts a value to a T:System.DateTime.
ulong GetUInt64(string name)
Retrieves a 64-bit unsigned integer value from the T:System.Runtime.Serialization....
float GetSingle(string name)
Retrieves a single-precision floating-point value from the T:System.Runtime.Serialization....
sbyte GetSByte(string name)
Retrieves an 8-bit signed integer value from the T:System.Runtime.Serialization.SerializationInfo sto...
abstract string FullName
Gets the fully qualified name of the type, including its namespace but not its assembly.
Definition: Type.cs:153
virtual string FullName
Gets the display name of the assembly.
Definition: Assembly.cs:49
StringComparison
Specifies the culture, case, and sort rules to be used by certain overloads of the M:System....
bool ToBoolean(object value)
Converts a value to a T:System.Boolean.
ushort ToUInt16(object value)
Converts a value to a 16-bit unsigned integer.
bool GetBoolean(string name)
Retrieves a Boolean value from the T:System.Runtime.Serialization.SerializationInfo store.
Provides base functionality for proxies.
Definition: RealProxy.cs:22
void SetType(Type type)
Sets the T:System.Type of the object to serialize.
Definition: __Canon.cs:3
sbyte ToSByte(object value)
Converts a value to a T:System.SByte.
SerializationInfoEnumerator GetEnumerator()
Returns a T:System.Runtime.Serialization.SerializationInfoEnumerator used to iterate through the name...
void AddValue(string name, char value)
Adds a Unicode character value into the T:System.Runtime.Serialization.SerializationInfo store.
string FullTypeName
Gets or sets the full name of the T:System.Type to serialize.
void AddValue(string name, int value)
Adds a 32-bit signed integer value into the T:System.Runtime.Serialization.SerializationInfo store.
Represents an instant in time, typically expressed as a date and time of day. To browse the ....
Definition: DateTime.cs:13
double ToDouble(object value)
Converts a value to a double-precision floating-point number.
ulong ToUInt64(object value)
Converts a value to a 64-bit unsigned integer.
void AddValue(string name, short value)
Adds a 16-bit signed integer value into the T:System.Runtime.Serialization.SerializationInfo store.
SerializationInfo(Type type, IFormatterConverter converter)
Creates a new instance of the T:System.Runtime.Serialization.SerializationInfo class.
long ToInt64(object value)
Converts a value to a 64-bit signed integer.
Type ObjectType
Returns the type of the object to be serialized.
bool ContainsKey(TKey key)
Determines whether the T:System.Collections.Generic.Dictionary`2 contains the specified key.
Definition: Dictionary.cs:1303
string ToString(object value)
Converts a value to a T:System.String.
char GetChar(string name)
Retrieves a Unicode character value from the T:System.Runtime.Serialization.SerializationInfo store.
string AssemblyName
Gets or sets the assembly name of the type to serialize during serialization only.
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
void AddValue(string name, decimal value)
Adds a decimal value into the T:System.Runtime.Serialization.SerializationInfo store.
void AddValue(string name, object value, Type type)
Adds a value into the T:System.Runtime.Serialization.SerializationInfo store, where value is associa...
string Name
Gets or sets the simple name of the assembly. This is usually, but not necessarily,...
Definition: AssemblyName.cs:51
void AddValue(string name, ushort value)
Adds a 16-bit unsigned integer value into the T:System.Runtime.Serialization.SerializationInfo store.
float ToSingle(object value)
Converts a value to a single-precision floating-point number.
SerializationInfo(Type type, IFormatterConverter converter, bool requireSameTokenInPartialTrust)
Initializes a new instance of the T:System.Runtime.Serialization.SerializationInfo class.
short ToInt16(object value)
Converts a value to a 16-bit signed integer.
Defines the underlying structure of all code access permissions.
void AddValue(string name, byte value)
Adds an 8-bit unsigned integer value into the T:System.Runtime.Serialization.SerializationInfo store.
void AddValue(string name, float value)
Adds a single-precision floating-point value into the T:System.Runtime.Serialization....
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition: Array.cs:17
abstract Assembly Assembly
Gets the T:System.Reflection.Assembly in which the type is declared. For generic types,...
Definition: Type.cs:131
Represents type declarations: class types, interface types, array types, value types,...
Definition: Type.cs:18
short GetInt16(string name)
Retrieves a 16-bit signed integer value from the T:System.Runtime.Serialization.SerializationInfo sto...
The exception thrown when an error occurs during serialization or deserialization.
void AddValue(string name, ulong value)
Adds a 64-bit unsigned integer value into the T:System.Runtime.Serialization.SerializationInfo store.
Stores all the data needed to serialize or deserialize an object. This class cannot be inherited.
void AddValue(string name, DateTime value)
Adds a T:System.DateTime value into the T:System.Runtime.Serialization.SerializationInfo store.
static bool IsTransparentProxy(object proxy)
Returns a Boolean value that indicates whether the given object is a transparent proxy or a real obje...
uint ToUInt32(object value)
Converts a value to a 32-bit unsigned integer.
Describes an assembly's unique identity in full.
Definition: AssemblyName.cs:19
void AddValue(string name, object value)
Adds the specified object into the T:System.Runtime.Serialization.SerializationInfo store,...
The exception that is thrown when one of the arguments provided to a method is not valid.
void Demand()
Forces a T:System.Security.SecurityException at run time if all callers higher in the call stack have...
static RealProxy GetRealProxy(object proxy)
Returns the real proxy backing the specified transparent proxy.
decimal GetDecimal(string name)
Retrieves a decimal value from the T:System.Runtime.Serialization.SerializationInfo store.
static void Copy(Array sourceArray, Array destinationArray, int length)
Copies a range of elements from an T:System.Array starting at the first element and pastes them into ...
Definition: Array.cs:1275
char ToChar(object value)
Converts a value to a Unicode character.
bool IsFullTypeNameSetExplicit
Gets whether the full type name has been explicitly set.
uint GetUInt32(string name)
Retrieves a 32-bit unsigned integer value from the T:System.Runtime.Serialization....
int MemberCount
Gets the number of members that have been added to the T:System.Runtime.Serialization....
string GetString(string name)
Retrieves a T:System.String value from the T:System.Runtime.Serialization.SerializationInfo store.
ushort GetUInt16(string name)
Retrieves a 16-bit unsigned integer value from the T:System.Runtime.Serialization....
void AddValue(string name, long value)
Adds a 64-bit signed integer value into the T:System.Runtime.Serialization.SerializationInfo store.
object GetValue(string name, Type type)
Retrieves a value from the T:System.Runtime.Serialization.SerializationInfo store.
decimal ToDecimal(object value)
Converts a value to a T:System.Decimal.
bool IsAssemblyNameSetExplicit
Gets whether the assembly name has been explicitly set.
object Convert(object value, Type type)
Converts a value to the given T:System.Type.
void AddValue(string name, bool value)
Adds a Boolean value into the T:System.Runtime.Serialization.SerializationInfo store.
void Add(TKey key, TValue value)
Adds the specified key and value to the dictionary.
Definition: Dictionary.cs:1244
int GetInt32(string name)
Retrieves a 32-bit signed integer value from the T:System.Runtime.Serialization.SerializationInfo sto...
DateTime GetDateTime(string name)
Retrieves a T:System.DateTime value from the T:System.Runtime.Serialization.SerializationInfo store.
abstract new Module Module
Gets the module (the DLL) in which the current T:System.Type is defined.
Definition: Type.cs:123
byte GetByte(string name)
Retrieves an 8-bit unsigned integer value from the T:System.Runtime.Serialization....
double GetDouble(string name)
Retrieves a double-precision floating-point value from the T:System.Runtime.Serialization....
Provides several methods for using and publishing remoted objects and proxies. This class cannot be i...
void AddValue(string name, double value)
Adds a double-precision floating-point value into the T:System.Runtime.Serialization....
Provides a formatter-friendly mechanism for parsing the data in T:System.Runtime.Serialization....
bool TryGetValue(TKey key, out TValue value)
Gets the value associated with the specified key.
Definition: Dictionary.cs:1624
int ToInt32(object value)
Converts a value to a 32-bit signed integer.
byte ToByte(object value)
Converts a value to an 8-bit unsigned integer.
void AddValue(string name, uint value)
Adds a 32-bit unsigned integer value into the T:System.Runtime.Serialization.SerializationInfo store.
byte [] GetPublicKeyToken()
Gets the public key token, which is the last 8 bytes of the SHA-1 hash of the public key under which ...
virtual bool IsAssignableFrom(Type c)
Determines whether an instance of a specified type can be assigned to an instance of the current type...
Definition: Type.cs:2707