1

I'm trying to map an object with property names like so:

Property_One -> PropertyOne ... etc
Sample_Property -> SampleProperty

Is there a better way to do this than to map individually each property to another? The only difference is the underline.

Brian Liang
  • 7,734
  • 10
  • 57
  • 72
  • you can do it easily with the ValueInjecter http://valueinjecter.codeplex.com/documentation, it would be a modified SameNameType ValueInjection, you just need to replace the "_" with string.empty when looking for the target property – Omu May 05 '10 at 18:44

2 Answers2

5

You'll need to specify the underscore naming convention on the source side:

Mapper.Initialize(i => 
{
    i.SourceMemberNamingConvention = new LowerUnderscoreNamingConvention();
    i.CreateMap<Source, Dest>(); 
});

You can do that globally (as shown above) or per Profile, if only some of your source types follow this naming convention.

Jimmy Bogard
  • 26,045
  • 5
  • 74
  • 69
0
public class Source
{
    public string Property_One { get; set; }
}

public class Dest
{
    public string PropertyOne { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        Mapper.CreateMap<Source, Dest>()
            .ForMember(dest => dest.PropertyOne,
                       opt => opt.MapFrom(src => src.Property_One));

        var source = new Source
        {
            Property_One = "property1"
        };

        var destination = Mapper.Map<Source, Dest>(source);
        Console.WriteLine(destination.PropertyOne);
    }
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • This is what I am doing now, I need a generic way of mapping it. Looks like the INamingConvention interface will accomplish this. – Brian Liang Oct 27 '09 at 12:34