Ejemplo n.º 1
0
        private Variant ReadArray(EncodingKind kind)
        {
            int length;

            switch (kind)
            {
            case EncodingKind.Array_0:
                length = 0;
                break;

            case EncodingKind.Array_1:
                length = 1;
                break;

            case EncodingKind.Array_2:
                length = 2;
                break;

            case EncodingKind.Array_3:
                length = 3;
                break;

            default:
                length = (int)this.ReadCompressedUInt();
                break;
            }

            // SUBTLE: If it was a primitive array, only the EncodingKind byte of the element type was written, instead of encoding as a type.
            var elementKind = (EncodingKind)_reader.ReadByte();

            Type elementType;

            if (StreamObjectWriter.s_reverseTypeMap.TryGetValue(elementKind, out elementType))
            {
                return(Variant.FromArray(this.ReadPrimitiveTypeArrayElements(elementType, elementKind, length)));
            }
            else
            {
                // custom type case
                elementType = this.ReadType(elementKind);

                _constructionStack.Push(Construction.CreateArrayConstruction(elementType, length, _valueStack.Count));
                return(Variant.None);
            }
        }
Ejemplo n.º 2
0
        private Variant ReadArray(EncodingKind kind)
        {
            int length;

            switch (kind)
            {
            case EncodingKind.Array_0:
                length = 0;
                break;

            case EncodingKind.Array_1:
                length = 1;
                break;

            case EncodingKind.Array_2:
                length = 2;
                break;

            case EncodingKind.Array_3:
                length = 3;
                break;

            default:
                length = (int)this.ReadCompressedUInt();
                break;
            }

            // SUBTLE: If it was a primitive array, only the EncodingKind byte of the element type was written, instead of encoding as a type.
            var elementKind = (EncodingKind)_reader.ReadByte();

            Type elementType;

            if (StreamObjectWriter.s_reverseTypeMap.TryGetValue(elementKind, out elementType))
            {
                return(Variant.FromArray(this.ReadPrimitiveTypeArrayElements(elementType, elementKind, length)));
            }
            else
            {
                // custom type case
                elementType = this.ReadType(elementKind);

                if (_recursive)
                {
                    // recursive: create instance and read elements next in stream
                    Array array = Array.CreateInstance(elementType, length);

                    for (int i = 0; i < length; ++i)
                    {
                        var value = this.ReadValue();
                        array.SetValue(value, i);
                    }

                    return(Variant.FromObject(array));
                }
                else
                {
                    // non-recursive: remember construction info to be used later when all elements are available
                    _constructionStack.Push(Construction.CreateArrayConstruction(elementType, length, _valueStack.Count));
                    return(Variant.None);
                }
            }
        }