我正在尝试将对象的成员从短转换为?通过使用解析器界面的AutoMapper CreateMap ..来解决问题。
我创建了一个IMemberValueResolver接口,使用一个resolve函数对该接口进行类化,然后尝试将CreateMap与opt.MapFrom和opt.ResolveUsing结合使用-无效。.
接口和类编译没有错误,但只有CreateMap错误。
尝试了以下文档:
http://docs.automapper.org/en/stable/Custom-value-resolvers.html
http://docs.automapper.org/en/stable/5.0-Upgrade-Guide.html
现在看了两天,然后尝试了所有我能找到的东西,没有任何效果..:(
这就是我现在拥有的..
接口(IMemberValueResolver.cs):
public interface IMemberValueResolver<in TSource, in TDestination, TSourceMember, TMember>
{
TMember Resolve(TSource source, TDestination destination, TSourceMember sourceMember, TMember destinationMember, ResolutionContext context);
}
映射器配置(AutoMapperConfig.cs):
public class IsBootstrapResolver : IMemberValueResolver<PageTemplateDataContract, PageTemplateViewModel, short?, bool>
{
public bool Resolve(PageTemplateDataContract source, PageTemplateViewModel destination, short? sourceMember, bool destMember, ResolutionContext context)
{
return sourceMember == 1 ? true : false;
}
}
//NONE OF THESE WORK!!!!!
public class DataContractToViewModelProfile : Profile
{
public DataContractToViewModelProfile()
{
CreateMap<PageTemplateDataContract, PageTemplateViewModel>().ForMember(dest => dest.IsBootstrapEnable, opt => opt.ResolveUsing<IsBootstrapResolver>().FromMember(x => x.IsBootstrapEnable));
CreateMap<PageTemplateDataContract, PageTemplateViewModel>().ForMember(dest => dest.IsBootstrapEnable, opt => opt.ResolveUsing(IsBootstrapResolver));
CreateMap<PageTemplateDataContract, PageTemplateViewModel>().ForMember(dest => dest.IsBootstrapEnable, opt => opt.MapFrom<IsBootstrapResolver, short?>(!?!?!?!));
CreateMap<PageTemplateDataContract, PageTemplateViewModel>().ForMember(dest => dest.IsBootstrapEnable, opt => opt.MapFrom(new IsBootstrapResolver()));
CreateMap<PageTemplateDataContract, PageTemplateViewModel>().ForMember(dest => dest.IsBootstrapEnable, opt => opt.ResolveUsing<IsBootstrapResolver>().FromMember(x => x.IsBootstrapEnable));
CreateMap<PageTemplateDataContract, PageTemplateViewModel>().ForMember(dest => dest.IsBootstrapEnable, opt => opt.ResolveUsing<IsBootstrapResolver, bool>(src => src.IsBootstrapEnable));
}
}
我希望对象映射成短?使用自定义解析器将其转换为bool。
谢谢!