using System;
namespace ProtoBuf
{
///
/// Additional information about a serialization operation
///
public sealed class SerializationContext
{
private bool frozen;
internal void Freeze() { frozen = true;}
private void ThrowIfFrozen() { if (frozen) throw new InvalidOperationException("The serialization-context cannot be changed once it is in use"); }
private object context;
///
/// Gets or sets a user-defined object containing additional information about this serialization/deserialization operation.
///
public object Context
{
get { return context; }
set { if (context != value) { ThrowIfFrozen(); context = value; } }
}
private static readonly SerializationContext @default;
static SerializationContext()
{
@default = new SerializationContext();
@default.Freeze();
}
///
/// A default SerializationContext, with minimal information.
///
internal static SerializationContext Default { get {return @default;}}
#if PLAT_BINARYFORMATTER || (SILVERLIGHT && NET_4_0)
#if !(WINRT || PHONE7 || PHONE8 || COREFX)
private System.Runtime.Serialization.StreamingContextStates state = System.Runtime.Serialization.StreamingContextStates.Persistence;
///
/// Gets or sets the source or destination of the transmitted data.
///
public System.Runtime.Serialization.StreamingContextStates State
{
get { return state; }
set { if (state != value) { ThrowIfFrozen(); state = value; } }
}
#endif
///
/// Convert a SerializationContext to a StreamingContext
///
public static implicit operator System.Runtime.Serialization.StreamingContext(SerializationContext ctx)
{
#if WINRT || PHONE7 || PHONE8 || COREFX
return new System.Runtime.Serialization.StreamingContext();
#else
if (ctx == null) return new System.Runtime.Serialization.StreamingContext(System.Runtime.Serialization.StreamingContextStates.Persistence);
return new System.Runtime.Serialization.StreamingContext(ctx.state, ctx.context);
#endif
}
///
/// Convert a StreamingContext to a SerializationContext
///
public static implicit operator SerializationContext (System.Runtime.Serialization.StreamingContext ctx)
{
SerializationContext result = new SerializationContext();
#if !(WINRT || PHONE7 || PHONE8 || COREFX)
result.Context = ctx.Context;
result.State = ctx.State;
#endif
return result;
}
#endif
}
}