我正在尝试在映射过程中清除字符串EmailInstructions
,但是当我使用以下命令调用Mapper.Configuration.AssertConfigurationIsValid();
时失败:
No coercion operator is defined between types 'System.String' and 'ElectionViewModel'
这是我的设置:
public class AutoMapperConfiguration
{
public static void Configure()
{
Mapper.Initialize(x =>
{
x.AddProfile<SystemProfile>();
});
Mapper.Configuration.AssertConfigurationIsValid();
}
}
我的映射器:
public class SystemProfile : Profile
{
public SystemProfile()
{
CreateMap<ElectionViewModel, Election>()
.ForMember(x => x.EmailInstructions, y =>
y.ConvertUsing(new EmailInstructionVariablesCleanerConverter()))
我的ValueConverter
public class EmailInstructionVariablesCleanerConverter : IValueConverter<ElectionViewModel, string>
{
public string Convert(ElectionViewModel source, ResolutionContext context)
{
return CleanVariables(source.EmailInstructions);
}
private static string CleanVariables(string text)
{
return Clean the text here
}
}