Skip to content

Mapping.ObjectToObject

Igor Tkachev edited this page May 20, 2016 · 1 revision

Home / Mapping

Note that the mapper tries to convert the source values to their destination types.

ObjectToObject.cs

using System;

using NUnit.Framework;

using BLToolkit.Mapping;

namespace HowTo.Mapping
{
    [TestFixture]
    public class ObjectToObject
    {
        public class SourceObject
        {
            public bool   Value1   = true;
            public string Value2   = "10";
            public string StrValue = "test";
        }

        public class DestObject
        {
            [MapField("Value1")] public bool   BoolValue;
            [MapField("Value2")] public int    IntValue;

            // If the source and destination field/property names are equal,
            // there is no need for using the MapField attribute.
            //
                                 public string StrValue; 
        }

        [Test]
        public void Test1()
        {
            SourceObject source = new SourceObject();
            DestObject   dest   = Map.ObjectToObject<DestObject>(source);

            Assert.AreEqual(true,   dest.BoolValue);
            Assert.AreEqual(10,     dest.IntValue);
            Assert.AreEqual("test", dest.StrValue);
        }
    }
}
Clone this wiki locally