public static object From_Bytes(Type type, byte[] buffer, int index = 0) { if (type == typeof(Byte)) { return(Bit_Conversion <Byte> .From_Bytes(buffer, index)); } else if (type == typeof(SByte)) { return(Bit_Conversion <SByte> .From_Bytes(buffer, index)); } else if (type == typeof(Boolean)) { return(Bit_Conversion <Boolean> .From_Bytes(buffer, index)); } else if (type == typeof(Int16)) { return(Bit_Conversion <Int16> .From_Bytes(buffer, index)); } else if (type == typeof(UInt16)) { return(Bit_Conversion <UInt16> .From_Bytes(buffer, index)); } else if (type == typeof(Int32)) { return(Bit_Conversion <Int32> .From_Bytes(buffer, index)); } else if (type == typeof(UInt32)) { return(Bit_Conversion <UInt32> .From_Bytes(buffer, index)); } else if (type == typeof(Int64)) { return(Bit_Conversion <Int64> .From_Bytes(buffer, index)); } else if (type == typeof(UInt64)) { return(Bit_Conversion <UInt64> .From_Bytes(buffer, index)); } else if (type == typeof(Single)) { return(Bit_Conversion <Single> .From_Bytes(buffer, index)); } else if (type == typeof(Double)) { return(Bit_Conversion <Double> .From_Bytes(buffer, index)); } else if (type.IsEnum) { var obj = From_Bytes(type.GetEnumUnderlyingType(), buffer, index); return(Convert.ChangeType(obj, type)); } else if (type == typeof(Char)) { throw new NotSupportedException("Chars should be converted via a given Encoding!"); } else { throw new NotSupportedException("non templated Bit_Conversion can only be used for primitive types"); } }
public void Deserialize <T>(out T value) { if (Bit_Conversion <T> .Is_Supported) { var bytes = stream.Read_Bytes(Bit_Conversion <T> .Bytes_Needed); if (swap_endianness) { Array.Reverse(bytes); } value = Bit_Conversion <T> .From_Bytes(bytes); } else { Linear_Serialization_Helper <T> .Deserialize(this, out value); } }