using System;
using AutoMapper;
namespace AutoMapOneToMulti
{
class Program
{
static void Main(string[] args)
{
RegisterMaps();
var s = new Source { X = 1, Y = 2 };
Console.WriteLine(s);
Console.WriteLine(Mapper.Map(s));
Console.WriteLine(Mapper.Map(s));
Console.ReadLine();
}
static void RegisterMaps()
{
Mapper.Initialize(cfg => cfg.AddProfile());
}
}
public class GeneralProfile : Profile
{
public GeneralProfile()
{
CreateMap();
CreateMap();
}
}
public class Source
{
public int X { get; set; }
public int Y { get; set; }
public override string ToString()
{
return string.Format("Source = X : {0}, Y : {1}", X, Y);
}
}
public class Destination1
{
public int X { get; set; }
public int Y { get; set; }
public override string ToString()
{
return string.Format("Destination1 = X : {0}, Y : {1}", X, Y);
}
}
public class Destination2
{
public int X { get; set; }
public int Y { get; set; }
public override string ToString()
{
return string.Format("Destination2 = X : {0}, Y : {1}", X, Y);
}
}
}
And for version below 5 you could try this:
using System;
using AutoMapper;
namespace AutoMapOneToMulti
{
class Program
{
static void Main(string[] args)
{
RegisterMaps();
var s = new Source { X = 1, Y = 2 };
Console.WriteLine(s);
Console.WriteLine(Mapper.Map(s));
Console.WriteLine(Mapper.Map(s));
Console.ReadLine();
}
static void RegisterMaps()
{
Mapper.Initialize(cfg => cfg.AddProfile());
}
}
public class GeneralProfile : Profile
{
protected override void Configure()
{
CreateMap();
CreateMap();
}
}
public class Source
{
public int X { get; set; }
public int Y { get; set; }
public override string ToString()
{
return string.Format("Source = X : {0}, Y : {1}", X, Y);
}
}
public class Destination1
{
public int X { get; set; }
public int Y { get; set; }
public override string ToString()
{
return string.Format("Destination1 = X : {0}, Y : {1}", X, Y);
}
}
public class Destination2
{
public int X { get; set; }
public int Y { get; set; }
public override string ToString()
{
return string.Format("Destination2 = X : {0}, Y : {1}", X, Y);
}
}
}
If you want a dynamic function, use this extension:
public static dynamic DaynamicMap(this Source source)
{
if (source.X == 1)
return Mapper.Map(source);
return Mapper.Map(source);
}
Console.WriteLine(new Source { X = 1, Y = 2 }.DaynamicMap());
Console.WriteLine(new Source { X = 2, Y = 2 }.DaynamicMap());