Пример #1
0
        public void MapTo()
        {
            //RuntimeServicePools.TryAdd<IProfileConfiguration, ProfileConfiguration>();

            var mapTo = RuntimeServicePools.Singleton <IMapToExpression, MapToExpression>();

            //? 为类型“CopyTest”指定代理。
            mapTo.Run <CopyToTest, MapToTest>(source =>
            {
                return(new MapToTest
                {
                    Id = source.Id,
                    Name = source.Name,
                    Date = source.Date
                });
            });

            mapTo.Map <MapToTest>(typeof(CopyToTest), source => null);

            mapTo.Map <MapToTest>(type => true, source => null);

            var value = new CopyToTest
            {
                Id   = 1000,
                Name = "test",
                Date = DateTime.Now
            };

            for (int i = 0; i < 100000; i++)
            {
                var map1 = value.MapTo <CopyTest>();

                value.Name = "test1";

                var map2 = value.MapTo <MapToTest>();

                //var map3 = value.MapTo<IEnumerable<KeyValuePair<string, object>>>();

                //var map4 = value.MapTo<ICollection<KeyValuePair<string, object>>>();

                var map5 = value.MapTo <Dictionary <string, object> >();

                value.Name = "test5";
            }
        }
Пример #2
0
        public void CopyTo()
        {
            var copyTo = new CopyToExpression();

            //? 为类型“CopyTest”指定代理。
            copyTo.Use((profile, type) =>
            {
                if (type == typeof(CopyToTest))
                {
                    return(source =>
                    {
                        var copy = (CopyTest)source;

                        return new CopyTest
                        {
                            Id = copy.Id,
                            Name = copy.Name
                        };
                    });
                }
                return(profile.Create <CopyTest>(type));
            });

            RuntimeServicePools.TryAddSingleton <ICopyToExpression>(() => copyTo);

            var value = new CopyToTest
            {
                Id   = 1000,
                Name = "test",
                Date = DateTime.Now
            };

            for (int i = 0; i < 100000; i++)
            {
                var copy1 = value.CopyTo();

                value.Name = "test1";

                var copy2 = value.CopyTo <CopyTest>();

                value.Name = "test5";
            }
        }