简体   繁体   English

使用 Automapper 将方法映射到字段

[英]Map a method to a field with Automapper

I want to use Automapper and need to map a method to the field(such as command part).我想使用 Automapper 并且需要将方法映射到字段(例如命令部分)。 How can I do it?我该怎么做? This my first Code这是我的第一个代码


     foreach(source item in sources)
      {

     ss.ServerStatusType = ServerStatusTypeName(item);
     ss.A = item.A ;
     ss.B =item.B;
     destinations.Add(dto);
}

I want Use Automapper and I just have problem with ServerStatusTypeName method我想使用 Automapper,但我对 ServerStatusTypeName 方法有问题


 var config = new MapperConfiguration(cfg =>   
     cfg.CreateMap<IList<source >, IList<Destination>>());

IList<Source> statuses = context.Sp_ServerUpdateStatus(userName).ToList();

            var mapper = new Mapper(config);
            IList<Source> dto = mapper.Map<IList<Destination>>(statuses);

Everything is ok just ss.ServerStatusType is null because I use a method to fill this item一切正常,只是 ss.ServerStatusType 为 null 因为我使用一种方法来填充此项目

You could try你可以试试

class YourObjectProfile : Profile
    {
        public YourObjectProfile()
        {
            CreateMap<UI_Object, Domain_Object>()
                .ForMember(c => c.name, p => p.MapFrom(dbc => dbc.name));

            CreateMap<Domain_Object, UI_Object>()
                .ForMember(dbc => dbc.name, dbp => dbp.MapFrom(c => c.name));
        }   
    }

I use this code and it is ok我使用此代码,没问题

 CreateMap<Sp_ServerUpdateStatus_Result, ServerStatus>()
            .ForMember(dest=>dest.ServerStatusType, opt => opt.MapFrom(src=> ServerStatusTypeName(src))
                );

... ServerStatusTypeName(src) is a method ... ServerStatusTypeName(src) 是一种方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM