Пример #1
0
 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");
     }
 }
Пример #2
0
 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);
     }
 }
Пример #3
0
        //NOTE: C# doesn't support template specialization, so to kind of mimick that we:
        //		1) use overloading (meaning even for Deserialize we use out, instead of just returning)
        //		2) use runtime reflection to bind to the given extension method (done only once, using static constructors)
        public void Serialize <T>(T value)
        {
            //primitive types will be handled here...
            if (Bit_Conversion <T> .Is_Supported)
            {
                var bytes = Bit_Conversion <T> .To_Bytes(value);

                if (swap_endianness)
                {
                    Array.Reverse(bytes);
                }
                stream.Write(bytes, 0, bytes.Length);
            }
            else
            {
                //...everything else will go via their extension, and come back to this method for individual fields
                Linear_Serialization_Helper <T> .Serialize(this, value);
            }
        }
Пример #4
0
        public static byte[] To_Bytes(object obj)
        {
            Type type = obj.GetType();

            if (type == typeof(Byte))
            {
                return(Bit_Conversion <Byte> .To_Bytes((Byte)obj));
            }
            else if (type == typeof(SByte))
            {
                return(Bit_Conversion <SByte> .To_Bytes((SByte)obj));
            }
            else if (type == typeof(Boolean))
            {
                return(Bit_Conversion <Boolean> .To_Bytes((Boolean)obj));
            }
            else if (type == typeof(Int16))
            {
                return(Bit_Conversion <Int16> .To_Bytes((Int16)obj));
            }
            else if (type == typeof(UInt16))
            {
                return(Bit_Conversion <UInt16> .To_Bytes((UInt16)obj));
            }
            else if (type == typeof(Int32))
            {
                return(Bit_Conversion <Int32> .To_Bytes((Int32)obj));
            }
            else if (type == typeof(UInt32))
            {
                return(Bit_Conversion <UInt32> .To_Bytes((UInt32)obj));
            }
            else if (type == typeof(Int64))
            {
                return(Bit_Conversion <Int64> .To_Bytes((Int64)obj));
            }
            else if (type == typeof(UInt64))
            {
                return(Bit_Conversion <UInt64> .To_Bytes((UInt64)obj));
            }
            else if (type == typeof(Single))
            {
                return(Bit_Conversion <Single> .To_Bytes((Single)obj));
            }
            else if (type == typeof(Double))
            {
                return(Bit_Conversion <Double> .To_Bytes((Double)obj));
            }
            else if (type.IsEnum)
            {
                return(To_Bytes(Convert_Enum_To_Underlying_Type((Enum)obj)));
            }
            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");
            }
        }