我的理解是我可以通過以下方式配置AutoMapper,並且在映射過程中,它應該將所有源模型日期格式化爲IValueFormatter中定義的規則,並將結果設置爲映射模型。AutoMapper:對於給定類型的網站廣泛使用IValueFormatter
ForSourceType<DateTime>().AddFormatter<StandardDateFormatter>();
ForSourceType<DateTime?>().AddFormatter<StandardDateFormatter>();
我對此類映射類沒有任何效果。
Mapper.CreateMap<Member, MemberForm>().ForMember(x => x.DateOfBirth, y => y.AddFormatter<StandardDateFormatter>());
我映射日期時間:當我做了以下它只適用? Member.DateOfBirth至string MemberForm.DateOfBirth。格式化程序基本上從日期創建一個短日期字符串。
設置給定類型的默認格式化程序時是否存在缺少的內容?
感謝
public class StandardDateFormatter : IValueFormatter
{
public string FormatValue(ResolutionContext context)
{
if (context.SourceValue == null)
return null;
if (!(context.SourceValue is DateTime))
return context.SourceValue.ToNullSafeString();
return ((DateTime)context.SourceValue).ToShortDateString();
}
}
感謝您的幫助。我嘗試了這些例子,但仍然沒有運氣。我將繼續爲每個字段添加格式化程序。 – CRice 2010-05-14 04:48:43