Example #1
0
        public static void AppendExtensionData(IExtensible instance, ProtoWriter writer)
        {
            if (instance == null)
            {
                throw new ArgumentNullException("instance");
            }
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }
            if (writer.wireType != WireType.None)
            {
                throw ProtoWriter.CreateException(writer);
            }
            IExtension extn = instance.GetExtensionObject(false);

            if (extn != null)
            {
                Stream source = extn.BeginQuery();
                try
                {
                    ProtoWriter.CopyRawFromStream(source, writer);
                }
                finally
                {
                    extn.EndQuery(source);
                }
            }
        }
Example #2
0
        internal static void AppendExtendValue <TValue>(TypeModel model, IExtensible instance, int tag, DataFormat format, object value)
        {
            if (instance == null)
            {
                throw new ArgumentNullException("instance");
            }
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            //TODO: CheckTagNotInUse
            //model.CheckTagNotInUse(tag);

            // obtain the extension object and prepare to write
            IExtension extn = instance.GetExtensionObject(true);

            if (extn == null)
            {
                throw new InvalidOperationException("No extension object available; appended data would be lost.");
            }
            bool   commit = false;
            Stream stream = extn.BeginAppend();

            try {
                using (ProtoWriter writer = new ProtoWriter(stream, model, null)) {
                    model.TrySerializeAuxiliaryType(writer, null, format, tag, value, false);
                    writer.Close();
                }
                commit = true;
            }
            finally {
                extn.EndAppend(stream, commit);
            }
        }
        /// <summary>
        /// Copies any extension data stored for the instance to the underlying stream
        /// </summary>
        public static void AppendExtensionData(IExtensible instance, ProtoWriter writer)
        {
            if (instance == null)
            {
                throw new ArgumentNullException("instance");
            }
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }
            // we expect the writer to be raw here; the extension data will have the
            // header detail, so we'll copy it implicitly
            if (writer.wireType != WireType.None)
            {
                throw CreateException(writer);
            }

            IExtension extn = instance.GetExtensionObject(false);

            if (extn != null)
            {
                // unusually we *don't* want "using" here; the "finally" does that, with
                // the extension object being responsible for disposal etc
                Stream source = extn.BeginQuery();
                try
                {
                    CopyRawFromStream(source, writer);
                }
                finally { extn.EndQuery(source); }
            }
        }
Example #4
0
        public static void AppendExtensionData(IExtensible instance, ProtoWriter writer)
        {
            if (instance == null)
            {
                throw new ArgumentNullException("instance");
            }
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }
            if (writer.wireType != WireType.None)
            {
                throw CreateException(writer);
            }
            IExtension extensionObject = instance.GetExtensionObject(createIfMissing: false);

            if (extensionObject != null)
            {
                Stream stream = extensionObject.BeginQuery();
                try
                {
                    CopyRawFromStream(stream, writer);
                }
                finally
                {
                    extensionObject.EndQuery(stream);
                }
            }
        }
Example #5
0
        public void AppendExtendValue(TypeModel model, IExtensible instance, int tag, BinaryDataFormat format, object value)
        {
#if FEAT_IKVM
            throw new NotSupportedException();
#else
            if (instance == null)
            {
                throw new ArgumentNullException(nameof(instance));
            }
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            // obtain the extension object and prepare to write
            IExtension extn = instance.GetExtensionObject(true);
            if (extn == null)
            {
                throw new InvalidOperationException("No extension object available; appended data would be lost.");
            }
            bool   commit = false;
            Stream stream = extn.BeginAppend();
            try {
                using (ProtoWriter writer = new ProtoWriter(stream, model, null)) {
                    model.TrySerializeAuxiliaryType(writer, null, format, tag, value, false, true);
                    writer.Close();
                }
                commit = true;
            }
            finally {
                extn.EndAppend(stream, commit);
            }
#endif
        }
