Skip to content

M_CodeJam_Mapping_MapperBuilder_2_MapMember__1

andrewvk edited this page Jun 24, 2016 · 9 revisions

MapperBuilder(TFrom, TTo).MapMember(T) Method

Adds member mapper.

Namespace: CodeJam.Mapping
Assembly: CodeJam (in CodeJam.dll) Version: 1.0.0.0 (1.0.0.0)

Syntax

C#

public MapperBuilder<TFrom, TTo> MapMember<T>(
	Expression<Func<TTo, T>> toMember,
	Expression<Func<TFrom, T>> setter
)

VB

Public Function MapMember(Of T) ( 
	toMember As Expression(Of Func(Of TTo, T)),
	setter As Expression(Of Func(Of TFrom, T))
) As MapperBuilder(Of TFrom, TTo)

F#

member MapMember : 
        toMember : Expression<Func<'TTo, 'T>> * 
        setter : Expression<Func<'TFrom, 'T>> -> MapperBuilder<'TFrom, 'TTo> 

Parameters

 

toMember
Type: System.Linq.Expressions.Expression(Func(TTo, T))
Expression that returns a member to map.
setter
Type: System.Linq.Expressions.Expression(Func(TFrom, T))
Expression to set the member.

Type Parameters

 

T
Type of the member to map.

Return Value

Type: MapperBuilder(TFrom, TTo)
Returns this mapper.

Examples

This example shows how to explicitly convert one value to another. C#

using System;

using NUnit.Framework;

#pragma warning disable 649

namespace CodeJam.Mapping.Examples
{
    [TestFixture]
    public class MapMemberTests
    {
        class Class1
        {
            public int    Prop1 { get; set; }
            public string Field1;
        }

        class Class2
        {
            public string    Prop1 { get; set; }
            public DateTime? Field1;
        }

        static readonly Mapper<Class1,Class2> _class1ToClass2Mapper =
            Map.GetMapper<Class1,Class2>(m =>
                m.MapMember(c2 => c2.Field1, c1 => DateTime.Parse(c1.Field1).AddDays(1)));

        [Test]
        public void Test1()
        {
            var c2 = _class1ToClass2Mapper.Map(
                new Class1
                {
                    Prop1  = 41,
                    Field1 = "2016-01-01"
                });

            Assert.That(c2.Prop1,        Is.EqualTo("41"));
            Assert.That(c2.Field1?.Year, Is.EqualTo(2016));
            Assert.That(c2.Field1?.Day,  Is.EqualTo(2));
        }
    }
}

See Also

Reference

MapperBuilder(TFrom, TTo) Class
CodeJam.Mapping Namespace

Clone this wiki locally