public object Deserialize(Stream source, string className)
    {
        CLS_Type_Class sClass = m_clsEnv.GetTypeByKeywordQuiet(className) as CLS_Type_Class;

        if (sClass == null)
        {
            throw new NotImplementedException("未实现类型: " + className);
        }

        if (!sClass.compiled)
        {
            RuntimeCompilerClass(className);
        }

        CLS_Content.Value retVal    = (sClass.function as SType).New(m_clsContent, m_emptyParams);
        SInstance         sInstance = (SInstance)retVal.value;

        ProtoReader reader = null;

        try
        {
            reader = ProtoReader.Create(source, null, null, ProtoReader.TO_EOF);
            ReadSInstance(reader, sInstance, m_clsEnv);
            reader.CheckFullyConsumed();
            return(sInstance);
        }
        finally
        {
            ProtoReader.Recycle(reader);
        }
    }
Exemple #2
0
        /// <summary>
        /// All this does is call GetExtendedValuesTyped with the correct type for "instance";
        /// this ensures that we don't get issues with subclasses declaring conflicting types -
        /// the caller must respect the fields defined for the type they pass in.
        /// </summary>
        internal static IEnumerable GetExtendedValues(TypeModel model, Type type, IExtensible instance, int tag, DataFormat format, bool singleton, bool allowDefinedTag)
        {
            if (instance == null)
            {
                throw new ArgumentNullException(nameof(instance));
            }
            if (tag <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(tag));
            }
            IExtension extn = instance.GetExtensionObject(false);

            if (extn == null)
            {
                yield break;
            }

            Stream      stream = extn.BeginQuery();
            object      value  = null;
            ProtoReader reader = null;

            try
            {
                SerializationContext ctx = new SerializationContext();
                reader = ProtoReader.Create(stream, model, ctx, ProtoReader.TO_EOF);
                while (model.TryDeserializeAuxiliaryType(reader, format, tag, type, ref value, true, true, false, false, null) && value != null)
                {
                    if (!singleton)
                    {
                        yield return(value);

                        value = null; // fresh item each time
                    }
                }
                if (singleton && value != null)
                {
                    yield return(value);
                }
            }
            finally
            {
                ProtoReader.Recycle(reader);
                extn.EndQuery(stream);
            }
        }
Exemple #3
0
        object Deserialize <T>(TypeModel model, byte[] data)
        {
            Type type = typeof(T);
            int  key  = model.GetKey(ref type);

            using (MemoryStream ms = new MemoryStream(data))
            {
                ProtoReader reader = null;
                try
                {
                    reader = ProtoReader.Create(ms, model, null, ProtoReader.TO_EOF);
                    return(model.Deserialize(key, null, reader));
                }
                finally
                {
                    ProtoReader.Recycle(reader);
                }
            }
        }
        public object Deserialize(Stream source, object value, Type type, int length, SerializationContext context)
        {
            bool        noAutoCreate = PrepareDeserialize(value, ref type);
            ProtoReader protoReader  = null;

            try
            {
                protoReader = ProtoReader.Create(source, this, context, length);
                if (value != null)
                {
                    protoReader.SetRootObject(value);
                }
                object result = DeserializeCore(protoReader, type, value, noAutoCreate);
                protoReader.CheckFullyConsumed();
                return(result);
            }
            finally
            {
                ProtoReader.Recycle(protoReader);
            }
        }
Exemple #5
0
        public object Deserialize(Stream source, object value, Type type, SerializationContext context)
        {
            bool        autoCreate = this.PrepareDeserialize(value, ref type);
            ProtoReader reader     = null;
            object      result;

            try
            {
                reader = ProtoReader.Create(source, this, context, -1);
                if (value != null)
                {
                    reader.SetRootObject(value);
                }
                object obj = this.DeserializeCore(reader, type, value, autoCreate);
                reader.CheckFullyConsumed();
                result = obj;
            }
            finally
            {
                ProtoReader.Recycle(reader);
            }
            return(result);
        }
