本文整理汇总了C#中Serializer.ObtainOpenStreamDeserializer方法的典型用法代码示例。如果您正苦于以下问题:C# Serializer.ObtainOpenStreamDeserializer方法的具体用法?C# Serializer.ObtainOpenStreamDeserializer怎么用?C# Serializer.ObtainOpenStreamDeserializer使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Serializer
的用法示例。
在下文中一共展示了Serializer.ObtainOpenStreamDeserializer方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ShouldDoBasicOpenStreamSerialization
public void ShouldDoBasicOpenStreamSerialization(
[Values(ReferencePreservation.DoNotPreserve, ReferencePreservation.Preserve, ReferencePreservation.UseWeakReference)]
ReferencePreservation referencePreservation)
{
var localSettings = settings.With(referencePreservation: referencePreservation);
var someObject = Tuple.Create(0, 1);
var otherObject = Tuple.Create(1, 2);
var serializer = new Serializer(localSettings);
var stream = new MemoryStream();
using(var osSerializer = serializer.ObtainOpenStreamSerializer(stream))
{
osSerializer.Serialize(someObject);
osSerializer.Serialize(someObject);
osSerializer.Serialize(otherObject);
}
stream.Seek(0, SeekOrigin.Begin);
using(var osDeserializer = serializer.ObtainOpenStreamDeserializer(stream))
{
var objs = new object[3];
for(var i = 0; i < objs.Length; i++)
{
objs[i] = osDeserializer.Deserialize<Tuple<int, int>>();
}
Assert.AreEqual(someObject, objs[0]);
Assert.AreEqual(someObject, objs[1]);
Assert.AreEqual(otherObject, objs[2]);
}
}
示例2: ShouldDeserializeMany
public void ShouldDeserializeMany()
{
var serializer = new Serializer(settings.With(useBuffering: false));
var stream = new MemoryStream();
var tuples = Enumerable.Range(1, 10).Select(x => Tuple.Create(x, x + 0.5)).ToList();
using(var osSerializer = serializer.ObtainOpenStreamSerializer(stream))
{
foreach(var tuple in tuples)
{
osSerializer.Serialize(tuple);
}
}
stream.Seek(0, SeekOrigin.Begin);
using(var osDeserializer = serializer.ObtainOpenStreamDeserializer(stream))
{
var deserializedTuples = osDeserializer.DeserializeMany<Tuple<int, double>>();
CollectionAssert.AreEqual(tuples.ToList(), deserializedTuples.ToList());
}
}
示例3: ShouldReadPreservationOptionFromTheStream
public void ShouldReadPreservationOptionFromTheStream(
[Values(ReferencePreservation.Preserve, ReferencePreservation.DoNotPreserve)]
ReferencePreservation referencePreservation)
{
var localSettings = settings.With(referencePreservation: referencePreservation);
var oppositeSettings = settings.With(referencePreservation:
referencePreservation == ReferencePreservation.Preserve
? ReferencePreservation.DoNotPreserve : ReferencePreservation.Preserve);
var someObject = Tuple.Create(0, 1);
var serializer = new Serializer(localSettings);
var stream = new MemoryStream();
using(var osSerializer = serializer.ObtainOpenStreamSerializer(stream))
{
osSerializer.Serialize(someObject);
osSerializer.Serialize(someObject);
}
stream.Seek(0, SeekOrigin.Begin);
serializer = new Serializer(oppositeSettings);
using(var osDeserializer = serializer.ObtainOpenStreamDeserializer(stream))
{
var first = osDeserializer.Deserialize<object>();
var second = osDeserializer.Deserialize<object>();
Assert.AreEqual(first, second);
Assert.AreEqual(first, someObject);
}
}
示例4: ShouldntPreserveReferences
public void ShouldntPreserveReferences()
{
var localSettings = settings.With(referencePreservation: ReferencePreservation.DoNotPreserve);
var someObject = new object();
var serializer = new Serializer(localSettings);
var stream = new MemoryStream();
using(var osSerializer = serializer.ObtainOpenStreamSerializer(stream))
{
osSerializer.Serialize(someObject);
osSerializer.Serialize(someObject);
}
stream.Seek(0, SeekOrigin.Begin);
using(var osDeserializer = serializer.ObtainOpenStreamDeserializer(stream))
{
Assert.AreNotSame(osDeserializer.Deserialize<object>(), osDeserializer.Deserialize<object>());
}
}