user3685348
user3685348

Reputation: 1

AutoMapper NamingConvention - why doesn't this work?

New to Automapper - Confused why this doesn't work...Thanks!!!!

public class Dog
{
    public string Bark { get; set; }
}

public class Pig
{
    public string bark { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        Mapper.Initialize(cfg =>
        {
            cfg.DestinationMemberNamingConvention = new PascalCaseNamingConvention();
            cfg.SourceMemberNamingConvention = new LowerUnderscoreNamingConvention();
        });
        var map = Mapper.Map<Pig, Dog>(new Pig());
    }
}

Upvotes: 0

Views: 240

Answers (1)

Darek
Darek

Reputation: 4797

You should use DynamicMap:

namespace ConsoleApplication5
{
    #region

    using AutoMapper;

    #endregion

    internal class Program
    {
        #region Methods

        private static void Main(string[] args)
        {
            Mapper.Initialize(
                cfg =>
                {
                    cfg.DestinationMemberNamingConvention = new PascalCaseNamingConvention();
                    cfg.SourceMemberNamingConvention = new LowerUnderscoreNamingConvention();
                });
            var dog = Mapper.DynamicMap<Pig, Dog>(
                new Pig
                {
                    bark = "Bark!"
                });
        }

        #endregion
    }

    public class Dog
    {
        #region Public Properties

        public string Bark { get; set; }

        #endregion
    }

    public class Pig
    {
        #region Public Properties

        public string bark { get; set; }

        #endregion
    }
}

Or configure AutoMapper first:

        Mapper.CreateMap<Pig, Dog>();

        var dog = Mapper.Map<Dog>(
            new Pig
            {
                bark = "Bark!"
            });

That's all.

Upvotes: 3

Related Questions