Exemple #6
0
        public static T Deserialize(Stream stream, T obj)
        {
            if (_invoke == null)
            {
                return(_default(stream));
            }

            obj = ProtoSerializeHelper <T> .CreateInstance(obj);

            ProtoReader source = null;

            try
            {
                source = ProtoReader.Create(stream, RuntimeTypeModel.Default, null, ProtoReader.TO_EOF);
                source.SetRootObject(obj);
                obj = _invoke(source, obj);
                source.CheckFullyConsumed();
                return(obj);
            }
            finally
            {
                ProtoReader.Recycle(source);
            }
        }
        /// <summary>
        /// Reads the body of an object
        /// </summary>
        public override object ReadObject(System.Xml.XmlDictionaryReader reader, bool verifyObjectName)
        {
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }
            reader.MoveToContent();
            bool isSelfClosed = reader.IsEmptyElement, isNil = reader.GetAttribute("nil") == "true";

            reader.ReadStartElement(PROTO_ELEMENT);

            // explicitly null
            if (isNil)
            {
                if (!isSelfClosed)
                {
                    reader.ReadEndElement();
                }
                return(null);
            }
            if (isSelfClosed) // no real content
            {
                if (isList || isEnum)
                {
                    return(model.Deserialize(Stream.Null, null, type, null));
                }
                ProtoReader protoReader = null;
                try
                {
                    protoReader = ProtoReader.Create(Stream.Null, model, null, ProtoReader.TO_EOF);
                    return(model.Deserialize(key, null, protoReader));
                }
                finally
                {
                    ProtoReader.Recycle(protoReader);
                }
            }

            object result;

            Helpers.DebugAssert(reader.CanReadBinaryContent, "CanReadBinaryContent");
            using (MemoryStream ms = new MemoryStream(reader.ReadContentAsBase64()))
            {
                if (isList || isEnum)
                {
                    result = model.Deserialize(ms, null, type, null);
                }
                else
                {
                    ProtoReader protoReader = null;
                    try
                    {
                        protoReader = ProtoReader.Create(ms, model, null, ProtoReader.TO_EOF);
                        result      = model.Deserialize(key, null, protoReader);
                    }
                    finally
                    {
                        ProtoReader.Recycle(protoReader);
                    }
                }
            }
            reader.ReadEndElement();
            return(result);
        }
