当前位置: 首页>>代码示例>>C#>>正文


C# Mapper.Map方法代码示例

本文整理汇总了C#中Mapper.Map方法的典型用法代码示例。如果您正苦于以下问题:C# Mapper.Map方法的具体用法?C# Mapper.Map怎么用?C# Mapper.Map使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Mapper的用法示例。


在下文中一共展示了Mapper.Map方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: ShouldMapColumnsToExpectedTypeBasedOnSpecifiedFieldOrder

 public void ShouldMapColumnsToExpectedTypeBasedOnSpecifiedFieldOrder()
 {
     var mapper = new Mapper<SpecifiedOrderTargetType>(new ErrorCollector());
     var target = mapper.Map(new[] {"4", "hello"});
     Assert.Equal("hello", target.AString);
     Assert.Equal(4, target.AnInt);
 }
开发者ID:wayneqs,项目名称:claims,代码行数:7,代码来源:MapperFacts.cs

示例2: Map_WhenInvoked_ShouldWork

        public void Map_WhenInvoked_ShouldWork()
        {
            // Arrange
            var mapper = new Mapper<Schedule, ScheduleModel>()
                // Straigt mapping
                .Map(e => e.Title, e => e.Title)
                // Mapping properties with different names
                .Map((e, i) => e.Tasks[i].Title, (e, i) => e.Tasks[i].DisplayName)
                // Mapping string <-> decimal, string-to-primitive-type converter is picked up automatically
                .Map((e, i) => e.Tasks[i].Budget, (e, i) => e.Tasks[i].Budget)
                // Mapping array of convertible types supported too
                .Map((e, i, j) => e.Tasks[i].Points[j], (e, i, j) => e.Tasks[i].Points[j])
                // Two-level nesting is supported too
                .Map((e, i, j) => e.Tasks[i].Responsible[j].Id, (e, i, j) => e.Tasks[i].ResponsibleUsers[j].Id)
                .Map((e, i, j) => e.Tasks[i].Responsible[j].DisplayName, (e, i, j) => e.Tasks[i].ResponsibleUsers[j].Display);

            var schedule = fixture.Create<Schedule>();

            // Act
            // From entity to model
            var scheduleModel = mapper.Map(schedule);
            // And from model to entity
            var newSchedule = mapper.Map(scheduleModel);

            // Assert
            newSchedule.Title.Should().Be(schedule.Title);
            newSchedule.Tasks.Should().NotBeNull();
            newSchedule.Tasks.Length.Should().Be(schedule.Tasks.Length);

            for (var i = 0; i < newSchedule.Tasks.Length; i++)
            {
                newSchedule.Tasks[i].Budget.Should().Be(schedule.Tasks[i].Budget);
                newSchedule.Tasks[i].Title.Should().Be(schedule.Tasks[i].Title);
                newSchedule.Tasks[i].Points.Length.Should().Be(schedule.Tasks[i].Points.Length);

                for (var j = 0; j < newSchedule.Tasks[i].Points.Length; j++)
                    newSchedule.Tasks[i].Points[j].Should().BeCloseTo(schedule.Tasks[i].Points[j], 1000);

                newSchedule.Tasks[i].Responsible.Count.Should().Be(schedule.Tasks[i].Responsible.Count);
                for (var j = 0; j < newSchedule.Tasks[i].Responsible.Count; j++)
                {
                    newSchedule.Tasks[i].Responsible[j].DisplayName.Should().Be(newSchedule.Tasks[i].Responsible[j].DisplayName);
                    newSchedule.Tasks[i].Responsible[j].Id.Should().Be(newSchedule.Tasks[i].Responsible[j].Id);
                }
            }
        }
开发者ID:rd-dev-ukraine,项目名称:two-way-mapper,代码行数:46,代码来源:ExampleTest.cs

