/// <summary> /// Serialize a IBinaryTypeMapped object. /// </summary> /// <param name="buffer">The buffer where to serialize the data.</param> /// <param name="start">The start index in the buffer where to serialize the data.</param> /// <param name="value">The object to serialize.</param> /// <returns>The number of serialized bytes.</returns> public uint ToBytes(ref Buffer buffer, uint start, IBinaryTypeMapped value) { uint written; CheckSerializationParameters(buffer, start); uint type_id; if (value != null) { if (typeToId.TryGetValue(value.GetType(), out type_id) && type_id != 0) { CheckDefaultConstructor(value.GetType()); written = ToBytes(ref buffer, start, type_id); written += value.ToBytes(this, ref buffer, start + written); } else { throw new SerializationException(string.Format("Type '{0}' does not have a valid mapped ID", value.GetType()), new Exception()); } } else { written = ToBytes(ref buffer, start, (uint)0); } return(written); }
/// <summary> /// Deserialize into an existing IBinaryTypeMapped object. /// </summary> /// <param name="buffer">The buffer containing the serialized data.</param> /// <param name="start">The start index in the buffer of the serialized object.</param> /// <param name="value">The deserialized object.</param> /// <returns>The number of deserialized bytes.</returns> public uint FromBytesOverwrite(Buffer buffer, uint start, IBinaryTypeMapped value) { uint type_id; CheckDeserializationParameters(buffer, start); uint read = FromBytes(buffer, start, out type_id); Type type; if (type_id != 0 && idToType.TryGetValue(type_id, out type)) { if (!type.IsAssignableFrom(value.GetType())) { throw new FormatException("The type of IBinaryTypeMapped object does not match the provided object."); } read += value.FromBytes(this, buffer, start + read); } else { // Nothing to do: we got nothing to deserialize and the value already exists, so returning null is not an option. // It would be great to notify the user, but we do have a mecanism for that, and raising an exception would stop // the deserialization, but this should just be a warning. } return(read); }
/// <summary> /// Deserialize a IBinaryTypeMapped object. /// </summary> /// <param name="buffer">The buffer containing the serialized data.</param> /// <param name="start">The start index in the buffer of the serialized object.</param> /// <param name="value">The deserialized object.</param> /// <returns>The number of deserialized bytes.</returns> public uint FromBytes(Buffer buffer, uint start, out IBinaryTypeMapped value) { uint type_id; CheckDeserializationParameters(buffer, start); uint read = FromBytes(buffer, start, out type_id); Type type; if (type_id != 0 && idToType.TryGetValue(type_id, out type)) { CallDefaultConstructor(type, out value); read += value.FromBytes(this, buffer, start + read); } else { value = null; } return(read); }
protected static void CallDefaultConstructor(Type type, out IBinaryTypeMapped value) { value = (IBinaryTypeMapped)CheckDefaultConstructor(type).Invoke(emptyParameters); }
public static uint GetTypeMappedId(IBinaryTypeMapped instance) { return(GetTypeMappedId(instance?.GetType())); }