Example #6
0
        // Token: 0x06003293 RID: 12947 RVA: 0x00127460 File Offset: 0x00125860
        internal static void AppendExtendValue(TypeModel model, IExtensible instance, int tag, DataFormat format, object value)
        {
            if (instance == null)
            {
                throw new ArgumentNullException("instance");
            }
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            IExtension extensionObject = instance.GetExtensionObject(true);

            if (extensionObject == null)
            {
                throw new InvalidOperationException("No extension object available; appended data would be lost.");
            }
            bool   commit = false;
            Stream stream = extensionObject.BeginAppend();

            try
            {
                using (ProtoWriter protoWriter = new ProtoWriter(stream, model, null))
                {
                    model.TrySerializeAuxiliaryType(protoWriter, null, format, tag, value, false);
                    protoWriter.Close();
                }
                commit = true;
            }
            finally
            {
                extensionObject.EndAppend(stream, commit);
            }
        }
Example #7
0
        internal static int Serialize(T instance, SerializationContext context)
        {
            // check for inheritance; if the instance is a subclass, then we
            // should serialize the sub-type first, allowing for more efficient
            // deserialization; note that we don't push the instance onto the
            // stack yet - we'll do that within each instance (otherwise deep
            // items could incorrectly count as cyclic).
            Type actualType = instance.GetType();
            int  total = 0, len;

            Callback(CallbackType.BeforeSerialization, instance);

            if (actualType != typeof(T))
            {
                bool subclassFound = false;
                foreach (KeyValuePair <Type, Property <T, T> > subclass in subclasses)
                {
                    if (subclass.Key.IsAssignableFrom(actualType))
                    {
                        total        += subclass.Value.Serialize(instance, context);
                        subclassFound = true;
                        break;
                    }
                }
                if (!subclassFound)
                {
                    throw new ProtoException("Unexpected type found during serialization; types must be included with ProtoIncludeAttribute; "
                                             + "found " + actualType.Name + " passed as " + typeof(T).Name);
                }
            }

            context.Push(instance);
            for (int i = 0; i < writeProps.Length; i++)
            {
                // note that this serialization includes the headers...
                total += writeProps[i].Serialize(instance, context);
            }
            IExtensible extensible = instance as IExtensible;
            IExtension  extra      = extensible == null ? null : extensible.GetExtensionObject(false);

            if (extra != null && (len = extra.GetLength()) > 0)
            {
                Stream extraStream = extra.BeginQuery();
                try
                {
                    context.WriteFrom(extraStream, len);
                    total += len;
                }
                finally
                {
                    extra.EndQuery(extraStream);
                }
            }
            context.Pop(instance);
            Callback(CallbackType.AfterSerialization, instance);
            return(total);
        }
Example #8
0
#pragma warning disable RCS1163, IDE0060 // Unused parameter.
        /// <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)
