public static void TestCopyNullableOverNonnullable() { var src = new D(); var dest = new C(); var copier = new FieldCopier <C, D>(new[] { "sometimesnullable" }); src.sometimesnullable = null; copier.Copy(dest, src); Check.That(dest.sometimesnullable).Equals(0); src.sometimesnullable = 12; copier.Copy(dest, src); Check.That(dest.sometimesnullable).Equals(12); src.sometimesnullable = null; copier.Copy(dest, src); Check.That(dest.sometimesnullable).Equals(0); }
public static void TestCopier() { c1.a = 111; c1.b = 999; var copier = new FieldCopier <C, C>(fields); copier.Copy(c1, c2); }
public static void Benchmark() { var c1 = new C() { a = 666, b = 12, date = DateTime.Now, mynullable = DateTime.Now }; var d1 = new D() { a = 666, b = 8, c = 9, date = DateTime.Now, mynullable = DateTime.Now }; var fields = new[] { "a", "b" }; WriteLine("Stats (microseconds) per iteration"); RepeatBench("new Differ()", 1000, () => { var dd = new Differ <C, D>(fields); }); var d = new Differ <C, D>(fields); RepeatBench("Compare small objects", 1000000, () => { var res = d.Compare(c1, d1); }); RepeatBench("new FieldExtract()", 1000, () => { var dd = new FieldExtract <C, int>(fields); }); var extractor = new FieldExtract <C, int>(fields); RepeatBench("Extract integers", 1000000, () => { var ints = extractor.Extract(c1); }); // big data var big1 = new BigDto(); var big2 = new BigDto(); var bigprops = ReflectionHelper.CollectProps <BigDto>(); var bigpropnames = bigprops.SelectMany(p => p.Item2).ToArray(); RepeatBench("new Differ() for big class", 100, () => { var dd = new Differ <BigDto, BigDto>(bigpropnames); }); var bigd = new Differ <BigDto, BigDto>(bigpropnames); RepeatBench("Compare large objects", 10000, () => { bigd.Compare(big1, big2); }); var types = ReflectionHelper.CollectProps <BigDto>(); var e4 = ReflectionHelper.GetExtractorFor <BigDto, int>(types); var e5 = ReflectionHelper.GetExtractorFor <BigDto, string>(types); var e6 = ReflectionHelper.GetExtractorFor <BigDto, decimal>(types); RepeatBench("Extract fields from large object", 10000, () => { var r1 = e4.Extract(big1); var r2 = e5.Extract(big1); var r3 = e6.Extract(big1); }); RepeatBench("Extract fields from large object, convert to dict, naive", 10000, () => { var pd = e4.ResultsAsDict(e4.Extract(big1).Select(i => i.ToString()).ToList()) .Union(e5.ResultsAsDict(e5.Extract(big1).Select(e => e.ToString()).ToList())) .Union(e6.ResultsAsDict(e6.Extract(big1).Select(e => e.ToString()).ToList())); }); var boxedExtract = new FieldExtract <BigDto, object>(bigpropnames); RepeatBench("Extract fields, boxed", 100000, () => { var r1 = boxedExtract.Extract(big1); }); RepeatBench("Extract fields from large dto. string -> object dict", 10000, () => { var r1 = boxedExtract.Extract(big1); var r2 = boxedExtract.ResultsAsZip(r1); }); var propertyInfos = typeof(BigDto).GetProperties(); RepeatBench("Extract fields with reflection", 10000, () => { foreach (var p in propertyInfos) { var val = p.GetValue(big1); } }); RepeatBench("Extract fields with reflection, convert to string dict", 10000, () => { var resdict = new Dictionary <string, string>(); foreach (var p in propertyInfos) { var val = p.GetValue(big1); var s = val.ToString(); resdict[p.Name] = s; } }); var copier = new FieldCopier <BigDto, BigDto>(bigpropnames); RepeatBench("Copy big object", 100000, () => { copier.Copy(big1, big2); }); }