BinaryCommon.cs 4.4 KB

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