#pragma warning restore RCS1163, IDE0060 // Unused parameter.
        {
            model ??= TypeModel.DefaultModel;
            if (instance == null)
            {
                ThrowHelper.ThrowArgumentNullException(nameof(instance));
            }
            if (tag <= 0)
            {
                ThrowHelper.ThrowArgumentOutOfRangeException(nameof(tag));
            }
#pragma warning disable RCS1227 // Validate arguments correctly.
            IExtension extn = instance.GetExtensionObject(false);
#pragma warning restore RCS1227 // Validate arguments correctly.

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

            Stream stream = extn.BeginQuery();
            try
            {
                object value             = null;
                SerializationContext ctx = new SerializationContext();
                var state = ProtoReader.State.Create(stream, model, ctx, ProtoReader.TO_EOF).Solidify();
                try
                {
                    while (model.TryDeserializeAuxiliaryType(ref state, 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
                {
                    state.Dispose();
                }
            }
            finally
            {
                extn.EndQuery(stream);
            }
        }
Example #9
0
        internal static void AppendExtendValue(TypeModel model, IExtensible instance, int tag, DataFormat format, object value)
        {
            model ??= TypeModel.DefaultModel;
            if (instance == null)
            {
                ThrowHelper.ThrowArgumentNullException(nameof(instance));
            }
            if (value == null)
            {
                ThrowHelper.ThrowArgumentNullException(nameof(value));
            }

            // TODO
            //model.CheckTagNotInUse(tag);

            // obtain the extension object and prepare to write
            IExtension extn = instance.GetExtensionObject(true);

            if (extn == null)
            {
                ThrowHelper.ThrowInvalidOperationException("No extension object available; appended data would be lost.");
            }
            bool   commit = false;
            Stream stream = extn.BeginAppend();

            try
            {
                var state = ProtoWriter.State.Create(stream, model, null);
                try
                {
                    model.TrySerializeAuxiliaryType(ref state, null, format, tag, value, false, null);
                    state.Close();
                }
                catch
                {
                    state.Abandon();
                    throw;
                }
                finally
                {
                    state.Dispose();
                }
#pragma warning disable IDE0059 // Unnecessary assignment of a value - the rule is wrong; this matters
                commit = true;
#pragma warning restore IDE0059
            }
            finally
            {
                extn.EndAppend(stream, commit);
            }
        }
        internal static IEnumerable GetExtendedValues(TypeModel model, Type type, IExtensible instance, int tag, DataFormat format, bool singleton, bool allowDefinedTag)
        {
            if (instance == null)
            {
                throw new ArgumentNullException("instance");
            }
            if (tag <= 0)
            {
                throw new ArgumentOutOfRangeException("tag");
            }
            IExtension extensionObject = instance.GetExtensionObject(false);

            if (extensionObject != null)
            {
                Stream      stream      = extensionObject.BeginQuery();
                object      obj         = null;
                ProtoReader protoReader = null;
                try
                {
                    SerializationContext serializationContext = new SerializationContext();
                    protoReader = ProtoReader.Create(stream, model, serializationContext, -1);
                    while (model.TryDeserializeAuxiliaryType(protoReader, format, tag, type, ref obj, true, false, false, false) && obj != null)
                    {
                        if (singleton)
                        {
                            continue;
                        }
                        yield return(obj);

                        obj = null;
                    }
                    if (!singleton || obj == null)
                    {
                        goto Label0;
                    }
                    yield return(obj);
                }
                finally
                {
                    ProtoReader.Recycle(protoReader);
                    extensionObject.EndQuery(stream);
                }
            }
Label0:
            yield break;
        }
Example #11
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, false, 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);
            }
        }
Example #12
0
        public static byte[] ToBytes(this IExtensible msg, int msgid)
        {
            int length = sizeof(short) + 2 + msg.GetExtensionObject(true).GetLength();
            var buffer = PooledByteBufferAllocator.Default.Buffer(length + sizeof(int)).WithOrder(ByteOrder.LittleEndian);

            buffer.WriteInt(length);
            buffer.WriteShort(msgid);

            MemoryStream stream = new MemoryStream(buffer.Array, buffer.WriterIndex + buffer.ArrayOffset, msg.GetExtensionObject(true).GetLength());

            Serializer.NonGeneric.Serialize(stream, msg);
            stream.Flush();
            buffer.SetWriterIndex(sizeof(int) + length);

            var ret = buffer.ReadBytes(buffer.ReadableBytes).ToArray();

            buffer.Release();
            return(ret);
        }
Example #13
0
        internal static IEnumerable GetExtendedValues(TypeModel model, Type type, IExtensible instance, int tag, DataFormat format, bool singleton, bool allowDefinedTag)
        {
            if (instance == null)
            {
                throw new ArgumentNullException("instance");
            }
            if (tag <= 0)
            {
                throw new ArgumentOutOfRangeException("tag");
            }
            IExtension extn = instance.GetExtensionObject(createIfMissing: false);

            if (extn != null)
            {
                Stream      stream = extn.BeginQuery();
                object      value  = null;
                ProtoReader reader = null;
                try
                {
                    SerializationContext context = new SerializationContext();
                    reader = ProtoReader.Create(stream, model, context, -1);
                    while (model.TryDeserializeAuxiliaryType(reader, format, tag, type, ref value, skipOtherFields: true, asListItem: false, autoCreate: false, insideList: false) && value != null)
                    {
                        if (!singleton)
                        {
                            yield return(value);

                            value = null;
                        }
                    }
                    if (singleton && value != null)
                    {
                        yield return(value);
                    }
                }
                finally
                {
                    ProtoReader.Recycle(reader);
                    extn.EndQuery(stream);
                }
            }
        }
        internal static void AppendExtendValue(TypeModel model, IExtensible instance, int tag, DataFormat format, object value)
        {
            model ??= TypeModel.DefaultModel;
            if (instance is null) ThrowHelper.ThrowArgumentNullException(nameof(instance));
            if (value is null) ThrowHelper.ThrowArgumentNullException(nameof(value));

            // TODO
            //model.CheckTagNotInUse(tag);

            // obtain the extension object and prepare to write
            IExtension extn = instance.GetExtensionObject(true);
            if (extn is null) ThrowHelper.ThrowInvalidOperationException("No extension object available; appended data would be lost.");
            bool commit = false;
            Stream stream = extn.BeginAppend();
            try
            {
                var state = ProtoWriter.State.Create(stream, model, null);
                try
                {
                    model.TrySerializeAuxiliaryType(ref state, null, format, tag, value, false, null, isRoot: false);
                    state.Close();
                }
                catch
                {
                    state.Abandon();
                    throw;
                }
                finally
                {
                    state.Dispose();
                }

                commit = true;
            }
            finally
            {
                extn.EndAppend(stream, commit);
            }
        }