示例3: Should_Create_Assign

        public void Should_Create_Assign()
        {
            Helpers.ActionLambda<Entity, Model>(_map)(_entity, _model);
            _model.subEntities.First().name.ShouldEqual("Dillon");

            var mapper = new Mapper<Entity, Model>(null);
            Expression<Func<IList<Entity>, IList<Model>>> fark = e => e.Select(q => mapper.Map(q, new Model())).ToList();
        }
开发者ID:Grummle,项目名称:yamm,代码行数:8,代码来源:ListMapTests.cs

示例4: ReturnsNullIfSourceValueIsNull

            public void ReturnsNullIfSourceValueIsNull()
            {
                var config = new MapperConfiguration(_ => { });
                IMapper mapper = new Mapper(config);

                var result = mapper.Map((NameValueCollection)null, new NameValueCollection());

                result.ShouldBeNull();
            }
开发者ID:GeertVL,项目名称:AutoMapper,代码行数:9,代码来源:NameValueCollectionMapperTests.cs

示例5: ReturnsEmptyCollectionWhenSourceCollectionIsEmpty

            public void ReturnsEmptyCollectionWhenSourceCollectionIsEmpty()
            {
                var config = new MapperConfiguration(_ => { });
                IMapper mapper = new Mapper(config);

                var result = mapper.Map(new NameValueCollection(), (NameValueCollection)null);

                result.ShouldBeEmpty(); 
            }
开发者ID:GeertVL,项目名称:AutoMapper,代码行数:9,代码来源:NameValueCollectionMapperTests.cs

示例6: ShouldRecordErrorIfNotEnoughColumnsToExtractFrom

        public void ShouldRecordErrorIfNotEnoughColumnsToExtractFrom()
        {
            var collector = new ErrorCollector();
            Assert.False(collector.ContainsErrors);

            var mapper = new Mapper<DefaultOrderTargetType>(collector);
             mapper.Map(new[] {"hello", "4"});
            Assert.True(collector.ContainsErrors);
            Console.WriteLine(collector.ToString()); // if you're interested
        }
开发者ID:wayneqs,项目名称:claims,代码行数:10,代码来源:MapperFacts.cs

示例7: ShouldRecordErrorIfUnableToParseTheInput

        public void ShouldRecordErrorIfUnableToParseTheInput()
        {
            var collector = new ErrorCollector();
            Assert.False(collector.ContainsErrors);

            var mapper = new Mapper<SpecifiedOrderTargetType>(collector);
             mapper.Map(new[] {"4,787a", "hello" });
            Assert.True(collector.ContainsErrors);
            Console.WriteLine(collector.ToString()); // if you're interested
        }
开发者ID:wayneqs,项目名称:claims,代码行数:10,代码来源:MapperFacts.cs

示例8: ShouldMapColumnsToExpectedTypeBasedOnDefaultFieldOrder

        public void ShouldMapColumnsToExpectedTypeBasedOnDefaultFieldOrder()
        {
            var mapper = new Mapper<DefaultOrderTargetType>(new ErrorCollector());
            var target = mapper.Map(new[] {"hello", "4", "23-Jun-2012"});
            Assert.Equal("hello", target.AString);
            Assert.Equal(4, target.AnInt);

            // ok; mapping datetimes like this is a bit dodgy because ParseExact isn't being used under the hood
            // but for the purposes of the narrow scope of this example project i hope its ok.
            Assert.Equal(new DateTime(2012, 6, 23), target.ADateTime);
        }
开发者ID:wayneqs,项目名称:claims,代码行数:11,代码来源:MapperFacts.cs

示例9: ReturnsMappedObjectWithExpectedValuesWhenSourceCollectionHasOneItem

            public void ReturnsMappedObjectWithExpectedValuesWhenSourceCollectionHasOneItem()
            {
                var config = new MapperConfiguration(_ => { });
                IMapper mapper = new Mapper(config);
                var sourceValue = new NameValueCollection() { { "foo", "bar" } };

                var result = mapper.Map(sourceValue, new NameValueCollection());

                1.ShouldEqual(result.Count);
                "foo".ShouldEqual(result.AllKeys[0]);
                "bar".ShouldEqual(result["foo"]);
            }
