Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 带ValueFormatter的自动映射器_C#_Automapper_Automapper 2 - Fatal编程技术网

C# 带ValueFormatter的自动映射器

C# 带ValueFormatter的自动映射器,c#,automapper,automapper-2,C#,Automapper,Automapper 2,我正在学习如何使用AutoMapper,但在使用ValueFormatter时遇到了问题 下面是控制台中的一个简单示例,我无法将其与NameFormatter一起使用: class Program { static void Main(string[] args) { Mapper.Initialize(x => x.AddProfile<ExampleProfile>()); var person = new Person {

我正在学习如何使用AutoMapper,但在使用ValueFormatter时遇到了问题

下面是控制台中的一个简单示例,我无法将其与NameFormatter一起使用:

class Program
{
    static void Main(string[] args)
    {
        Mapper.Initialize(x => x.AddProfile<ExampleProfile>());

        var person = new Person {FirstName = "John", LastName = "Smith"};

        PersonView oV = Mapper.Map<Person, PersonView>(person);

        Console.WriteLine(oV.Name);

        Console.ReadLine();
    }
}

public class ExampleProfile : Profile
{
    protected override void Configure()
    {
        //works:
        //CreateMap<Person, PersonView>()
        //    .ForMember(personView => personView.Name, ex => ex.MapFrom(
        //        person => person.FirstName + " " + person.LastName));

        //doesn't work:
        CreateMap<Person, PersonView>()
            .ForMember(personView => personView.Name, 
             person => person.AddFormatter<NameFormatter>());
    }
}

public class NameFormatter : ValueFormatter<Person>
{
    protected override string FormatValueCore(Person value)
    {
        return value.FirstName + " " + value.LastName;
    }
}

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

public class PersonView
{
    public string Name { get; set; }
}
类程序
{
静态void Main(字符串[]参数)
{
初始化(x=>x.AddProfile());
var person=newperson{FirstName=“John”,LastName=“Smith”};
PersonView oV=Mapper.Map(person);
控制台写入线(oV.Name);
Console.ReadLine();
}
}
公共类示例概要文件:概要文件
{
受保护的覆盖无效配置()
{
//作品:
//CreateMap()
//.ForMember(personView=>personView.Name,ex=>ex.MapFrom(
//person=>person.FirstName+“”+person.LastName));
//不起作用:
CreateMap()
.FormMember(personView=>personView.Name,
person=>person.AddFormatter());
}
}
公共类名称格式化程序:ValueFormatter
{
受保护的覆盖字符串FormatValueCore(个人值)
{
返回value.FirstName+“”+value.LastName;
}
}
公共阶层人士
{
公共字符串名{get;set;}
公共字符串LastName{get;set;}
}
公共类PersonView
{
公共字符串名称{get;set;}
}

我错过了什么?AutoMapper是2.2.1版

您应该使用
值解析器
(更多信息):


显然,值格式化程序一直是一个难题。

谢谢,这是可行的,但显而易见的问题是,为什么使用解析器而不是格式化程序?再次感谢。显然,我应该用最近的书来学习;)
public class PersonNameResolver : ValueResolver<Person, string>
{
    protected override string ResolveCore(Person value)
    {
        return (value == null ? string.Empty : value.FirstName + " " + value.LastName);
    }
}
public class ExampleProfile : Profile
{
    protected override void Configure()
    {
        CreateMap<Person, PersonView>()
            .ForMember(personView => personView.Name, person => person.ResolveUsing<PersonNameResolver>());
    }
}
 CreateMap<Person, PersonView>()
      .ForMember(personView => personView.Name, ex => ex.MapFrom(
           person => person.FirstName + " " + person.LastName));