AutoMapper 8 ConvertUsing(复杂,字符串)

时间:2018-12-08 23:25:26

标签: c# automapper valueconverter

我正在尝试在映射过程中清除字符串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
    }
}

0 个答案:

没有答案