本文整理汇总了C#中BinarySerializer.ToBytes方法的典型用法代码示例。如果您正苦于以下问题:C# BinarySerializer.ToBytes方法的具体用法?C# BinarySerializer.ToBytes怎么用?C# BinarySerializer.ToBytes使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BinarySerializer
的用法示例。
在下文中一共展示了BinarySerializer.ToBytes方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestAfterDeserialization
public void TestAfterDeserialization()
{
var bs = new BinarySerializer();
var v0 = new SampleAfter { X = "m" };
var result0 = bs.ToBytes(v0);
Assert.AreEqual(
"20 01 00 " + XS(typeof(SampleAfter)) + " 01 00 " + XS("X", RoughType.String) +
" 01 00 " + XS("m") + " 00 00",
XS(result0));
var result1 = bs.ToBytes(new SampleAfter2 { X = "m" });
CheckDeserializers(bd => {
Assert.AreEqual("m1", bd.FromBytes<SampleAfter>(result0).X);
Assert.AreEqual("m231", bd.FromBytes<SampleAfter2>(result1).X);
});
}
示例2: TestJsonToBinarySimple
public void TestJsonToBinarySimple()
{
var bs = new BinarySerializer();
var bd = new BinaryDeserializer();
var js = new JsonSerializer();
js.JsonOptions.FieldSeparator = "";
js.JsonOptions.Indent = "";
js.JsonOptions.SaveRootClass = true;
var jd = new JsonDeserializer();
var data1 = "{\"class\":\"YuzuTest.Sample1, YuzuTest\",\"X\":98,\"Y\":\"\"}";
var result1 = js.ToString(bd.FromBytes(bs.ToBytes(jd.FromString(data1))));
Assert.AreEqual(data1, result1);
}
示例3: TestAllowReadingFromAncestor
public void TestAllowReadingFromAncestor()
{
var bs = new BinarySerializer();
var v1 = new Sample2 { X = 83, Y = "83" };
var result1 = bs.ToBytes(v1);
Assert.AreEqual(
"20 01 00 " + XS(typeof(Sample2)) + " 02 00 " + XS("X", RoughType.Int, "Y", RoughType.String) +
" 01 00 53 00 00 00 00 00", XS(result1));
var w1 = new Sample2Allow();
var bd = new BinaryDeserializer();
bd.FromBytes(w1, result1);
Assert.AreEqual(v1.X, w1.X);
}
示例4: TestAbstract
public void TestAbstract()
{
var bs = new BinarySerializer();
var bd = new BinaryDeserializer();
SampleAbstract v1 = new SampleConcrete { XX = 81 };
var result1 = bs.ToBytes(v1);
Assert.AreEqual(
"20 01 00 " + XS(typeof(SampleConcrete)) + " 01 00 " + XS("XX", RoughType.Int) +
" 01 00 51 00 00 00 00 00", XS(result1));
var w1 = bd.FromBytes<SampleAbstract>(result1);
Assert.AreEqual((v1 as SampleConcrete).XX, (w1 as SampleConcrete).XX);
var w1g = (SampleConcrete)(new BinaryDeserializerGen().FromBytes(result1));
Assert.AreEqual((v1 as SampleConcrete).XX, w1g.XX);
var v2 = new List<SampleAbstract>();
v2.Add(new SampleConcrete { XX = 51 });
var w2 = bd.FromBytes<List<SampleAbstract>>(bs.ToBytes(v2));
Assert.AreEqual(v2.Count, w2.Count);
Assert.AreEqual((v2[0] as SampleConcrete).XX, (w2[0] as SampleConcrete).XX);
}
示例5: TestBinaryToJsonUnknown
public void TestBinaryToJsonUnknown()
{
var bs = new BinarySerializer();
var bd = new BinaryDeserializer();
var js = new JsonSerializer();
js.JsonOptions.SaveRootClass = true;
var jd = new JsonDeserializer();
var n = "NewType".ToCharArray().Select(ch => (byte)ch).ToList();
var data1 =
new byte[] { 0x20, 0x01, 0x00, (byte)n.Count }.Concat(n).
Concat(new byte[] { 0x01, 0x00, 0x01, (byte)'X', (byte)RoughType.String, 0x01, 0x00,
0x03, (byte)'a', (byte)'b', (byte)'c', 0, 0
}).ToArray();
var result1 = bs.ToBytes(jd.FromString(js.ToString(bd.FromBytes(data1))));
CollectionAssert.AreEqual(data1, result1);
}
示例6: TestInterface
public void TestInterface()
{
var bs = new BinarySerializer();
var v1 = new SampleInterfaceField { I = new SampleInterfaced { X = 34 } };
var result1 = bs.ToBytes(v1);
Assert.AreEqual(
"20 01 00 " + XS(typeof(SampleInterfaceField)) + " 01 00 " + XS("I", RoughType.Record) +
" 01 00 02 00 " + XS(typeof(SampleInterfaced)) + " 01 00 " + XS("X", RoughType.Int) +
" 01 00 22 00 00 00 00 00 00 00",
XS(result1));
var w1 = new SampleInterfaceField();
var bd = new BinaryDeserializer();
bd.FromBytes(w1, result1);
Assert.IsInstanceOfType(w1.I, typeof(SampleInterfaced));
Assert.AreEqual(34, w1.I.X);
var w1g = new SampleInterfaceField();
var bdg = new BinaryDeserializerGen();
bdg.FromBytes(w1g, result1);
Assert.IsInstanceOfType(w1g.I, typeof(SampleInterfaced));
Assert.AreEqual(34, w1g.I.X);
var w1n = (SampleInterfaceField)bd.FromBytes(new byte[] { 0x20, 01, 00, 01, 00, 00, 00, 00, 00 });
Assert.AreEqual(null, w1n.I);
var w1ng = (SampleInterfaceField)bdg.FromBytes(new byte[] { 0x20, 01, 00, 01, 00, 00, 00, 00, 00 });
Assert.AreEqual(null, w1ng.I);
var v2 = new List<ISample> { null, new SampleInterfaced { X = 37 } };
var result2 = bs.ToBytes(v2);
Assert.AreEqual("21 20 02 00 00 00 00 00 02 00 01 00 25 00 00 00 00 00", XS(result2));
var w2 = new List<ISample>();
bd.FromBytes(w2, result2);
Assert.AreEqual(2, w2.Count);
Assert.IsNull(w2[0]);
Assert.AreEqual(37, w2[1].X);
ISampleField v3 = new SampleInterfacedField { X = 41 };
var result3 = bs.ToBytes(v3);
Assert.AreEqual(
"20 03 00 " + XS(typeof(SampleInterfacedField)) + " 01 00 " + XS("X", RoughType.Int) +
" 01 00 29 00 00 00 00 00", XS(result3));
var w3 = (ISampleField)bd.FromBytes(result3);
Assert.AreEqual(41, w3.X);
}
示例7: TestBool
public void TestBool()
{
var bs = new BinarySerializer();
var v = new SampleBool { B = true };
var result1 = bs.ToBytes(v);
Assert.AreEqual(
"20 01 00 " + XS(typeof(SampleBool)) +
" 01 00 " + XS("B", RoughType.Bool) + " 01 00 01 00 00",
XS(result1));
var bd = new BinaryDeserializer();
var w = new SampleBool();
bd.FromBytes(w, result1);
Assert.AreEqual(true, w.B);
}
示例8: TestClassNames
public void TestClassNames()
{
var bs = new BinarySerializer();
bs.Options.TagMode = TagMode.Names;
Assert.AreEqual(
"20 01 00 " + XS(typeof(SampleBase)) + " 01 00 " + XS("FBase", RoughType.Int) +
" 01 00 00 00 00 00 00 00",
XS(bs.ToBytes(new SampleBase())));
Assert.AreEqual(
"20 02 00 " + XS(typeof(SampleDerivedA)) + " 02 00 " +
XS("FBase", RoughType.Int, "FA", RoughType.Int) +
" 01 00 00 00 00 00 02 00 00 00 00 00 00 00",
XS(bs.ToBytes(new SampleDerivedA())));
var bd = new BinaryDeserializer();
bd.Options.TagMode = TagMode.Names;
var v = bd.FromBytes(SX(
"20 01 00 " + XS(typeof(SampleDerivedB)) + " 02 00 " +
XS("FBase", RoughType.Int, "FB", RoughType.Int) +
" 01 00 03 00 00 00 02 00 07 00 00 00 00 00"));
Assert.IsInstanceOfType(v, typeof(SampleDerivedB));
var b = (SampleDerivedB)v;
Assert.AreEqual(3, b.FBase);
Assert.AreEqual(7, b.FB);
}
示例9: TestGeneric
public void TestGeneric()
{
var bs = new BinarySerializer();
var v1 = new SampleInterfaceField { I = new SampleInterfacedGeneric<string> { X = 35, G = "qq" } };
var result1 = bs.ToBytes(v1);
Assert.AreEqual(
"20 01 00 " + XS(typeof(SampleInterfaceField)) + " 01 00 " + XS("I", RoughType.Record) +
" 01 00 02 00 " + XS("YuzuTest.SampleInterfacedGeneric`1[[System.String]], YuzuTest") +
" 02 00 " + XS("G", RoughType.String, "X", RoughType.Int) +
" 01 00 " + XS("qq") + " 02 00 23 00 00 00 00 00 00 00",
XS(result1));
CheckDeserializers(bd => {
var w1 = (SampleInterfaceField)bd.FromBytes(new SampleInterfaceField(), result1);
Assert.AreEqual(w1.I.X, 35);
Assert.AreEqual((w1.I as SampleInterfacedGeneric<string>).G, "qq");
});
}
示例10: TestArray
public void TestArray()
{
var bs = new BinarySerializer();
var bd = new BinaryDeserializer();
var bdg = new BinaryDeserializerGen();
var v0 = new SampleArray { A = new string[] { "a", "b", "c" } };
var result0 = bs.ToBytes(v0);
Assert.AreEqual(
"20 01 00 " + XS(typeof(SampleArray)) + " 01 00 " + XS("A", RoughType.Sequence) +
" 10 01 00 03 00 00 00 " + XS("a", "b", "c") + " 00 00",
XS(result0));
var w0 = new SampleArray();
bd.FromBytes(w0, result0);
CollectionAssert.AreEqual(v0.A, w0.A);
var w0g = (SampleArray)bdg.FromBytes(result0);
CollectionAssert.AreEqual(v0.A, w0g.A);
var v2 = new SampleArray();
var result2 = bs.ToBytes(v2);
Assert.AreEqual("20 01 00 01 00 FF FF FF FF 00 00", XS(result2));
var w2 = new SampleArray();
bd.FromBytes(w2, result2);
CollectionAssert.AreEqual(v2.A, w2.A);
var w2g = (SampleArray)bdg.FromBytes(result2);
CollectionAssert.AreEqual(v2.A, w2g.A);
}
示例11: TestMerge
public void TestMerge()
{
var bs = new BinarySerializer();
var v1 = new SampleMerge();
v1.DI.Add(3, 4);
v1.LI.Add(33);
v1.M = new Sample1 { X = 768, Y = "ttt" };
var result1 = bs.ToBytes(v1);
Assert.AreEqual(
"20 01 00 " + XS(typeof(SampleMerge)) + " 03 00 " +
XS("DI") + " 22 05 05 " +
XS("LI") + " 21 05 " + XS("M", RoughType.Record) +
" 01 00 01 00 00 00 03 00 00 00 04 00 00 00 02 00 01 00 00 00 21 00 00 00 03 00 02 00 " +
XS(typeof(Sample1)) + " 02 00 " + XS("X", RoughType.Int, "Y", RoughType.String) +
" 01 00 00 03 00 00 00 00 00 00",
XS(result1));
CheckDeserializers(bd => {
var w1 = new SampleMerge();
w1.DI.Add(5, 6);
w1.LI.Add(44);
w1.M = new Sample1 { X = 999, Y = "qqq" };
bd.FromBytes(w1, result1);
CollectionAssert.AreEqual(new Dictionary<int, int> { { 5, 6 }, { 3, 4 } }, w1.DI);
CollectionAssert.AreEqual(new[] { 44, 33 }, w1.LI);
Assert.AreEqual(768, w1.M.X);
Assert.AreEqual("qqq", w1.M.Y);
});
}
示例12: TestDictionaryKeys
public void TestDictionaryKeys()
{
var bs = new BinarySerializer();
var v0 = new SampleDictKeys {
I = new Dictionary<int, int> { { 5, 7 } },
E = new Dictionary<SampleEnum, int> { { SampleEnum.E2, 8 } },
K = new Dictionary<SampleKey, int> { { new SampleKey { V = 3 }, 9 } },
};
var result0 = bs.ToBytes(v0);
Assert.AreEqual(
"20 01 00 " + XS(typeof(SampleDictKeys)) + " 03 00 " +
XS("E", RoughType.Mapping) + " 05 05 " +
XS("I", RoughType.Mapping) + " 05 05 " +
XS("K", RoughType.Mapping) + " 20 05" +
" 01 00 01 00 00 00 01 00 00 00 08 00 00 00 " +
"02 00 01 00 00 00 05 00 00 00 07 00 00 00 " +
"03 00 01 00 00 00 02 00 " + XS(typeof(SampleKey)) +
" 01 00 " + XS("V", RoughType.Int) +
" 01 00 03 00 00 00 00 00 09 00 00 00 00 00", XS(result0));
CheckDeserializers(bd => {
var w = new SampleDictKeys();
bd.FromBytes(w, result0);
Assert.AreEqual(1, w.I.Count);
Assert.AreEqual(7, w.I[5]);
Assert.AreEqual(1, w.E.Count);
Assert.AreEqual(8, w.E[SampleEnum.E2]);
Assert.AreEqual(1, w.K.Count);
Assert.AreEqual(9, w.K[new SampleKey { V = 3 }]);
});
}
示例13: TestMatrix
public void TestMatrix()
{
var src =
"20 01 00 " + XS(typeof(SampleMatrix)) + " 01 00 " + XS("M") + " 21 21 05 01 00 " +
"04 00 00 00 03 00 00 00 01 00 00 00 02 00 00 00 03 00 00 00 " +
"02 00 00 00 04 00 00 00 05 00 00 00 " +
"01 00 00 00 06 00 00 00 00 00 00 00 00 00";
var w = new SampleMatrix();
CheckDeserializers(bd => {
bd.FromBytes(w, SX(src));
Assert.AreEqual(4, w.M.Count);
CollectionAssert.AreEqual(new int[] { 1, 2, 3 }, w.M[0]);
CollectionAssert.AreEqual(new int[] { 4, 5 }, w.M[1]);
CollectionAssert.AreEqual(new int[] { 6 }, w.M[2]);
Assert.AreEqual(0, w.M[3].Count);
});
var bs = new BinarySerializer();
Assert.AreEqual(src, XS(bs.ToBytes(w)));
}
示例14: TestDelegate
public void TestDelegate()
{
var bs = new BinarySerializer();
var v1 = new SampleSelfDelegate { x = 77 };
v1.OnSomething = v1.Handler1;
var result = bs.ToBytes(v1);
Assert.AreEqual(
"20 01 00 " + XS(typeof(SampleSelfDelegate)) + " 02 00 " +
XS("OnSomething", RoughType.Record, "x", RoughType.Int) +
" 01 00 " + XS("Handler1") + " 02 00 4D 00 00 00 00 00",
XS(result));
var w1 = new SampleSelfDelegate();
var bd = new BinaryDeserializer();
bd.FromBytes(w1, result);
Assert.AreEqual(v1.x, w1.x);
w1.OnSomething(10);
Assert.AreEqual(87, w1.x);
result[69]++; // Replace("Handler1", "Handler2")
(new BinaryDeserializer()).FromBytes(w1, result);
w1.OnSomething(10);
Assert.AreEqual(770, w1.x);
}
示例15: TestDictionary
public void TestDictionary()
{
var bs = new BinarySerializer();
bs.Options.TagMode = TagMode.Names;
var bd = new BinaryDeserializer();
bd.Options.TagMode = TagMode.Names;
var bdg = new BinaryDeserializer();
bdg.Options.TagMode = TagMode.Names;
var v0 = new SampleDict {
Value = 3, Children = new Dictionary<string, SampleDict> {
{ "a", new SampleDict { Value = 5, Children = new Dictionary<string, SampleDict>() } },
{ "b", new SampleDict { Value = 7 } },
}
};
var result0 = bs.ToBytes(v0);
Assert.AreEqual(
"20 01 00 " + XS(typeof(SampleDict)) + " 02 00 " +
XS("Value", RoughType.Int, "Children", RoughType.Mapping) + " 10 20" +
" 01 00 03 00 00 00 02 00 02 00 00 00 " + XS("a") +
" 01 00 01 00 05 00 00 00 02 00 00 00 00 00 00 00 " +
XS("b") + " 01 00 01 00 07 00 00 00 02 00 FF FF FF FF 00 00 00 00",
XS(result0));
var w0 = new SampleDict();
bd.FromBytes(w0, result0);
Assert.AreEqual(v0.Value, w0.Value);
Assert.AreEqual(v0.Children.Count, w0.Children.Count);
Assert.AreEqual(v0.Children["a"].Value, w0.Children["a"].Value);
var w1 = (SampleDict)bdg.FromBytes(result0);
Assert.AreEqual(v0.Value, w1.Value);
Assert.AreEqual(v0.Children.Count, w1.Children.Count);
Assert.AreEqual(v0.Children["a"].Value, w1.Children["a"].Value);
}