public void Test2()
        {
            IServiceCollection sc = new ServiceCollection()
                                    .AddLightweightMapper();

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

            List <string> target = new List <string>();

            provider.CopyTo(new List <string> {
                "A", "B"
            }, target);
            Assert.Equal("A", target[0]);
            Assert.Equal("B", target[1]);

            string[] invalidList = new string[0];
            provider.CopyTo(new List <String> {
                "A"
            }, invalidList);

            List <S2> t2 = new List <S2>();

            provider.CopyTo(new List <S1> {
                new S1 {
                    A = "10"
                }
            }, t2);
            Assert.Single(t2);
            Assert.Equal("10", t2[0].A);

            List <S3> t3 = new List <S3>()
            {
                new S3()
                {
                    A = "A", B = new S2()
                    {
                        A = "B"
                    }
                },
                new S3()
                {
                    A = "A1", B = new S2()
                    {
                        A = "B1"
                    }
                },
            };
            List <S4> t4 = new List <S4>();

            provider.CopyTo(t3, t4);
            Assert.Equal(2, t4.Count);
            Assert.Equal("B1", t4[1].B.A);
        }
        public void Test1()
        {
            IServiceCollection sc = new ServiceCollection()
                                    .AddLightweightMapper();

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

            provider.CopyTo(1, 1);
            int?a = 0;

            provider.CopyTo <int?, int?>(null, a);
        }
Ejemplo n.º 3
0
        private void onConfigChanged(WebApiConfig config, string name)
        {
            ServiceCollectionExtensions._configAction?.Invoke(config);
            WebApiConfig old = _serviceProvider.GetRequiredService <WebApiConfig>();

            if (old != config)
            {
                IMapperProvider mapper = _serviceProvider.GetRequiredService <IMapperProvider>();
                mapper.CopyTo <WebApiConfig, WebApiConfig>(config, old);
            }
        }
        public void Test4()
        {
            IServiceCollection sc = new ServiceCollection()
                                    .AddLightweightMapper();

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

            S3 s1 = new S3()
            {
                A = "A", B = new S2 {
                    A = "B"
                }
            };
            S4 t1 = new S4()
            {
                B = new S1()
            };
            var t1_b = t1.B;

            provider.CopyTo(s1, t1);
            Assert.Equal("A", t1.A);
            Assert.Equal("B", t1.B.A);
            Assert.Equal(t1_b, t1.B);

            t1.B = null;
            provider.CopyTo(s1, t1);
            Assert.Equal("B", t1.B.A);

            s1.B = null;
            t1.B = null;
            provider.CopyTo(s1, t1);
            Assert.Null(t1.B);

            s1.B = null;
            provider.CopyTo(s1, t1);
            Assert.Null(t1.B);
        }
        public void Test3()
        {
            IServiceCollection sc = new ServiceCollection()
                                    .AddLightweightMapper();

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

            Dictionary <string, string> dic = new Dictionary <string, string>();

            provider.CopyTo(new Dictionary <string, string>
            {
                { "A", "B" }
            }, dic);
            Assert.Single(dic);
            Assert.Equal("B", dic["A"]);

            Dictionary <S3, S4> s1 = new Dictionary <S3, S4>()
            {
                {
                    new S3 {
                        A = "A", B = new S2 {
                            A = "A1"
                        }
                    }, new S4 {
                        A = "B", B = new S1 {
                            A = "B1"
                        }
                    }
                }
            };
            Dictionary <S4, S3> t1 = new Dictionary <S4, S3>();

            provider.CopyTo(s1, t1);
            Assert.Single(t1);
            Assert.Equal("A1", t1.Keys.ToList()[0].B.A);
            Assert.Equal("B1", t1.Values.ToList()[0].B.A);
        }
Ejemplo n.º 6
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);
        }
        public void Test10()
        {
            IServiceCollection sc = new ServiceCollection()
                                    .AddLightweightMapper();

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

            S5 s5 = new S5()
            {
                S1 = new S1()
                {
                    A = 10
                }
            };

            s5.Self = s5;

            T5 t5 = new T5();

            provider.CopyTo <S5, T5>(s5, t5);
            Assert.Equal(t5.Self, t5.Self.Self);
            Assert.Equal(10, t5.T1.A);

            // List
            List <S5> list = new List <S5>();

            list.Add(s5);
            list.Add(s5);
            list.Add(s5);

            List <T5> t5List = new List <T5>();

            provider.CopyTo <List <S5>, List <T5> >(list, t5List);
            Assert.NotNull(t5List);
            Assert.Equal(3, t5List.Count);
            Assert.Equal(t5List[0], t5List[1]);
            Assert.Equal(t5List[0], t5List[2]);

            Dictionary <S5, S5> dic = new Dictionary <S5, S5>()
            {
                { s5, s5 },
                { new S5()
                  {
                      S1 = new S1 {
                          A = 11
                      }
                  }, s5 }
            };

            Dictionary <T5, T5> targetDic = new Dictionary <T5, T5>();

            provider.CopyTo <Dictionary <S5, S5>, Dictionary <T5, T5> >(dic, targetDic);
            Assert.NotNull(targetDic);
            Assert.Equal(2, targetDic.Count);
            var item1 = dic.First();
            var item2 = dic.Last();

            Assert.Equal(item1.Value, item2.Value);
            Assert.Equal(item1.Key, item1.Value);
            Assert.Equal(11, item2.Key.S1.A);
        }