本文整理汇总了C#中ProtoBuf.Meta.TypeModel.Serialize方法的典型用法代码示例。如果您正苦于以下问题:C# TypeModel.Serialize方法的具体用法?C# TypeModel.Serialize怎么用?C# TypeModel.Serialize使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ProtoBuf.Meta.TypeModel
的用法示例。
在下文中一共展示了TypeModel.Serialize方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ExecuteTest
void ExecuteTest(TypeModel model, string test)
{
A a = new A { flags = new List<string> { "abc", "def" } }, c;
Assert.IsNotNull(a.flags.Count, test);
Assert.AreEqual(2, a.flags.Count, test);
Assert.AreEqual("abc", a.flags[0], test);
Assert.AreEqual("def", a.flags[1], test);
B b;
using (var ms = new MemoryStream())
{
model.Serialize(ms, a);
ms.Position = 0;
b = (B)model.Deserialize(ms, null, typeof(B));
}
Assert.IsNotNull(b.flags.Count, test);
Assert.AreEqual(2, b.flags.Count, test);
Assert.AreEqual("abc", b.flags[0], test);
Assert.AreEqual("def", b.flags[1], test);
using (var ms = new MemoryStream())
{
model.Serialize(ms, b);
ms.Position = 0;
c = (A)model.Deserialize(ms, null, typeof(A));
}
Assert.IsNotNull(c.flags.Count, test);
Assert.AreEqual(2, c.flags.Count, test);
Assert.AreEqual("abc", c.flags[0], test);
Assert.AreEqual("def", c.flags[1], test);
}
示例2: RunTestIssue103
private static void RunTestIssue103(int loop, TypeA typeA, TypeB typeB, TypeModel model, string caption)
{
// for JIT and preallocation
MemoryStream ms = new MemoryStream();
ms.SetLength(0);
model.Serialize(ms, typeA);
ms.Position = 0;
model.Deserialize(ms, null, typeof(TypeA));
Stopwatch typeASer = Stopwatch.StartNew();
for (int i = 0; i < loop; i++)
{
ms.SetLength(0);
model.Serialize(ms, typeA);
}
typeASer.Stop();
Stopwatch typeADeser = Stopwatch.StartNew();
for (int i = 0; i < loop; i++)
{
ms.Position = 0;
model.Deserialize(ms, null, typeof(TypeA));
}
typeADeser.Stop();
ms.SetLength(0);
model.Serialize(ms, typeB);
ms.Position = 0;
TypeB clone = (TypeB)model.Deserialize(ms, null, typeof(TypeB));
Assert.AreEqual(typeB.containedType.Count, clone.containedType.Count);
Stopwatch typeBSer = Stopwatch.StartNew();
for (int i = 0; i < loop; i++)
{
ms.SetLength(0);
model.Serialize(ms, typeB);
}
typeBSer.Stop();
Stopwatch typeBDeser = Stopwatch.StartNew();
for (int i = 0; i < loop; i++)
{
ms.Position = 0;
model.Deserialize(ms, null, typeof(TypeB));
}
typeBDeser.Stop();
Console.WriteLine(caption + " A/ser\t" + (typeASer.ElapsedMilliseconds * 1000 / loop) + " μs/item");
Console.WriteLine(caption + " A/deser\t" + (typeADeser.ElapsedMilliseconds * 1000 / loop) + " μs/item");
Console.WriteLine(caption + " B/ser\t" + (typeBSer.ElapsedMilliseconds * 1000 / loop) + " μs/item");
Console.WriteLine(caption + " B/deser\t" + (typeBDeser.ElapsedMilliseconds * 1000 / loop) + " μs/item");
}
示例3: Check
private void Check(TypeModel model, SerializationContext ctx, int magicNumber, string caption)
{
try
{
CanHazFactory orig = new CanHazFactory {Foo = 123, Bar = 456}, clone;
using(var ms = new MemoryStream())
{
model.Serialize(ms, orig, ctx);
ms.Position = 0;
clone = (CanHazFactory) model.Deserialize(ms, null, typeof(CanHazFactory), ctx);
}
Assert.AreNotSame(orig, clone);
Assert.AreEqual(123, orig.Foo, caption);
Assert.AreEqual(456, orig.Bar, caption);
Assert.AreEqual(0, orig.MagicNumber, caption);
Assert.AreEqual(123, clone.Foo, caption);
Assert.AreEqual(456, clone.Bar, caption);
Assert.AreEqual(magicNumber, clone.MagicNumber, caption);
} catch
{
Debug.WriteLine(caption);
throw;
}
}
示例4: CheckBytes
void CheckBytes(TypeModel model, object obj, string expected, string message)
{
using(var ms = new MemoryStream())
{
model.Serialize(ms, obj);
Assert.AreEqual(expected, Program.GetByteString(ms.ToArray()), message);
}
}
示例5: Execute
private static void Execute(TestUser obj, TypeModel model, string caption)
{
var ms = new MemoryStream();
model.Serialize(ms, obj);
Assert.Greater(2, 0, caption + ": I always get this wrong");
Assert.Greater(ms.Length, 0, caption + ": Nothing was serialized");
var clone = (TestUser) model.DeepClone(obj);
Assert.AreEqual(0, clone.uid, caption + ": uid wasn't zero");
Assert.IsTrue(clone.uidSpecified, caption + ": uid wasn't specified");
}
示例6: Execute
public void Execute(TypeModel model, string caption)
{
var obj = new Foo { Status = Status.All };
using(var ms = new MemoryStream())
{
model.Serialize(ms, obj);
ms.Position = 0;
Assert.AreEqual(3, ms.Length);
var clone = (Foo)model.Deserialize(ms, null, typeof(Foo));
Assert.AreEqual(Status.All, clone.Status);
}
}
示例7: Test
private static void Test(TypeModel m)
{
var list = new List<object> { new A { Id = "Abracadabra" }, new B { Id = "Focuspocus" }, new A { Id = "Abracadabra" }, };
using (var ms = new MemoryStream())
{
m.Serialize(ms, list);
ms.Position = 0;
var list2 = (List<object>)m.Deserialize(ms, null, typeof(List<object>));
Debug.Assert(list.SequenceEqual(list2));
File.WriteAllBytes(@"output.dump", ms.ToArray());
}
}
示例8: Execute
private void Execute(TypeModel model, string caption)
{
var large = new LargeType { Foo = 1, Bar = "abc" };
SmallType small;
using(var ms = new MemoryStream())
{
model.Serialize(ms, large);
ms.Position = 0;
small = (SmallType) model.Deserialize(ms, null, typeof(SmallType));
}
Assert.IsNotNull(small, caption);
}
示例9: TestGeneratedModel
private static void TestGeneratedModel(TypeModel model, string message)
{
var a = new A_generated() { Age = 10, b = new B_generated { Balls = 23 } };
using (var ms = new MemoryStream())
{
model.Serialize(ms, a);
Assert.IsTrue(ms.ToArray().SequenceEqual(new byte[] { 08, 10, 82, 2, 16, 23 }), message);
ms.Position = 0;
var clone = (A_generated)model.Deserialize(ms, null, typeof(A_generated));
Assert.AreEqual(10, clone.Age, message);
Assert.AreEqual(23, clone.b.Balls, message);
}
}
示例10: Test
static void Test(TypeModel model, C c, string caption)
{
Assert.AreEqual(43, c.Unknown.N, "braindead");
using (var ms = new MemoryStream())
{
model.Serialize(ms, c);
Assert.Greater(1, 0, "args fail");
Assert.Greater(ms.Length, 0, "Nothing written");
ms.Position = 0;
var c2 = (C)model.Deserialize(ms, null, typeof(C));
Assert.AreEqual(c.Unknown.N, c2.Unknown.N, caption);
}
}
示例11: Execute_Vanilla
void Execute_Vanilla(TypeModel model, string caption)
{
const Test original = Test.test2;
using (MemoryStream ms = new MemoryStream())
{
model.Serialize(ms, original);
ms.Position = 0;
Assert.AreEqual("08-01", BitConverter.ToString(ms.GetBuffer(), 0, (int)ms.Length));
Test obj;
obj = (Test)model.Deserialize(ms, null, typeof(Test));
Assert.AreEqual(original, obj);
}
}
示例12: Execute
public void Execute(TypeModel model, string caption)
{
try
{
var ms = new MemoryStream();
model.Serialize(ms, new EncapsulatedOuter { X = 123, Inner = new EncapsulatedInner { Y = 456 } });
ms.Position = 0;
var obj = (InheritedChild)model.Deserialize(ms, null, typeof(InheritedBase));
Assert.AreEqual(123, obj.X, caption);
Assert.AreEqual(456, obj.Y, caption);
}
catch (Exception ex)
{
Assert.Fail(caption + ":" + ex.Message);
}
}
示例13: Execute
private static void Execute(int count, TypeModel model, string caption)
{
const int InnerLoop = 1000;
object lockObj = new object();
var average = 0d;
var min = double.MaxValue;
var max = double.MinValue;
int complete = 0;
model.DeepClone(Create()); // warm-up
Parallel.For(0, count, i =>
{
var classThree = Create();
var counter = Stopwatch.StartNew();
using (var ms = new MemoryStream())
{
for (int j = 0; j < InnerLoop; j++)
{
ms.SetLength(0);
model.Serialize(ms, classThree);
ms.Position = 0;
var des = model.Deserialize(ms, null, typeof(ClassThree));
var aaa = des;
}
counter.Stop();
}
var elapsed = counter.Elapsed.TotalMilliseconds;
double currentAverage;
lock (lockObj)
{
complete++;
average += elapsed;
var oldMin = min;
min = Math.Min(min, elapsed);
max = Math.Max(max, elapsed);
currentAverage = average / complete;
if (min != oldMin || (complete % 500) == 0)
{
Trace.WriteLine(string.Format("{5}\tCycle {0}: {1:N2} ms - avg: {2:N2} ms - min: {3:N2} - max: {4:N2}", i, elapsed, currentAverage, min, max, caption));
}
}
});
Trace.WriteLine(string.Format("{5}\tComplete {0}: avg: {2:N2} ms - min: {3:N2} - max: {4:N2}", complete, 0, average / complete, min, max, caption));
}
示例14: Test
static void Test(TypeModel with, TypeModel without, string message)
{
var obj = new DodgyDefault { Value = false };
DodgyDefault c1 = (DodgyDefault)with.DeepClone(obj);
Assert.IsTrue(c1.Value, message);
DodgyDefault c2 = (DodgyDefault)without.DeepClone(obj);
Assert.IsFalse(c2.Value, message);
using (var ms = new MemoryStream())
{
with.Serialize(ms, obj);
Assert.AreEqual(0, ms.Length, message);
}
using (var ms = new MemoryStream())
{
without.Serialize(ms, obj);
Assert.AreEqual(2, ms.Length, message);
}
}
示例15: WriteCustomer
private static void WriteCustomer(TypeModel model, string caption, object obj)
{
WriteHeading(caption);
byte[] blob;
using (MemoryStream ms = new MemoryStream())
{
model.Serialize(ms, obj);
blob = ms.ToArray();
}
foreach (byte b in blob)
{
Console.Write(b.ToString("x2"));
}
Console.WriteLine();
using (MemoryStream ms = new MemoryStream(blob))
{
object clone = model.Deserialize(ms, null, obj.GetType());
string oldS = Convert.ToString(obj), newS = Convert.ToString(clone);
Console.WriteLine(oldS == newS ? ("match: " + newS) : ("delta" + oldS + " vs " + newS));
}
Console.WriteLine();
}