BinaryCommon.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // BinaryCommon.cs
  2. //
  3. // Author:
  4. // Lluis Sanchez Gual ([email protected])
  5. //
  6. // (C) 2003 Lluis Sanchez Gual
  7. using System;
  8. namespace System.Runtime.Serialization.Formatters.Binary
  9. {
  10. internal class BinaryCommon
  11. {
  12. // Header present in all binary serializations
  13. public static byte[] BinaryHeader = new Byte[] {0,1,0,0,0,255,255,255,255,1,0,0,0,0,0,0,0};
  14. static Type[] _typeCodesToType;
  15. static byte[] _typeCodeMap;
  16. static BinaryCommon()
  17. {
  18. _typeCodesToType = new Type [19];
  19. _typeCodesToType[(int)BinaryTypeCode.Boolean] = typeof (Boolean);
  20. _typeCodesToType[(int)BinaryTypeCode.Byte] = typeof (Byte);
  21. _typeCodesToType[(int)BinaryTypeCode.Char] = typeof (Char);
  22. _typeCodesToType[(int)BinaryTypeCode.TimeSpan] = typeof (TimeSpan);
  23. _typeCodesToType[(int)BinaryTypeCode.DateTime] = typeof (DateTime);
  24. _typeCodesToType[(int)BinaryTypeCode.Decimal] = typeof (Decimal);
  25. _typeCodesToType[(int)BinaryTypeCode.Double] = typeof (Double);
  26. _typeCodesToType[(int)BinaryTypeCode.Int16] = typeof (Int16);
  27. _typeCodesToType[(int)BinaryTypeCode.Int32] = typeof (Int32);
  28. _typeCodesToType[(int)BinaryTypeCode.Int64] = typeof (Int64);
  29. _typeCodesToType[(int)BinaryTypeCode.SByte] = typeof (SByte);
  30. _typeCodesToType[(int)BinaryTypeCode.Single] = typeof (Single);
  31. _typeCodesToType[(int)BinaryTypeCode.UInt16] = typeof (UInt16);
  32. _typeCodesToType[(int)BinaryTypeCode.UInt32] = typeof (UInt32);
  33. _typeCodesToType[(int)BinaryTypeCode.UInt64] = typeof (UInt64);
  34. _typeCodesToType[(int)BinaryTypeCode.Null] = null;
  35. _typeCodesToType[(int)BinaryTypeCode.String] = typeof (string);
  36. _typeCodeMap = new byte[30];
  37. _typeCodeMap[(int)TypeCode.Boolean] = (byte) BinaryTypeCode.Boolean;
  38. _typeCodeMap[(int)TypeCode.Byte] = (byte) BinaryTypeCode.Byte;
  39. _typeCodeMap[(int)TypeCode.Char] = (byte) BinaryTypeCode.Char;
  40. _typeCodeMap[(int)TypeCode.DateTime] = (byte) BinaryTypeCode.DateTime;
  41. _typeCodeMap[(int)TypeCode.Decimal] = (byte) BinaryTypeCode.Decimal;
  42. _typeCodeMap[(int)TypeCode.Double] = (byte) BinaryTypeCode.Double;
  43. _typeCodeMap[(int)TypeCode.Int16] = (byte) BinaryTypeCode.Int16;
  44. _typeCodeMap[(int)TypeCode.Int32] = (byte) BinaryTypeCode.Int32;
  45. _typeCodeMap[(int)TypeCode.Int64] = (byte) BinaryTypeCode.Int64;
  46. _typeCodeMap[(int)TypeCode.SByte] = (byte) BinaryTypeCode.SByte;
  47. _typeCodeMap[(int)TypeCode.Single] = (byte) BinaryTypeCode.Single;
  48. _typeCodeMap[(int)TypeCode.UInt16] = (byte) BinaryTypeCode.UInt16;
  49. _typeCodeMap[(int)TypeCode.UInt32] = (byte) BinaryTypeCode.UInt32;
  50. _typeCodeMap[(int)TypeCode.UInt64] = (byte) BinaryTypeCode.UInt64;
  51. _typeCodeMap[(int)TypeCode.String] = (byte) BinaryTypeCode.String;
  52. // TimeStamp does not have a TypeCode, so it is managed as a special
  53. // case in GetTypeCode()
  54. }
  55. public static bool IsPrimitive (Type type)
  56. {
  57. return type.IsPrimitive ||
  58. type == typeof (DateTime) ||
  59. type == typeof (TimeSpan) ||
  60. type == typeof (Decimal);
  61. }
  62. public static byte GetTypeCode (Type type)
  63. {
  64. if (type == typeof(TimeSpan)) return (byte) BinaryTypeCode.TimeSpan;
  65. else return _typeCodeMap [(int)Type.GetTypeCode(type)];
  66. }
  67. public static Type GetTypeFromCode (int code)
  68. {
  69. return _typeCodesToType [code];
  70. }
  71. }
  72. internal enum BinaryElement : byte
  73. {
  74. Header = 0,
  75. RefTypeObject = 1,
  76. _Unknown1 = 2,
  77. _Unknown2 = 3,
  78. RuntimeObject = 4,
  79. ExternalObject = 5,
  80. String = 6,
  81. GenericArray = 7,
  82. BoxedPrimitiveTypeValue = 8,
  83. ObjectReference = 9,
  84. NullValue = 10,
  85. End = 11,
  86. Assembly = 12,
  87. ArrayFiller8b = 13,
  88. ArrayFiller32b = 14,
  89. ArrayOfPrimitiveType = 15,
  90. ArrayOfObject = 16,
  91. ArrayOfString = 17,
  92. Method = 18,
  93. _Unknown4 = 19,
  94. _Unknown5 = 20,
  95. MethodCall = 21,
  96. MethodResponse = 22
  97. }
  98. internal enum TypeTag : byte
  99. {
  100. PrimitiveType = 0,
  101. String = 1,
  102. ObjectType = 2,
  103. RuntimeType = 3,
  104. GenericType = 4,
  105. ArrayOfObject = 5,
  106. ArrayOfString = 6,
  107. ArrayOfPrimitiveType = 7
  108. }
  109. internal enum ArrayStructure : byte
  110. {
  111. SingleDimensional = 0,
  112. Jagged = 1,
  113. MultiDimensional = 2
  114. }
  115. internal enum MethodFlags : byte
  116. {
  117. NoArguments = 1,
  118. PrimitiveArguments = 2,
  119. ArgumentsInSimpleArray = 4,
  120. ArgumentsInMultiArray = 8,
  121. ExcludeLogicalCallContext = 16,
  122. IncludesLogicalCallContext = 64,
  123. IncludesSignature = 128,
  124. FormatMask = 15,
  125. NeedsInfoArrayMask = 4 + 8 + 64 + 128
  126. }
  127. internal enum ReturnTypeTag : byte
  128. {
  129. Null = 2,
  130. PrimitiveType = 8,
  131. ObjectType = 16,
  132. Exception = 32
  133. }
  134. enum BinaryTypeCode : byte
  135. {
  136. Boolean = 1,
  137. Byte = 2,
  138. Char = 3,
  139. Decimal = 5,
  140. Double = 6,
  141. Int16 = 7,
  142. Int32 = 8,
  143. Int64 = 9,
  144. SByte = 10,
  145. Single = 11,
  146. TimeSpan = 12,
  147. DateTime = 13,
  148. UInt16 = 14,
  149. UInt32 = 15,
  150. UInt64 = 16,
  151. Null = 17,
  152. String = 18
  153. }
  154. }