Example #15
0
        static byte[] GetExtensionBytes(IExtensible obj)
        {
            Assert.IsNotNull(obj, "null extensible");
            IExtension extn = obj.GetExtensionObject(false);

            Assert.IsNotNull(extn, "no extension object");
            Stream s = extn.BeginQuery();

            try
            {
                using (MemoryStream ms = new MemoryStream()) {
                    int b; // really lazy clone...
                    while ((b = s.ReadByte()) >= 0)
                    {
                        ms.WriteByte((byte)b);
                    }
                    return(ms.ToArray());
                }
            } finally {
                extn.EndQuery(s);
            }
        }
Example #16
0
        /// <summary>
        /// Copies the current field into the instance as extension data
        /// </summary>
        public void AppendExtensionData(IExtensible instance)
        {
            if (instance == null)
            {
                throw new ArgumentNullException("instance");
            }
            IExtension extn   = instance.GetExtensionObject(true);
            bool       commit = false;
            // unusually we *don't* want "using" here; the "finally" does that, with
            // the extension object being responsible for disposal etc
            Stream dest = extn.BeginAppend();

            try
            {
                //TODO: replace this with stream-based, buffered raw copying
                using (ProtoWriter writer = new ProtoWriter(dest, model))
                {
                    AppendExtensionField(writer);
                    writer.Close();
                }
                commit = true;
            }
            finally { extn.EndAppend(dest, commit); }
        }
Example #17
0
        public void AppendExtensionData(IExtensible instance)
        {
            if (instance == null)
            {
                throw new ArgumentNullException("instance");
            }
            IExtension extensionObject = instance.GetExtensionObject(true);
            bool       commit          = false;
            Stream     stream          = extensionObject.BeginAppend();

            try
            {
                using (ProtoWriter protoWriter = new ProtoWriter(stream, this.model, null))
                {
                    this.AppendExtensionField(protoWriter);
                    protoWriter.Close();
                }
                commit = true;
            }
            finally
            {
                extensionObject.EndAppend(stream, commit);
            }
        }