开发者ID:GeertVL,项目名称:AutoMapper,代码行数:12,代码来源:NameValueCollectionMapperTests.cs

示例10: InputCastAsObjectMapsToSameCode

        public void InputCastAsObjectMapsToSameCode()
        {
            // Arrange
            var firstValue = "firstValue";
            var firstCode = 0;
            var lookup = new Dictionary<string, int>
            {
                {firstValue, firstCode}
            };

            var mapper = new Mapper<string, int>(lookup);

            // Act
            var firstResult = mapper.Map(firstValue);
            var secondResult = mapper.Map((object)firstValue);

            // Assert
            Assert.AreEqual(firstCode, firstResult);
            Assert.IsInstanceOfType(secondResult, typeof(int));
            Assert.AreEqual(firstCode, (int)secondResult);
        }
开发者ID:adamgoral,项目名称:ML,代码行数:21,代码来源:MapperTests.cs

示例11: When_Mapping_Then_Mapped

        public void When_Mapping_Then_Mapped()
        {
            // Arrange
            sut = new Mapper();
            var from = new List<string> {"one", "two", "three"};

            // Act
            var res = sut.Map<List<string>>(from);

            // Assert
            Assert.NotNull(res);
            CollectionAssert.AreEqual(from, res);
        }
开发者ID:jonclare,项目名称:KodeKandy,代码行数:13,代码来源:Given_Mapper.cs

示例12: Map_WhenInvokedForNullArray_ShouldNotCreateDest

        public void Map_WhenInvokedForNullArray_ShouldNotCreateDest()
        {
            // Arrange
            var mapper = new Mapper<SArr, DArr>()
                .Map(s => s.Counter, s => s.Number)
                .Map((s, i) => s.Src[i].Name, (s, i) => s.Dst[i].Name);

            var src = fixture.Create<DArr>();
            src.Dst = null;

            // Act
            var dst = mapper.Map(src);

            // Assert
            Assert.Equal(src.Number, dst.Counter);
            Assert.Null(dst.Src);
        }
开发者ID:rd-dev-ukraine,项目名称:two-way-mapper,代码行数:17,代码来源:TwoWayMapperArrayTest.cs

示例13: Maps

        public void Maps()
        {
            var mapper = new Mapper<Person, PersonDto>();
            mapper.AddMapping(x => x.Name, y => y.FullName);
            mapper.AddMapping(x => x.Age, y => y.AgeInYears);

            var person = new Person
                {
                    Name = "Chuck Norris",
                    Age = 433,
                    Title = "Texas Ranger"
                };

            PersonDto personDto = mapper.Map(person);

            personDto.FullName.Should().Be("Chuck Norris");
            personDto.AgeInYears.Should().Be(433);
        }
开发者ID:Jonne,项目名称:demos,代码行数:18,代码来源:MappingSpec.cs

示例14: MappingDoesNotWorkBetweenTypes

        public void MappingDoesNotWorkBetweenTypes()
        {
            //Arrange
            var sut = new Mapper();

            //Act
            var result = sut.Map<Event>(new
            {
                Participants = new List<Participant>()
                {
                    new Participant() {
                        Name = "test"
                    }
                }
            });

            //Assert
            Assert.Null(result.Participants);
        }
开发者ID:joakimjm,项目名称:eziou,代码行数:19,代码来源:MapperTests.cs

示例15: MappingFromSameTypeIncludesNestedTypes

        public void MappingFromSameTypeIncludesNestedTypes()
        {
            //Arrange
            var sut = new Mapper();

            //Act
            var result = sut.Map<Event>(new Event
            {
                Participants = new List<Participant>()
                {
                    new Participant() {
                        Name = "test"
                    }
                }
            });

            //Assert
            Assert.Equal(1, result.Participants.Count());
        }
开发者ID:joakimjm,项目名称:eziou,代码行数:19,代码来源:MapperTests.cs


注:本文中的Mapper.Map方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。