예제 #1
0
        public void MappingTests_should_resolve_properties()
        {
            var config = new MapperConfiguration(cfg => cfg.CreateMap <SourceA, TargetA>());

            config.AssertConfigurationIsValid();
            var mapper = config.CreateMapper();

            var source = new SourceA
            {
                PropertyA = "property A",
                SourceB   = new SourceB {
                    PropertyB = "property B"
                },
                List = new List <SourceC> {
                    new SourceC {
                        PropertyC = "property C"
                    }
                },
                SourceD = new SourceD {
                    PropertyD = "property D"
                }
            };

            var target = mapper.Map <SourceA, TargetA>(source);

            Assert.AreEqual(source.PropertyA, target.PropertyA);
            Assert.AreEqual(source.SourceB.PropertyB, target.PropertyB);
            Assert.AreEqual(source.List.First().PropertyC, target.PropertyC);
            Assert.AreEqual(source.SourceD?.PropertyD, target.TargetB?.PropertyD);
        }
예제 #2
0
        public void TestMapper()
        {
            // SourceA --> TargetA
            // A==>A B==>D C==>E  A==>X1 C==>X2
            IServiceCollection sc = new ServiceCollection()
                                    .AddLightweightMapper();

            IServiceProvider sp       = sc.BuildServiceProvider();
            IMapperProvider  provider = sp.GetRequiredService <IMapperProvider>();

            SourceA a = new SourceA()
            {
                A  = "A",
                B  = "B",
                C  = "C",
                SA = new StructA()
                {
                    X = 1,
                    Y = 2
                }
            };
            var ta = provider.Convert <SourceA, TargetA>(a);

            Assert.Equal("A", ta.A);
            Assert.Equal("B", ta.D);
            Assert.Equal("C", ta.E);
            Assert.Equal("A", ta.X1);
            Assert.Equal("C", ta.X2);
            Assert.False(Object.ReferenceEquals(ta.TSA, a.SA));
            Assert.Equal(1, ta.TSA.X);
            Assert.Equal(2, ta.TSA.Y);
        }
예제 #3
0
        //static void Main1(string[] args)
        static void Main(string[] args)
        {
            //简单的转换
            var mapper = ObjectMapperManager.DefaultInstance.GetMapper <SourceA, desB>(
                new DefaultMapConfig()
                //属性指定
                .MatchMembers((x, y) =>
            {
                if (x == "dt" && y == "dateTime")
                {
                    return(true);
                }
                return(x == y);
            }
                              )
                //属性转换
                .PostProcess <desB>((value, state) =>
            {
                value.stra    = "这是字符串:" + value.stra;
                value.JsonStr = @"{'stra':'" + value.stra + "','dateTime':'" + value.dateTime + "'}";
                return(value);
            })
                //属性忽略
                .IgnoreMembers <SourceA, desB>(new string[] { "name" })
                //属性null时的默认值
                .NullSubstitution <DateTime?, DateTime>((value) => DateTime.Now)
                );
            var a = new SourceA
            {
                dt      = DateTime.Now,
                inta    = 1,
                stra    = "3",
                dt2     = DateTime.Now.AddDays(1),
                name    = "McXmart",
                address = null
            };
            var b = mapper.Map(a);

            if (b != null)
            {
                Console.WriteLine("b.DateTime:" + b.dateTime);
                Console.WriteLine("b.int:" + b.inta);
                Console.WriteLine("b.stra:" + b.stra);
                Console.WriteLine("b.JsonStr :" + b.JsonStr);
                Console.WriteLine("b.name :" + b.name);
                Console.WriteLine("b.address :" + b.address);
            }
            Console.ReadKey();
        }
예제 #4
0
        public void AppendValues(double L, double a, double b, double C, double H, double T)
        {
            DateTimeAxis axis = new DateTimeAxis();
            double       time = axis.ConvertToDouble(DateTime.Now);
            Point        p1   = new Point(time, L);
            Point        p2   = new Point(time, a);
            Point        p3   = new Point(time, b);
            Point        p4   = new Point(time, C);
            Point        p5   = new Point(time, H);
            Point        p6   = new Point(time, T);

            SourceL.AppendAsync(App.Current.Dispatcher, p1);
            SourceA.AppendAsync(App.Current.Dispatcher, p2);
            SourceB.AppendAsync(App.Current.Dispatcher, p3);
            SourceC.AppendAsync(App.Current.Dispatcher, p4);
            SourceH.AppendAsync(App.Current.Dispatcher, p5);
            SourceT.AppendAsync(App.Current.Dispatcher, p6);

            Thread.Sleep(10);
        }
예제 #5
0
        public void TestCopyTo()
        {
            IServiceCollection sc = new ServiceCollection()
                                    .AddLightweightMapper();

            IServiceProvider sp       = sc.BuildServiceProvider();
            IMapperProvider  provider = sp.GetRequiredService <IMapperProvider>();

            SourceA a = new SourceA()
            {
                A = "A",
                B = "B",
                C = "C"
            };
            TargetA ta = new TargetA();

            provider.CopyTo <SourceA, TargetA>(a, ta);
            Assert.Equal("A", ta.A);
            Assert.Equal("B", ta.D);
            Assert.Equal("C", ta.E);
            Assert.Equal("A", ta.X1);
            Assert.Equal("C", ta.X2);
        }
예제 #6
0
        public void TestCopyDefine()
        {
            IServiceCollection sc = new ServiceCollection()
                                    .AddLightweightMapper();

            IServiceProvider sp       = sc.BuildServiceProvider();
            IMapperProvider  provider = sp.GetRequiredService <IMapperProvider>();

            SourceA a = new SourceA()
            {
                A = "A",
                B = "B",
                C = "C"
            };
            TargetA ta       = new TargetA();
            var     copyFunc = provider.DefineCopyTo <SourceA, TargetA>();

            copyFunc(a, ta);
            Assert.Equal("A", ta.A);
            Assert.Equal("B", ta.D);
            Assert.Equal("C", ta.E);
            Assert.Equal("A", ta.X1);
            Assert.Equal("C", ta.X2);

            var copyFunc2 = provider.DefineCopyTo <SourceA, TargetA>(t => new
            {
                A = t.A
            });

            ta = new TargetA();
            copyFunc2(a, ta);
            Assert.Null(ta.A);
            Assert.Equal("B", ta.D);
            Assert.Equal("C", ta.E);
            Assert.Null(ta.X1);
            Assert.Equal("C", ta.X2);
        }
예제 #7
0
 public void AddItemToSourceA()
 {
     SourceA.Add(new Person("Max - " + SourceA.Count, "M" + SourceA.Count));
 }