Example #1
0
        public static T[] DeserializeArray <T>(this BinaryReader @this, int count)
            where T : IManualSerializer, new()
        {
            var array = FastActivator <T> .CreateArray(count);

            for (int i = 0; i < count; i++)
            {
                array[i] = @this.Deserialize <T>();
            }
            return(array);
        }
Example #2
0
        public static T[] ReadEnums <T>(this BinaryReader @this, int count)
            where T : struct, IComparable, IConvertible
        {
            var type = typeof(T);

            if (!type.GetTypeInfo().IsEnum)
            {
                throw new ArgumentException("T is not an enum");
            }

            var array = FastActivator <T> .CreateArray(count);

            for (int i = 0; i < count; i++)
            {
                array[i] = @this.ReadEnum <T>();
            }
            return(array);
        }
        /// <summary>
        /// Reads an array of primitive value types (for arrays with zero-based indexing).
        /// </summary>
        /// <param name="stream">Stream to read the array from.</param>
        /// <param name="type">Type of an array element.</param>
        /// <param name="elementSize">Size of an array element.</param>
        /// <returns>The read array.</returns>
        private Array ReadArrayOfPrimitives(Stream stream, Type type, int elementSize)
        {
            // read array length
            int length = Leb128EncodingHelper.ReadInt32(stream);
            int size   = length * elementSize;

            // read array data
            Array array = FastActivator.CreateArray(type, length);

            if (mTempBuffer_BigBuffer.Length < length)
            {
                mTempBuffer_BigBuffer = new byte[size];
            }
            stream.Read(mTempBuffer_BigBuffer, 0, size);
            Buffer.BlockCopy(mTempBuffer_BigBuffer, 0, array, 0, size);

            mDeserializedObjectIdTable.Add(mNextDeserializedObjectId++, array);
            return(array);
        }
        /// <summary>
        /// Reads an array of objects (for arrays with zero-based indexing).
        /// </summary>
        /// <param name="stream">Stream to read the array from.</param>
        /// <param name="context">Context object to pass to an internal/external object serializer class.</param>
        /// <returns>The read array.</returns>
        /// <exception cref="SerializationException">Stream ended unexpectedly.</exception>
        private object ReadArrayOfObjects(Stream stream, object context)
        {
            // assembly and type metadata has been read already

            Type t = mCurrentDeserializedType.Type;

            // read array length
            int length = Leb128EncodingHelper.ReadInt32(stream);

            // read array elements
            Array array = FastActivator.CreateArray(t, length);

            for (int i = 0; i < length; i++)
            {
                array.SetValue(InnerDeserialize(stream, context), i);
            }

            mDeserializedObjectIdTable.Add(mNextDeserializedObjectId++, array);
            return(array);
        }
 public void CreateArray_Class(int count)
 {
     TestClass <int>[] expected = new TestClass <int> [count];
     TestClass <int>[] actual   = (TestClass <int>[])FastActivator.CreateArray(typeof(TestClass <int>), count);
     Assert.Equal(expected, actual);
 }
Example #6
0
        public static T[] ReadArray <T>(this BinaryReader @this, int count)
            where T : struct, IComparable, IConvertible
        {
            var type  = typeof(T);
            var array = FastActivator <T> .CreateArray(count);

            byte[] data;

            switch (type.GetTypeCode())
            {
            case TypeCode.Boolean:
                data = @this.ReadBytes(sizeof(bool) * count);
                break;

            case TypeCode.Char:
                data = @this.ReadBytes(sizeof(char) * count);
                break;

            case TypeCode.Byte:
                data = @this.ReadBytes(sizeof(byte) * count);
                break;

            case TypeCode.SByte:
                data = @this.ReadBytes(sizeof(sbyte) * count);
                break;

            case TypeCode.Int16:
                data = @this.ReadBytes(sizeof(short) * count);
                break;

            case TypeCode.Int32:
                data = @this.ReadBytes(sizeof(int) * count);
                break;

            case TypeCode.Int64:
                data = @this.ReadBytes(sizeof(long) * count);
                break;

            case TypeCode.UInt16:
                data = @this.ReadBytes(sizeof(ushort) * count);
                break;

            case TypeCode.UInt32:
                data = @this.ReadBytes(sizeof(uint) * count);
                break;

            case TypeCode.UInt64:
                data = @this.ReadBytes(sizeof(ulong) * count);
                break;

            case TypeCode.Single:
                data = @this.ReadBytes(sizeof(float) * count);
                break;

            case TypeCode.Double:
                data = @this.ReadBytes(sizeof(double) * count);
                break;

            case TypeCode.Decimal:
                data = @this.ReadBytes(sizeof(decimal) * count);
                break;

            default:
                throw new NotSupportedException("Type is not supported");
            }

            System.Buffer.BlockCopy(data, 0, array, 0, data.Length);
            return(array);
        }