Skip to content

Instantly share code, notes, and snippets.

@lbargaoanu
Created February 13, 2017 07:57
Show Gist options
  • Select an option

  • Save lbargaoanu/f23b61dd76ed5a40fb6d3926fb5d4201 to your computer and use it in GitHub Desktop.

Select an option

Save lbargaoanu/f23b61dd76ed5a40fb6d3926fb5d4201 to your computer and use it in GitHub Desktop.
public void Main()
{
Mapper.Initialize(cfg =>
{
cfg.CreateMap<Source, Destination>();
cfg.CreateMap<object, DataRow>().ConvertUsing((s, d)=>
{
foreach(var item in Mapper.Map<IDictionary<string, object>>(s))
{
d[item.Key] = item.Value;
}
return d;
});
});
var table = new DataTable("x");
table.Columns.Add("Value1");
table.Columns.Add("Value2");
table.Columns.Add("Value3");
var dataRow = table.NewRow();
Mapper.AssertConfigurationIsValid();
Mapper.Map(new Source { Value = new Dto { Value1 = 1, Value2 = "2", Value3 = 3 }}, new Destination { Value = dataRow }).Dump();
}
class Dto
{
public int Value1 { get; set; }
public string Value2 { get; set; }
public double Value3 { get; set; }
}
class Source
{
public Dto Value { get; set; }
}
class Destination
{
public DataRow Value { get; set; }
}
@flcdrg
Copy link
Copy Markdown

flcdrg commented Feb 14, 2017

I had to use ConvertUsing((s, d, context) => and then call context.Mapper.Map

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment