/// <summary> /// Stores the given value into the instance's stream; the serializer /// is inferred from TValue and format. /// </summary> /// <remarks>Needs to be public to be callable thru reflection in Silverlight</remarks> public static void AppendExtendValueTyped <TSource, TValue>( TSource instance, int tag, DataFormat format, TValue value) where TSource : class, IExtensible { Serializer <TSource> .CheckTagNotInUse(tag); Property <TValue, TValue> prop = PropertyFactory.CreatePassThru <TValue>(tag, ref format); IExtension extn = instance.GetExtensionObject(true); if (extn == null) { throw new InvalidOperationException("No extension object available; appended data would be lost."); } Stream stream = extn.BeginAppend(); try { SerializationContext ctx = new SerializationContext(stream, null); ctx.Push(instance); // for recursion detection prop.Serialize(value, ctx); ctx.Pop(instance); ctx.Flush(); extn.EndAppend(stream, true); } catch { extn.EndAppend(stream, false); throw; } }
// 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); } }
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); } }
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 }
public int Serialize(IExtension extension) { if (extension == null) return 0; Stream stream = extension.BeginAppend(); try { SerializationContext ctx = new SerializationContext(stream, null); int len = Serialize(ctx); ctx.Flush(); extension.EndAppend(stream, true); return len; } catch { extension.EndAppend(stream, false); throw; } }
public int Serialize(IExtension extension) { if (extension == null) { return(0); } Stream stream = extension.BeginAppend(); try { SerializationContext ctx = new SerializationContext(stream, null); int len = Serialize(ctx); ctx.Flush(); extension.EndAppend(stream, true); return(len); } catch { extension.EndAppend(stream, false); throw; } }
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 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); } }
/// <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); } }
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); } }
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 }