Exemple #8
0
        /// <summary>
        /// All this does is call GetExtendedValuesTyped with the correct type for "instance";
        /// this ensures that we don't get issues with subclasses declaring conflicting types -
        /// the caller must respect the fields defined for the type they pass in.
        /// </summary>
        internal static IEnumerable GetExtendedValues(TypeModel model, Type type, IExtensible instance, int tag, DataFormat format, bool singleton, bool allowDefinedTag)
        {
#if FEAT_IKVM
            throw new NotSupportedException();
#else
            if (instance == null)
            {
                throw new ArgumentNullException("instance");
            }
            if (tag <= 0)
            {
                throw new ArgumentOutOfRangeException("tag");
            }
            IExtension extn = instance.GetExtensionObject(false);

            if (extn == null)
            {
#if FX11
                return(new object[0]);
#else
                yield break;
#endif
            }

#if FX11
            BasicList result = new BasicList();
#endif
            Stream      stream = extn.BeginQuery();
            object      value  = null;
            ProtoReader reader = null;
            try {
                SerializationContext ctx = new SerializationContext();
                reader = ProtoReader.Create(stream, model, ctx, ProtoReader.TO_EOF);
                while (model.TryDeserializeAuxiliaryType(reader, format, tag, type, ref value, true, false, false, false) && value != null)
                {
                    if (!singleton)
                    {
#if FX11
                        result.Add(value);
#else
                        yield return(value);
#endif
                        value = null; // fresh item each time
                    }
                }
                if (singleton && value != null)
                {
#if FX11
                    result.Add(value);
#else
                    yield return(value);
#endif
                }
#if FX11
                object[] resultArr = new object[result.Count];
                result.CopyTo(resultArr, 0);
                return(resultArr);
#endif
            } finally {
                ProtoReader.Recycle(reader);
                extn.EndQuery(stream);
            }
#endif
        }
        private object DeserializeWithLengthPrefix(Stream source, object value, Type type, PrefixStyle style, int expectedField, Serializer.TypeResolver resolver, out int bytesRead, out bool haveObject, SerializationContext context)
        {
            haveObject = false;
            bytesRead  = 0;
            if (type == null && (style != PrefixStyle.Base128 || resolver == null))
            {
                throw new InvalidOperationException("A type must be provided unless base-128 prefixing is being used in combination with a resolver");
            }
            int  num;
            bool flag2;

            do
            {
                bool flag = expectedField > 0 || resolver != null;
                num = ProtoReader.ReadLengthPrefix(source, flag, style, out int fieldNumber, out int bytesRead2);
                if (bytesRead2 == 0)
                {
                    return(value);
                }
                bytesRead += bytesRead2;
                if (num < 0)
                {
                    return(value);
                }
                if (style == PrefixStyle.Base128)
                {
                    if (flag && expectedField == 0 && type == null && resolver != null)
                    {
                        type  = resolver(fieldNumber);
                        flag2 = (type == null);
                    }
                    else
                    {
                        flag2 = (expectedField != fieldNumber);
                    }
                }
                else
                {
                    flag2 = false;
                }
                if (flag2)
                {
                    if (num == int.MaxValue)
                    {
                        throw new InvalidOperationException();
                    }
                    ProtoReader.Seek(source, num, null);
                    bytesRead += num;
                }
            }while (flag2);
            ProtoReader protoReader = null;

            try
            {
                protoReader = ProtoReader.Create(source, this, context, num);
                int key = GetKey(ref type);
                if (key >= 0 && !Helpers.IsEnum(type))
                {
                    value = Deserialize(key, value, protoReader);
                }
                else if (!TryDeserializeAuxiliaryType(protoReader, DataFormat.Default, 1, type, ref value, skipOtherFields: true, asListItem: false, autoCreate: true, insideList: false) && num != 0)
                {
                    ThrowUnexpectedType(type);
                }
                bytesRead += protoReader.Position;
                haveObject = true;
                return(value);
            }
            finally
            {
                ProtoReader.Recycle(protoReader);
            }
        }
        public object DeepClone(object value)
        {
            if (value == null)
            {
                return(null);
            }
            Type type = value.GetType();
            int  key  = GetKey(ref type);

            if (key >= 0 && !Helpers.IsEnum(type))
            {
                using (MemoryStream memoryStream = new MemoryStream())
                {
                    using (ProtoWriter protoWriter = new ProtoWriter(memoryStream, this, null))
                    {
                        protoWriter.SetRootObject(value);
                        Serialize(key, value, protoWriter);
                        protoWriter.Close();
                    }
                    memoryStream.Position = 0L;
                    ProtoReader protoReader = null;
                    try
                    {
                        protoReader = ProtoReader.Create(memoryStream, this, null, -1);
                        return(Deserialize(key, null, protoReader));
                    }
                    finally
                    {
                        ProtoReader.Recycle(protoReader);
                    }
                }
            }
            if (type == typeof(byte[]))
            {
                byte[] array  = (byte[])value;
                byte[] array2 = new byte[array.Length];
                Helpers.BlockCopy(array, 0, array2, 0, array.Length);
                return(array2);
            }
            if (GetWireType(Helpers.GetTypeCode(type), DataFormat.Default, ref type, out int modelKey) != WireType.None && modelKey < 0)
            {
                return(value);
            }
            using (MemoryStream memoryStream2 = new MemoryStream())
            {
                using (ProtoWriter protoWriter2 = new ProtoWriter(memoryStream2, this, null))
                {
                    if (!TrySerializeAuxiliaryType(protoWriter2, type, DataFormat.Default, 1, value, isInsideList: false))
                    {
                        ThrowUnexpectedType(type);
                    }
                    protoWriter2.Close();
                }
                memoryStream2.Position = 0L;
                ProtoReader reader = null;
                try
                {
                    reader = ProtoReader.Create(memoryStream2, this, null, -1);
                    value  = null;
                    TryDeserializeAuxiliaryType(reader, DataFormat.Default, 1, type, ref value, skipOtherFields: true, asListItem: false, autoCreate: true, insideList: false);
                    return(value);
                }
                finally
                {
                    ProtoReader.Recycle(reader);
                }
            }
        }
