本文整理汇总了C#中Orleans.Serialization.BinaryTokenStreamWriter类的典型用法代码示例。如果您正苦于以下问题:C# BinaryTokenStreamWriter类的具体用法?C# BinaryTokenStreamWriter怎么用?C# BinaryTokenStreamWriter使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
BinaryTokenStreamWriter类属于Orleans.Serialization命名空间,在下文中一共展示了BinaryTokenStreamWriter类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Serialize
public void Serialize(object item, BinaryTokenStreamWriter writer, Type expectedType)
{
if (writer == null)
{
throw new ArgumentNullException("writer");
}
if (item == null)
{
writer.WriteNull();
return;
}
var formatter = new BinaryFormatter();
byte[] bytes;
using (var memoryStream = new MemoryStream())
{
formatter.Serialize(memoryStream, item);
memoryStream.Flush();
bytes = memoryStream.ToArray();
}
writer.Write(bytes.Length);
writer.Write(bytes);
}
示例2: Serialize
/// <summary>
/// Serializes an object to a binary stream
/// </summary>
/// <param name="item">The object to serialize</param>
/// <param name="writer">The <see cref="BinaryTokenStreamWriter"/></param>
/// <param name="expectedType">The type the deserializer should expect</param>
public void Serialize(object item, BinaryTokenStreamWriter writer, Type expectedType)
{
if (writer == null)
{
throw new ArgumentNullException("writer");
}
if (item == null)
{
// Special handling for null value.
// Since in this ProtobufSerializer we are usualy writing the data lengh as 4 bytes
// we also have to write the Null object as 4 bytes lengh of zero.
writer.Write(0);
return;
}
IMessage iMessage = item as IMessage;
if (iMessage == null)
{
throw new ArgumentException("The provided item for serialization in not an instance of " + typeof(IMessage), "item");
}
// The way we write the data is potentially in-efficinet,
// since we are first writing to ProtoBuff's internal CodedOutputStream
// and then take its internal byte[] and write it into out own BinaryTokenStreamWriter.
// Writing byte[] to BinaryTokenStreamWriter may sometimes copy the byte[] and sometimes just append ass ArraySegment without copy.
// In the former case it will be a secodnd copy.
// It would be more effecient to write directly into BinaryTokenStreamWriter
// but protobuff does not currently support writing directly into a given arbitary stream
// (it does support System.IO.Steam but BinaryTokenStreamWriter is not compatible with System.IO.Steam).
// Alternatively, we could force to always append to BinaryTokenStreamWriter, but that could create a lot of small ArraySegments.
// The plan is to ask the ProtoBuff team to add support for some "InputStream" interface, like Bond does.
byte[] outBytes = iMessage.ToByteArray();
writer.Write(outBytes.Length);
writer.Write(outBytes);
}
示例3: ILSerializer_AllowCopiedFieldsToDifferFromSerializedFields
public void ILSerializer_AllowCopiedFieldsToDifferFromSerializedFields()
{
var input = new FieldTest
{
One = 1,
Two = 2,
Three = 3
};
var generator = new ILSerializerGenerator();
var serializers = generator.GenerateSerializer(input.GetType(), f => f.Name != "One", f => f.Name != "Three");
var copy = (FieldTest)serializers.DeepCopy(input);
Assert.Equal(1, copy.One);
Assert.Equal(2, copy.Two);
Assert.Equal(0, copy.Three);
var writer = new BinaryTokenStreamWriter();
serializers.Serialize(input, writer, input.GetType());
var reader = new BinaryTokenStreamReader(writer.ToByteArray());
var deserialized = (FieldTest)serializers.Deserialize(input.GetType(), reader);
Assert.Equal(0, deserialized.One);
Assert.Equal(2, deserialized.Two);
Assert.Equal(3, deserialized.Three);
}
示例4: UniqueKeySerializationShouldReproduceAnIdenticalObject
public void UniqueKeySerializationShouldReproduceAnIdenticalObject()
{
{
var expected = UniqueKey.NewKey(Guid.NewGuid());
BinaryTokenStreamWriter writer = new BinaryTokenStreamWriter();
writer.Write(expected);
BinaryTokenStreamReader reader = new BinaryTokenStreamReader(writer.ToBytes());
var actual = reader.ReadUniqueKey();
Assert.AreEqual(expected, actual, "UniqueKey.Serialize() and UniqueKey.Deserialize() failed to reproduce an identical object (case #1).");
}
{
var kx = random.Next().ToString(CultureInfo.InvariantCulture);
var expected = UniqueKey.NewKey(Guid.NewGuid(), category: UniqueKey.Category.KeyExtGrain, keyExt: kx);
BinaryTokenStreamWriter writer = new BinaryTokenStreamWriter();
writer.Write(expected);
BinaryTokenStreamReader reader = new BinaryTokenStreamReader(writer.ToBytes());
var actual = reader.ReadUniqueKey();
Assert.AreEqual(expected, actual, "UniqueKey.Serialize() and UniqueKey.Deserialize() failed to reproduce an identical object (case #2).");
}
{
var kx = random.Next().ToString(CultureInfo.InvariantCulture) + new String('*', 400);
var expected = UniqueKey.NewKey(Guid.NewGuid(), category: UniqueKey.Category.KeyExtGrain, keyExt: kx);
BinaryTokenStreamWriter writer = new BinaryTokenStreamWriter();
writer.Write(expected);
BinaryTokenStreamReader reader = new BinaryTokenStreamReader(writer.ToBytes());
var actual = reader.ReadUniqueKey();
Assert.AreEqual(expected, actual, "UniqueKey.Serialize() and UniqueKey.Deserialize() failed to reproduce an identical object (case #3).");
}
}
示例5: Serialize
private static void Serialize(object input, BinaryTokenStreamWriter stream, Type expected)
{
SerializeCounter++;
var obj = input as ClassWithCustomSerializer;
stream.Write(obj.IntProperty);
stream.Write(obj.StringProperty);
}
示例6: SerializeProperties
/// <summary>
/// Serializes event data properties
/// </summary>
/// <param name="eventData"></param>
/// <returns></returns>
public static byte[] SerializeProperties(this EventData eventData)
{
var writeStream = new BinaryTokenStreamWriter();
SerializationManager.Serialize(eventData.Properties.Where(kvp => !SkipProperties.Contains(kvp.Key)).ToList(), writeStream);
var result = writeStream.ToByteArray();
writeStream.ReleaseBuffers();
return result;
}
示例7: VerifyUsingFallbackSerializer
public static void VerifyUsingFallbackSerializer(object ob)
{
var writer = new BinaryTokenStreamWriter();
SerializationManager.FallbackSerializer(ob, writer, ob.GetType());
var bytes = writer.ToByteArray();
var reader = new BinaryTokenStreamReader(bytes);
var serToken = reader.ReadToken();
Assert.Equal(SerializationTokenType.Fallback, serToken);
}
示例8: Serialize
/// <summary>
/// Tries to serialize an item.
/// </summary>
/// <param name="item">The instance of the object being serialized</param>
/// <param name="writer">The writer used for serialization</param>
/// <param name="expectedType">The type that the deserializer will expect</param>
public void Serialize(object item, BinaryTokenStreamWriter writer, Type expectedType)
{
if (item == null)
{
writer.WriteNull();
return;
}
var actualType = item.GetType();
this.WriteType(actualType, expectedType, writer);
this.serializers.Get(actualType).Serialize(item, writer, expectedType);
}
示例9: EventSequenceTokenV2_Serialize_IfNotNull
public void EventSequenceTokenV2_Serialize_IfNotNull()
{
var writer = new BinaryTokenStreamWriter();
var token = new EventSequenceTokenV2(long.MaxValue, int.MaxValue);
EventSequenceTokenV2.Serialize(token, writer, null);
var reader = new BinaryTokenStreamReader(writer.ToByteArray());
var deserialized = EventSequenceTokenV2.Deserialize(typeof(EventSequenceTokenV2), reader) as EventSequenceTokenV2;
Assert.NotNull(deserialized);
Assert.NotSame(token, deserialized);
Assert.Equal(token.EventIndex, deserialized.EventIndex);
Assert.Equal(token.SequenceNumber, deserialized.SequenceNumber);
}
示例10: WriteType
private void WriteType(Type actualType, Type expectedType, BinaryTokenStreamWriter writer)
{
if (actualType == expectedType)
{
writer.Write((byte)SerializationTokenType.ExpectedType);
}
else
{
writer.Write((byte)SerializationTokenType.NamedType);
writer.Write(actualType.AssemblyQualifiedName);
}
}
示例11: DeepCopy
/// <summary>
/// Creates a deep copy of an object
/// </summary>
/// <param name="source">The source object to be copy</param>
/// <returns>The copy that was created</returns>
public object DeepCopy(object source)
{
if (source == null)
{
return null;
}
var writer = new BinaryTokenStreamWriter();
Serialize(source, writer, source.GetType());
var retVal = Deserialize(source.GetType(), new BinaryTokenStreamReader(writer.ToByteArray()));
return retVal;
}
示例12: Serialize
/// <summary>
/// Serialize the event sequence token.
/// </summary>
/// <param name="untypedInput">The object to serialize.</param>
/// <param name="writer">The writer to write the binary stream to.</param>
/// <param name="expected">The expected type.</param>
public static void Serialize(object untypedInput, BinaryTokenStreamWriter writer, Type expected)
{
var typed = untypedInput as EventSequenceTokenV2;
if (typed == null)
{
writer.WriteNull();
return;
}
writer.Write(typed.SequenceNumber);
writer.Write(typed.EventIndex);
}
示例13: Reserialize
internal static object Reserialize(IMessageSerializer serializer, object message)
{
if (serializer == null)
return message;
var writer = new BinaryTokenStreamWriter();
serializer.Serialize(message, writer);
var bytes = writer.ToByteArray();
var reader = new BinaryTokenStreamReader(bytes);
return serializer.Deserialize(reader);
}
示例14: Serialize
internal static void Serialize(object obj, BinaryTokenStreamWriter stream, Type unused)
{
var envelope = (ResponseEnvelope)obj;
if (envelope.Result == null)
{
stream.Write(ResultToken.Null);
return;
}
stream.Write(ResultToken.Some);
MessageEnvelope.Serializer.Serialize(envelope.Result, stream);
}
示例15: AzureQueueBatchContainerV2_Serialize_IfNotNull
public void AzureQueueBatchContainerV2_Serialize_IfNotNull()
{
var container = CreateAzureQueueBatchContainer();
var writer = new BinaryTokenStreamWriter();
AzureQueueBatchContainerV2.Serialize(container, writer, null);
var reader = new BinaryTokenStreamReader(writer.ToByteArray());
var deserialized = AzureQueueBatchContainerV2.Deserialize(typeof(AzureQueueBatchContainer), reader) as AzureQueueBatchContainerV2;
ValidateIdenticalQueueBatchContainerButNotSame(container, deserialized);
writer = new BinaryTokenStreamWriter();
SerializationManager.Serialize(container, writer);
reader = new BinaryTokenStreamReader(writer.ToByteArray());
deserialized = SerializationManager.Deserialize<AzureQueueBatchContainerV2>(reader);
ValidateIdenticalQueueBatchContainerButNotSame(container, deserialized);
}