public void ToClasses() { var Object1 = new TestA() { A = 10, B = "ASDF" }; var Object2 = new TestB() { A = 20, B = "ZXCV" }; TestB Result1 = Utilities.DataTypes.Conversion.Manager.To<TestA, TestB>(Object1, null); Assert.Equal(10, Result1.A); Assert.Equal("ASDF", Result1.B); TestA Result2 = Utilities.DataTypes.Conversion.Manager.To<TestB, TestA>(Object2, null); Assert.Equal(20, Result2.A); Assert.Equal("ZXCV", Result2.B); }
public void Add_TwoDifferentObjectTypesWithSameId_Ok() { // arrange var objectToSave1 = new TestA(); var objectToSave2 = new TestB(); Guid id = Guid.NewGuid(); this.database.Add(id, objectToSave1); // act / assert Assert.DoesNotThrow(() => this.database.Add(id, objectToSave2)); }
public void TestFromBtoA() { var mapper = new ExpressionMapper<TestA, TestB>() .Map(x => x.AValue1, x => x.BValue1) .Map(x => x.AValue2, x => x.BValue2); var testB = new TestB() { BValue1 = "TestValue", BValue2 = 42 }; var testA = mapper.MapBtoA(testB); Assert.AreEqual("TestValue", testA.AValue1); Assert.AreEqual(42, testA.AValue2); }
/// <summary>Read the VarInt length prefix and the given number of bytes from the stream and deserialze it into the instance.</summary> public static TestB.ExternalStruct DeserializeLengthDelimited(Stream stream, ref TestB.ExternalStruct instance) { BinaryReader br = new BinaryReader(stream); long limit = global::SilentOrbit.ProtocolBuffers.ProtocolParser.ReadUInt32(stream); limit += stream.Position; while (true) { if (stream.Position >= limit) { if (stream.Position == limit) break; else throw new InvalidOperationException("Read past max limit"); } int keyByte = stream.ReadByte(); if (keyByte == -1) throw new System.IO.EndOfStreamException(); // Optimized reading of known fields with field ID < 16 switch (keyByte) { // Field 1 Fixed64 case 9: instance.X = br.ReadDouble(); continue; } var key = global::SilentOrbit.ProtocolBuffers.ProtocolParser.ReadKey((byte)keyByte, stream); // Reading field ID > 16 and unknown field ID/wire type combinations switch (key.Field) { case 0: throw new InvalidDataException("Invalid field id: 0, something went wrong in the stream"); default: global::SilentOrbit.ProtocolBuffers.ProtocolParser.SkipKey(stream, key); break; } } return instance; }
/// <summary>Takes the remaining content of the stream and deserialze it into the instance.</summary> public static TestB.ExternalStruct Deserialize(Stream stream, ref TestB.ExternalStruct instance) { BinaryReader br = new BinaryReader(stream); while (true) { int keyByte = stream.ReadByte(); if (keyByte == -1) break; // Optimized reading of known fields with field ID < 16 switch (keyByte) { // Field 1 Fixed64 case 9: instance.X = br.ReadDouble(); continue; } var key = global::SilentOrbit.ProtocolBuffers.ProtocolParser.ReadKey((byte)keyByte, stream); // Reading field ID > 16 and unknown field ID/wire type combinations switch (key.Field) { case 0: throw new InvalidDataException("Invalid field id: 0, something went wrong in the stream"); default: global::SilentOrbit.ProtocolBuffers.ProtocolParser.SkipKey(stream, key); break; } } return instance; }
/// <summary>Helper: put the buffer into a MemoryStream before deserializing</summary> public static TestB.ExternalStruct Deserialize(byte[] buffer, ref TestB.ExternalStruct instance) { using (var ms = new MemoryStream(buffer)) Deserialize(ms, ref instance); return instance; }
/// <summary>Read the given number of bytes from the stream and deserialze it into the instance.</summary> public static TestB.ExternalClass DeserializeLength(Stream stream, int length, TestB.ExternalClass instance) { long limit = stream.Position + length; while (true) { if (stream.Position >= limit) { if (stream.Position == limit) break; else throw new InvalidOperationException("Read past max limit"); } int keyByte = stream.ReadByte(); if (keyByte == -1) throw new System.IO.EndOfStreamException(); // Optimized reading of known fields with field ID < 16 switch (keyByte) { // Field 1 Varint case 8: instance.A = (int)global::SilentOrbit.ProtocolBuffers.ProtocolParser.ReadUInt64(stream); continue; } var key = global::SilentOrbit.ProtocolBuffers.ProtocolParser.ReadKey((byte)keyByte, stream); // Reading field ID > 16 and unknown field ID/wire type combinations switch (key.Field) { case 0: throw new InvalidDataException("Invalid field id: 0, something went wrong in the stream"); default: global::SilentOrbit.ProtocolBuffers.ProtocolParser.SkipKey(stream, key); break; } } return instance; }
/// <summary>Takes the remaining content of the stream and deserialze it into the instance.</summary> public static TestB.ExternalClass Deserialize(Stream stream, TestB.ExternalClass instance) { while (true) { int keyByte = stream.ReadByte(); if (keyByte == -1) break; // Optimized reading of known fields with field ID < 16 switch (keyByte) { // Field 1 Varint case 8: instance.A = (int)global::SilentOrbit.ProtocolBuffers.ProtocolParser.ReadUInt64(stream); continue; } var key = global::SilentOrbit.ProtocolBuffers.ProtocolParser.ReadKey((byte)keyByte, stream); // Reading field ID > 16 and unknown field ID/wire type combinations switch (key.Field) { case 0: throw new InvalidDataException("Invalid field id: 0, something went wrong in the stream"); default: global::SilentOrbit.ProtocolBuffers.ProtocolParser.SkipKey(stream, key); break; } } return instance; }
/// <summary>Helper: put the buffer into a MemoryStream before deserializing</summary> public static TestB.ExternalClass Deserialize(byte[] buffer, TestB.ExternalClass instance) { using (var ms = new MemoryStream(buffer)) Deserialize(ms, instance); return instance; }
public TestA() { _testB = new TestB(); }
public static void Test(TestB textA) { TestAp(textA); }
static void Main(string[] args) { int?b = null; //定义可空类型 int a = b ?? 0; //合并空运算符 Console.WriteLine("a:{0},b:{1}", a, b); //类的比较 TestA testA = new TestA(); testA.a = 1; testA.b = "s"; TestA testB = new TestA(); testB.a = 1; testB.b = "s"; if (ReferenceEquals(testA, testB)) //判断两个类是否引用同一个对象,即是否指向同一个地址 { Console.WriteLine("两个对象相同!"); } else { Console.WriteLine("两个对象不同!"); } if (Equals(testA, testB)) { Console.WriteLine("equals,两个对象相同!"); } else { Console.WriteLine("equals,两个对象不同!"); } Console.WriteLine("testA+testB={0}", (testA + testB).a); //结构的比较 TestB testC = new TestB(); testC.a = 1; testC.b = "s"; TestB testD = new TestB(); testD.a = 1; testD.b = "s"; if (ReferenceEquals(testC, testD)) //判断两个类是否引用同一个对象,即是否指向同一个地址 { Console.WriteLine("两个对象相同!"); } else { Console.WriteLine("两个对象不同!"); } if (Equals(testC, testD)) { Console.WriteLine("equals,两个对象相同!"); } else { Console.WriteLine("equals,两个对象不同!"); } //unsafe //{ // Console.WriteLine(sizeof(TestA));//sizeof适用于不安全模式 //} //byte a = 255; //checked //执行溢出检查,当溢出时,会抛出异常 //{ // a++; //} //unchecked { //禁止溢出检查 // a++; //} Console.ReadKey(); }