Exemple #11
0
        private object DeserializeWithLengthPrefix(Stream source, object value, Type type, PrefixStyle style, int expectedField, Serializer.TypeResolver resolver, out int bytesRead, out bool haveObject, SerializationContext context)
        {
            haveObject = false;
            bytesRead  = 0;
            if (type == null && (style != PrefixStyle.Base128 || resolver == null))
            {
                throw new InvalidOperationException("A type must be provided unless base-128 prefixing is being used in combination with a resolver");
            }
            while (true)
            {
                bool expectPrefix = expectedField > 0 || resolver != null;
                int  actualField;
                int  tmpBytesRead;
                int  len = ProtoReader.ReadLengthPrefix(source, expectPrefix, style, out actualField, out tmpBytesRead);
                if (tmpBytesRead == 0)
                {
                    break;
                }
                bytesRead += tmpBytesRead;
                if (len < 0)
                {
                    return(value);
                }
                bool skip;
                if (style == PrefixStyle.Base128)
                {
                    if (expectPrefix && expectedField == 0 && type == null && resolver != null)
                    {
                        type = resolver(actualField);
                        skip = (type == null);
                    }
                    else
                    {
                        skip = (expectedField != actualField);
                    }
                }
                else
                {
                    skip = false;
                }
                if (skip)
                {
                    if (len == 2147483647)
                    {
                        goto Block_12;
                    }
                    ProtoReader.Seek(source, len, null);
                    bytesRead += len;
                }
                if (!skip)
                {
                    goto Block_13;
                }
            }
            return(value);

Block_12:
            throw new InvalidOperationException();
Block_13:
            ProtoReader reader = null;
            object result;

            try
            {
                int len;
                reader = ProtoReader.Create(source, this, context, len);
                int key = this.GetKey(ref type);
                if (key >= 0 && !Helpers.IsEnum(type))
                {
                    value = this.Deserialize(key, value, reader);
                }
                else
                {
                    if (!this.TryDeserializeAuxiliaryType(reader, DataFormat.Default, 1, type, ref value, true, false, true, false) && len != 0)
                    {
                        TypeModel.ThrowUnexpectedType(type);
                    }
                }
                bytesRead += reader.Position;
                haveObject = true;
                result     = value;
            }
            finally
            {
                ProtoReader.Recycle(reader);
            }
            return(result);
        }
Exemple #12
0
        public object DeepClone(object value)
        {
            if (value == null)
            {
                return(null);
            }
            Type   type = value.GetType();
            int    key  = this.GetKey(ref type);
            object result;

            if (key >= 0 && !Helpers.IsEnum(type))
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    using (ProtoWriter writer = new ProtoWriter(ms, this, null))
                    {
                        writer.SetRootObject(value);
                        this.Serialize(key, value, writer);
                        writer.Close();
                    }
                    ms.Position = 0L;
                    ProtoReader reader = null;
                    try
                    {
                        reader = ProtoReader.Create(ms, this, null, -1);
                        result = this.Deserialize(key, null, reader);
                        return(result);
                    }
                    finally
                    {
                        ProtoReader.Recycle(reader);
                    }
                }
            }
            if (type == typeof(byte[]))
            {
                byte[] orig  = (byte[])value;
                byte[] clone = new byte[orig.Length];
                Helpers.BlockCopy(orig, 0, clone, 0, orig.Length);
                return(clone);
            }
            int modelKey;

            if (this.GetWireType(Helpers.GetTypeCode(type), DataFormat.Default, ref type, out modelKey) != WireType.None && modelKey < 0)
            {
                return(value);
            }
            using (MemoryStream ms2 = new MemoryStream())
            {
                using (ProtoWriter writer2 = new ProtoWriter(ms2, this, null))
                {
                    if (!this.TrySerializeAuxiliaryType(writer2, type, DataFormat.Default, 1, value, false))
                    {
                        TypeModel.ThrowUnexpectedType(type);
                    }
                    writer2.Close();
                }
                ms2.Position = 0L;
                ProtoReader reader2 = null;
                try
                {
                    reader2 = ProtoReader.Create(ms2, this, null, -1);
                    value   = null;
                    this.TryDeserializeAuxiliaryType(reader2, DataFormat.Default, 1, type, ref value, true, false, true, false);
                    result = value;
                }
                finally
                {
                    ProtoReader.Recycle(reader2);
                }
            }
            return(result);
        }