public void SetUp() { var data = new byte[] { 0x00, // object: FirstObject 0x46, 0x69, 0x72, 0x73, 0x74, 0x4f, 0x62, 0x6a, 0x65, 0x63, 0x74, 0x00, 0x01, // string: firstkey = firstvalue 0x66, 0x69, 0x72, 0x73, 0x74, 0x6b, 0x65, 0x79, 0x00, 0x66, 0x69, 0x72, 0x73, 0x74, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x00, 0x08, // end object 0x08, // end document 0x00, // object: SecondObject 0x53, 0x65, 0x63, 0x6f, 0x6e, 0x64, 0x4f, 0x62, 0x6a, 0x65, 0x63, 0x74, 0x00, 0x01, // string: secondkey = secondvalue 0x73, 0x65, 0x63, 0x6f, 0x6e, 0x64, 0x6b, 0x65, 0x79, 0x00, 0x73, 0x65, 0x63, 0x6f, 0x6e, 0x64, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x00, 0x08, // end object 0x08, // end document }; using var stream = new MemoryStream(); stream.Write(data, 0, data.Length); stream.Seek(0, SeekOrigin.Begin); _firstObject = KVSerializer.Create(KVSerializationFormat.KeyValues1Binary).Deserialize <FirstObject>(stream); Assert.That(stream.Position, Is.EqualTo(36)); // ensure we read exactly 36 bytes _secondObject = KVSerializer.Create(KVSerializationFormat.KeyValues1Binary).Deserialize <SecondObject>(stream); Assert.That(stream.Position, Is.EqualTo(75)); // ensure we read exactly 39 bytes }
public void Test() { AutoMapper.Mapper.CreateMap <FirstObject, SecondObject>(); FirstObject t = new FirstObject(); t.Items = new object[] { new Item() { Id = 1 }, new Item() { Id = 2 } }; SecondObject result = AutoMapper.Mapper.Map <SecondObject>(t); }
static void Main(string[] args) { DateTime startTime = DateTime.Now; List <FirstObject> firstObjects = new List <FirstObject>(); List <MapFirstObject> mapFirstObjects = new List <MapFirstObject>(); // REDUCED AUTO MAPPER TEST ------------------------------------------------------------ ////ReducedAutoMapper.Instance.CreateMap<FirstObject, MapFirstObject>(); ////ReducedAutoMapper.Instance.CreateMap<SecondObject, MapSecondObject>(); ////ReducedAutoMapper.Instance.CreateMap<ThirdObject, MapThirdObject>(); ////for (int i = 0; i < 100000; i++) ////{ //// FirstObject firstObject = //// new FirstObject(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), (decimal)12.2, DateTime.Now, //// new SecondObject(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), (decimal)11.2)); //// firstObjects.Add(firstObject); //// System.Console.WriteLine("Object created: {0}", i); ////} ////for (int i = 0; i < firstObjects.Count - 1; i++) ////{ //// MapFirstObject mapSecObj = ReducedAutoMapper.Instance.Map<FirstObject, MapFirstObject>(firstObjects[i]); //// mapFirstObjects.Add(mapSecObj); //// System.Console.WriteLine("Map Object: {0}", i); ////} // AUTO MAPPER TEST ------------------------------------------------------------ AutoMapper.Mapper.CreateMap <FirstObject, MapFirstObject>(); AutoMapper.Mapper.CreateMap <SecondObject, MapSecondObject>(); AutoMapper.Mapper.CreateMap <ThirdObject, MapThirdObject>(); for (int i = 0; i < 100000; i++) { FirstObject firstObject = new FirstObject(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), (decimal)12.2, DateTime.Now, new SecondObject(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), (decimal)11.2)); firstObjects.Add(firstObject); System.Console.WriteLine("Object created: {0}", i); } for (int i = 0; i < firstObjects.Count; i++) { MapFirstObject mapSecObj = AutoMapper.Mapper.Map <FirstObject, MapFirstObject>(firstObjects[i]); mapFirstObjects.Add(mapSecObj); System.Console.WriteLine("Map Object: {0}", i); } DateTime endTime = DateTime.Now; System.Console.WriteLine("Finish for {0}", endTime - startTime); System.Console.WriteLine(); }
static void Main(string[] args) { JObject o = JObject.Parse(json); List <FirstObject> l = new List <FirstObject>(); int c = 0; while (o[$"{c}"] != null) { FirstObject fo = o[$"{c++}"].ToObject <FirstObject>(); l.Add(fo); } SecondObject so = JsonConvert.DeserializeObject <SecondObject>(json); so.FirstObjects = l.ToArray(); Console.ReadKey(); }
static void MapObjectsAutoMapper() { List <FirstObject> firstObjects = new List <FirstObject>(); List <MapFirstObject> mapFirstObjects = new List <MapFirstObject>(); AutoMapper.Mapper.CreateMap <FirstObject, MapFirstObject>(); AutoMapper.Mapper.CreateMap <SecondObject, MapSecondObject>(); AutoMapper.Mapper.CreateMap <ThirdObject, MapThirdObject>(); for (int i = 0; i < 10000; i++) { FirstObject firstObject = new FirstObject(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), (decimal)12.2, DateTime.Now, new SecondObject(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), (decimal)11.2)); firstObjects.Add(firstObject); } foreach (var currentObject in firstObjects) { MapFirstObject mapSecObj = AutoMapper.Mapper.Map <FirstObject, MapFirstObject>(currentObject); mapFirstObjects.Add(mapSecObj); } }
public override bool AreEqual() { var objectType = FirstObject.GetType(); var equalsMethod = GetEqualsMethod(objectType); if (equalsMethod != null) { return((bool)equalsMethod.Invoke(FirstObject, new object[] { SecondObject })); } var objectProperties = objectType.GetProperties(); var enumerableType = typeof(IEnumerable <object>); foreach (var property in objectProperties) { var propertyType = property.PropertyType; var firstValue = property.GetValue(FirstObject); var secondValue = property.GetValue(SecondObject); if (firstValue == null || secondValue == null) { States.Add(new NullObjectState { FirstObject = firstValue, SecondObject = secondValue }); continue; } if (IsPreviousReferenceProperty(propertyType)) { States.Add(new PreviousObjectState { FirstObject = firstValue, SecondObject = secondValue }); continue; } if (propertyType.IsValueType) { States.Add(new ValueTypeState { FirstObject = firstValue, SecondObject = secondValue }); continue; } if (enumerableType.IsAssignableFrom(propertyType)) { States.Add(new CollectionState { FirstObject = firstValue, SecondObject = secondValue }); continue; } if (propertyType.IsClass) { PreviousReferences.Add(objectType); States.Add(new RefTypeState(PreviousReferences) { FirstObject = firstValue, SecondObject = secondValue }); continue; } } return(States.All(x => x.AreEqual())); }
static void Main(string[] args) { IMyMapper mapper = new MyMapper(); var obj = new FirstObject() { Bool = true, Decimal = 0.5, Float = 0.7f, Double = 4.3, Int = 4, String = "String", NestedType = new NestedType() { String = "String", Int = 5 }, Struct2 = new Struct() { String = "String", Int = 5 }, StringIntoInt = "5", DoubleIntoDecimal = 5.2, DecimalIntoInt = new decimal(2.4), IntIntoString = 13, IntArray = new[] { 4, 3, 6, 2 }, EnumerableDoubles = new List <double>() { 4.4, 5.3 }, Dictionary = new Dictionary <string, int>() { { "String1", 1 }, { "String2", 2 }, { "String3", 3 }, { "String4", 4 }, }, ArrayIntoList = new[] { "String1", "String2", "String3", "String4", "String5", }, ListIntoArray = new List <int> { 2, 4, 5, 1 }, ListNestedTypes = new List <NestedType>() { new NestedType() { String = "5", Int = 3 }, new NestedType() { String = "3", Int = 5 } } }; var secondObject = mapper.Map <SecondObject>(obj); }
public override bool AreEqual() { return(FirstObject.Equals(SecondObject)); }