private Type DeserializeDictionaryType(StreamBuffer reader, out byte keyTypeCode, out byte valTypeCode) { keyTypeCode = reader.ReadByteAsByte(); valTypeCode = reader.ReadByteAsByte(); GpTypeV16 gpType = (GpTypeV16)keyTypeCode; GpTypeV16 gpType2 = (GpTypeV16)valTypeCode; Type type = (gpType != 0) ? this.GetTypeOfCode(keyTypeCode) : typeof(object); Type type2 = (gpType2 != 0) ? this.GetTypeOfCode(valTypeCode) : typeof(object); return(typeof(Dictionary <,>).MakeGenericType(type, type2)); }
private void SerializeDictionaryHeader(StreamBuffer writer, object dict, out bool setKeyType, out bool setValueType) { Type[] genericArguments = dict.GetType().GetGenericArguments(); setKeyType = (genericArguments[0] == typeof(object)); setValueType = (genericArguments[1] == typeof(object)); if (setKeyType) { writer.WriteByte(0); } else { GpTypeV16 codeOfType = this.GetCodeOfType(genericArguments[0]); if (codeOfType == GpTypeV16.Unknown || codeOfType == GpTypeV16.Dictionary) { throw new Exception("Unexpected - cannot serialize Dictionary with key type: " + genericArguments[0]); } writer.WriteByte((byte)codeOfType); } if (setValueType) { writer.WriteByte(0); } else { GpTypeV16 codeOfType2 = this.GetCodeOfType(genericArguments[1]); if (codeOfType2 == GpTypeV16.Unknown) { throw new Exception("Unexpected - cannot serialize Dictionary with value type: " + genericArguments[0]); } writer.WriteByte((byte)codeOfType2); if (codeOfType2 == GpTypeV16.Dictionary) { this.SerializeDictionaryHeader(writer, genericArguments[1]); } } }
private void SerializeArray(StreamBuffer dout, Array serObject, bool setType) { if (setType) { dout.WriteByte(121); } if (serObject.Length > 32767) { throw new NotSupportedException("String[] that exceed 32767 (short.MaxValue) entries are not supported. Yours is: " + serObject.Length); } this.SerializeShort(dout, (short)serObject.Length, false); Type elementType = serObject.GetType().GetElementType(); GpTypeV16 codeOfType = this.GetCodeOfType(elementType); if (codeOfType != 0) { dout.WriteByte((byte)codeOfType); if (codeOfType == GpTypeV16.Dictionary) { bool setKeyType = default(bool); bool setValueType = default(bool); this.SerializeDictionaryHeader(dout, (object)serObject, out setKeyType, out setValueType); for (int i = 0; i < serObject.Length; i++) { object value = serObject.GetValue(i); this.SerializeDictionaryElements(dout, value, setKeyType, setValueType); } } else { for (int j = 0; j < serObject.Length; j++) { object value2 = serObject.GetValue(j); this.Serialize(dout, value2, false); } } return; } CustomType customType = default(CustomType); if (Protocol.TypeDict.TryGetValue(elementType, out customType)) { dout.WriteByte(99); dout.WriteByte(customType.Code); int num = 0; short num2; long num3; while (true) { if (num < serObject.Length) { object value3 = serObject.GetValue(num); if (customType.SerializeStreamFunction == null) { byte[] array = customType.SerializeFunction(value3); this.SerializeShort(dout, (short)array.Length, false); dout.Write(array, 0, array.Length); } else { int position = dout.IntPosition; dout.IntPosition += 2; num2 = customType.SerializeStreamFunction(dout, value3); num3 = dout.IntPosition; dout.IntPosition = position; this.SerializeShort(dout, num2, false); dout.IntPosition += num2; if (dout.IntPosition != num3) { break; } } num++; continue; } return; } throw new Exception("Serialization failed. Stream position corrupted. Should be " + num3 + " is now: " + dout.IntPosition + " serializedLength: " + num2); } throw new NotSupportedException("cannot serialize array of type " + elementType); }