Example #18
0
        /// <summary>
        /// Copies any extension data stored for the instance to the underlying stream
        /// </summary>
        public static void AppendExtensionData(IExtensible instance, ProtoWriter writer, ref State state)
        {
            if (instance == null)
            {
                throw new ArgumentNullException(nameof(instance));
            }
            if (writer == null)
            {
                throw new ArgumentNullException(nameof(writer));
            }
            // we expect the writer to be raw here; the extension data will have the
            // header detail, so we'll copy it implicitly
            if (writer.WireType != WireType.None)
            {
                throw CreateException(writer);
            }

            IExtension extn = instance.GetExtensionObject(false);

            if (extn != null)
            {
                // unusually we *don't* want "using" here; the "finally" does that, with
                // the extension object being responsible for disposal etc
                Stream source = extn.BeginQuery();
                try
                {
                    if (ProtoReader.TryConsumeSegmentRespectingPosition(source, out var data, ProtoReader.TO_EOF))
                    {
                        writer.ImplWriteBytes(ref state, data.Array, data.Offset, data.Count);
                        writer.Advance(data.Count);
                    }
                    else
                    {
                        writer.ImplCopyRawFromStream(ref state, source);
                    }
                }
 /// <summary>
 /// Copies the current field into the instance as extension data
 /// </summary>
 public void AppendExtensionData(IExtensible instance)
 {
     if (instance == null) throw new ArgumentNullException("instance");
     IExtension extn = instance.GetExtensionObject(true);
     bool commit = false;
     // unusually we *don't* want "using" here; the "finally" does that, with
     // the extension object being responsible for disposal etc
     Stream dest = extn.BeginAppend();
     try
     {
         //TODO: replace this with stream-based, buffered raw copying
         using (ProtoWriter writer = new ProtoWriter(dest, model))
         {
             AppendExtensionField(writer);
             writer.Close();
         }
         commit = true;
     }
     finally { extn.EndAppend(dest, commit); }
 }
Example #20
0
        internal static void Deserialize <TCreation>(ref T instance, SerializationContext context)
            where TCreation : class, T
        {
            uint prefix = 0;

            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
#if !CF
            try
            {
#endif
            if (instance != null)
            {
                Callback(CallbackType.BeforeDeserialization, instance);
            }

            context.Push();
            int propCount = readProps.Length;
            //context.CheckSpace();
            IExtensible extensible = instance as IExtensible;
            IExtension extn        = null;
            Property <T> prop      = propCount == 0 ? null : readProps[0];

            int lastIndex   = prop == null ? -1 : 0;
            uint lastPrefix = prop == null ? uint.MaxValue : prop.FieldPrefix;

            while (context.TryReadFieldPrefix(out prefix))
            {
                // scan for the correct property
                bool foundTag = false;
                if (prefix == lastPrefix)
                {
                    foundTag = true;
                }
                else if (prefix > lastPrefix)
                {
                    for (int i = lastIndex + 1; i < propCount; i++)
                    {
                        if (readProps[i].FieldPrefix == prefix)
                        {
                            prop       = readProps[i];
                            lastIndex  = i;
                            lastPrefix = prefix;
                            foundTag   = true;
                            break;
                        }
                        if (readProps[i].FieldPrefix > prefix)
                        {
                            break;                                        // too far
                        }
                    }
                }
                else
                {
                    for (int i = lastIndex - 1; i >= 0; i--)
                    {
                        if (readProps[i].FieldPrefix == prefix)
                        {
                            prop       = readProps[i];
                            lastIndex  = i;
                            lastPrefix = prefix;
                            foundTag   = true;
                            break;
                        }
                        if (readProps[i].FieldPrefix < prefix)
                        {
                            break;                                        // too far
                        }
                    }
                }

                if (!foundTag)
                {
                    // check for subclass creation
                    foreach (KeyValuePair <Type, Property <T, T> > subclass in subclasses)
                    {
                        // deserialize the nested data
                        if (prefix == subclass.Value.FieldPrefix)
                        {
                            foundTag = true;
                            instance = subclass.Value.DeserializeImpl(instance, context);
                            break;
                        }
                    }
                    if (foundTag)
                    {
                        continue;               // nothing more to do for this...
                    }
                }

                // not a sub-class, but *some* data there, so create an object
                if (instance == null)
                {
                    instance = ObjectFactory <TCreation> .Create();

                    Callback(CallbackType.ObjectCreation, instance);
                    extensible = instance as IExtensible;
                }
                if (foundTag)
                {
                    // found it by seeking; deserialize and continue

                    // ReSharper disable PossibleNullReferenceException
                    try
                    {
                        prop.Deserialize(instance, context);
                    }
                    catch (UnexpectedDataException ex)
                    {
                        if (extensible != null)
                        {
                            if (extn == null)
                            {
                                extn = extensible.GetExtensionObject(true);
                            }
                            ex.Serialize(extn);
                        }
                        // DON'T re-throw; we've handled this
                    }
                    // ReSharper restore PossibleNullReferenceException
                    continue;
                }

                WireType wireType;
                int      fieldTag;
                Serializer.ParseFieldToken(prefix, out wireType, out fieldTag);
                if (wireType == WireType.EndGroup)
                {
                    context.EndGroup(fieldTag);
                    break;     // this ends the entity, so stop the loop
                }

                // so we couldn't find it...
                if (extensible != null)
                {
                    if (extn == null)
                    {
                        extn = extensible.GetExtensionObject(true);
                    }
                    Stream extraStream = extn.BeginAppend();
                    try {
                        SerializationContext extraData = new SerializationContext(extraStream, null);

                        // copy the data into the output stream
                        // ReSharper disable PossibleNullReferenceException
                        extraData.EncodeUInt32(prefix);
                        // ReSharper restore PossibleNullReferenceException
                        ProcessExtraData(context, fieldTag, wireType, extraData);
                        extraData.Flush();
                        extn.EndAppend(extraStream, true);
                    } catch {
                        extn.EndAppend(extraStream, false);
                        throw;
                    }
                }
                else
                {
                    // unexpected fields for an inextensible object; discard the data
                    Serializer.SkipData(context, fieldTag, wireType);
                }
            }


            // final chance to create an instance - this only gets invoked for empty
            // messages (otherwise instance should already be non-null)
            if (instance == null)
            {
                instance = ObjectFactory <T> .Create();

                Callback(CallbackType.ObjectCreation, instance);
            }
            context.Pop();
            Callback(CallbackType.AfterDeserialization, instance);
#if !CF
        }

        catch (Exception ex)
        {
            const string ErrorDataKey = "protoSource";
            if (!ex.Data.Contains(ErrorDataKey))
            {
                ex.Data.Add(ErrorDataKey, string.Format("tag={0}; wire-type={1}; offset={2}; depth={3}; type={4}",
                                                        (int)(prefix >> 3), (WireType)(prefix & 7),
                                                        context.Position, context.Depth, typeof(T).FullName));
            }
            throw;
        }
#endif
        }
Example #21
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
        }
Example #22
0
 public void AppendExtensionData(IExtensible instance)
 {
     if (instance == null)
     {
         throw new ArgumentNullException("instance");
     }
     IExtension extensionObject = instance.GetExtensionObject(true);
     bool commit = false;
     Stream stream = extensionObject.BeginAppend();
     try
     {
         using (ProtoWriter protoWriter = new ProtoWriter(stream, this.model, null))
         {
             this.AppendExtensionField(protoWriter);
             protoWriter.Close();
         }
         commit = true;
     }
     finally
     {
         extensionObject.EndAppend(stream, commit);
     }
 }
Example #23
0
        /// <summary>
        /// Copies any extension data stored for the instance to the underlying stream
        /// </summary>
        public static void AppendExtensionData(IExtensible instance, ProtoWriter writer)
        {
            if (instance == null) throw new ArgumentNullException("instance");
            // we expect the writer to be raw here; the extension data will have the
            // header detail, so we'll copy it implicitly
            if(writer.wireType != WireType.None) throw CreateException(writer);

            IExtension extn = instance.GetExtensionObject(false);
            if (extn != null)
            {
                // unusually we *don't* want "using" here; the "finally" does that, with
                // the extension object being responsible for disposal etc
                Stream source = extn.BeginQuery();
                try
                {
                    CopyRawFromStream(source, writer);
                }
                finally { extn.EndQuery(source); }
            }
        }
Example #24
0
 public static void AppendExtensionData(IExtensible instance, ProtoWriter writer)
 {
     if (instance == null)
     {
         throw new ArgumentNullException("instance");
     }
     if (writer == null)
     {
         throw new ArgumentNullException("writer");
     }
     if (writer.wireType != WireType.None)
     {
         throw ProtoWriter.CreateException(writer);
     }
     IExtension extensionObject = instance.GetExtensionObject(false);
     if (extensionObject != null)
     {
         Stream stream = extensionObject.BeginQuery();
         try
         {
             ProtoWriter.CopyRawFromStream(stream, writer);
         }
         finally
         {
             extensionObject.EndQuery(stream);
         }
     }
 }