Exemple #1
0
        public static void Write(this BinaryWriter writer, IList value, BinaryUtility.TypeCodes elementTypeCode)
        {
            if (elementTypeCode == BinaryUtility.TypeCodes.Other || BinaryUtility.IsGenericTypeCode(elementTypeCode))
            {
                writer.Write(value.GetType().GetGenericArguments()[0]);
            }

            if (elementTypeCode == BinaryUtility.TypeCodes.Other || BinaryUtility.IsReferenceTypeCode(elementTypeCode))
            {
                writer.Write(value.Count);

                for (int i = 0; i < value.Count; i++)
                {
                    writer.Write(value[i]);
                }
            }
            else
            {
                writer.Write(value.Count);

                for (int i = 0; i < value.Count; i++)
                {
                    writer.Write(value[i], elementTypeCode);
                }
            }
        }
Exemple #2
0
        public static void Write(this BinaryWriter writer, IList value)
        {
            var elementTypeCode = BinaryUtility.ToTypeCode(value.GetType().GetGenericArguments()[0]);

            writer.Write((byte)elementTypeCode);
            writer.Write(value, elementTypeCode);
        }
Exemple #3
0
        public static void Write(this BinaryWriter writer, Array value, BinaryUtility.TypeCodes elementTypeCode)
        {
            if (elementTypeCode == BinaryUtility.TypeCodes.Other || BinaryUtility.IsGenericTypeCode(elementTypeCode))
            {
                writer.Write(value.GetType().GetElementType());
            }

            if (BinaryUtility.IsReferenceTypeCode(elementTypeCode))
            {
                writer.Write(value.Length);

                for (int i = 0; i < value.Length; i++)
                {
                    writer.Write(value.GetValue(i));
                }
            }
            else
            {
                writer.Write(value.Length);

                for (int i = 0; i < value.Length; i++)
                {
                    writer.Write(value.GetValue(i), elementTypeCode);
                }
            }
        }
Exemple #4
0
        public static void Write(this BinaryWriter writer, Array value)
        {
            var typeCode = BinaryUtility.ToTypeCode(value.GetType().GetElementType());

            writer.Write((byte)typeCode);
            writer.Write(value, typeCode);
        }
Exemple #5
0
        public static void Write(this BinaryWriter writer, object value)
        {
            var typeCode = BinaryUtility.ToTypeCode(value);

            writer.Write((byte)typeCode);
            writer.Write(value, typeCode);
        }
Exemple #6
0
        static IList ReadObjectList(this BinaryReader reader, BinaryUtility.TypeCodes elementTypeCode)
        {
            Type elementType = BinaryUtility.ToType(elementTypeCode) ?? reader.ReadType();
            int  count       = reader.ReadInt32();
            var  list        = (IList)Activator.CreateInstance(typeof(List <>).MakeGenericType(elementType), count);

            for (int i = 0; i < count; i++)
            {
                list.Add(reader.ReadObject());
            }

            return(list);
        }
Exemple #7
0
        static Array ReadObjectArray(this BinaryReader reader, BinaryUtility.TypeCodes elementTypeCode)
        {
            Type elementType = BinaryUtility.ToType(elementTypeCode) ?? reader.ReadType();
            int  length      = reader.ReadInt32();
            var  array       = Array.CreateInstance(elementType, length);

            for (int i = 0; i < length; i++)
            {
                array.SetValue(reader.ReadObject(), i);
            }

            return(array);
        }
Exemple #8
0
        static object ReadOther(this BinaryReader reader)
        {
            ushort serializerId = reader.ReadUInt16();
            var    serializer   = BinaryUtility.GetSerializer(BinaryUtility.ToType());

            if (serializer == null)
            {
                return(null);
            }
            else
            {
                return(serializer.Deserialize(reader));
            }
        }
Exemple #9
0
        static void WriteOther(this BinaryWriter writer, object value)
        {
            var serializer = BinaryUtility.GetSerializer(value.GetType());

            if (serializer == null)
            {
                writer.Write(ushort.MaxValue - 1);
            }
            else
            {
                writer.Write(serializer.TypeIdentifier);
                serializer.Serialize(writer, value);
            }
        }