Created
June 13, 2019 11:46
-
-
Save Rensvind/295eba9aff1a58b543ad083bba016d81 to your computer and use it in GitHub Desktop.
Problem when mapping DateTime
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static void Main(string[] args) | |
{ | |
var config = new MapperConfiguration(cfg => | |
{ | |
cfg.CreateMap<Period1, Period2>() | |
.ForMember(x => x.StartDate, o => o.MapFrom(p => p.Start)) | |
.ForMember(x => x.EndDate, o => o.MapFrom(p => p.End)); | |
}); | |
config.AssertConfigurationIsValid(); | |
var mapper = config.CreateMapper(); | |
var p1 = new Period1 | |
{ | |
Start = DateTime.UtcNow.AddDays(-2), | |
End = DateTime.UtcNow | |
}; | |
var p2 = mapper.Map<Period2>(p1); | |
if(p1.Start == p2.StartDate) | |
{ | |
//Yay, success! | |
} | |
else | |
throw new Exception($"{p1.Start} != {p2.StartDate}"); | |
} | |
public class Period1 | |
{ | |
public Period1() | |
{ } | |
public Period1(DateTime start, DateTime end) | |
{ | |
Start = start; | |
End = end; | |
} | |
public DateTime Start { get; set; } | |
public DateTime End { get; set; } | |
} | |
public class Period2 | |
{ | |
public Period2() | |
{ | |
} | |
public Period2(DateTime startDate, DateTime endDate) | |
{ | |
StartDate = startDate; | |
EndDate = endDate; | |
} | |
public DateTime StartDate { get; set; } | |
public DateTime EndDate { get; set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment