123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using ProtoBuf.Meta;
- namespace ProtoBuf
- {
-
-
-
-
-
-
- internal static class ExtensibleUtil
- {
- #if !NO_RUNTIME
-
-
-
-
-
- internal static IEnumerable<TValue> GetExtendedValues<TValue>(IExtensible instance, int tag, DataFormat format, bool singleton, bool allowDefinedTag)
- {
- foreach (TValue value in GetExtendedValues(RuntimeTypeModel.Default, typeof(TValue), instance, tag, format, singleton, allowDefinedTag))
- {
- yield return value;
- }
- }
- #endif
-
-
-
-
-
- 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;
- }
- }
- 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)
- {
- if (instance == null) throw new ArgumentNullException(nameof(instance));
- if (value == null) throw new ArgumentNullException(nameof(value));
-
-
-
- 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 = ProtoWriter.Create(stream, model, null))
- {
- model.TrySerializeAuxiliaryType(writer, null, format, tag, value, false, null);
- writer.Close();
- }
- commit = true;
- }
- finally
- {
- extn.EndAppend(stream, commit);
- }
- }
-
-
-
-
-
-
-
-
-
-
-
- }
- }
|