简体   繁体   English

使用 automapper 将枚举映射到枚举

[英]Map enum to enum with automapper

I have two enums: one is backend-only, second is versioned and exposed to API consumers.我有两个枚举:一个是仅后端,第二个是版本化并公开给 API 使用者。

Backend version:后端版本:

   public enum SomeEnum  
   {
          One,
          Two,
          Three
   }

API exposed version API公开版本

   public enum SomeEnum 
   {
          One,
          Two
   }

I'm using Automapper 5.2.0 to map backend enum to api enum like this:我正在使用 Automapper 5.2.0 将后端枚举映射到 api 枚举,如下所示:

     CreateMap<SomeEnum, CommonEnums.SomeEnum>()

And I need to ignore SomeEnum.Three now.我现在需要忽略 SomeEnum.Three。 So I changed code like:所以我改变了代码,如:

    CreateMap<SomeEnum, CommonEnums.SomeEnum>()
          .ForMember(t => t == CommonEnums.SomeEnum.Three, p => p.Ignore());

And now I have error:现在我有错误:

AutoMapper.AutoMapperConfigurationException : Custom configuration for members is only supported for top-level individual members on a type. AutoMapper.AutoMapperConfigurationException :成员的自定义配置仅支持类型上的顶级个人成员。

Why?为什么? How can I resolve it?我该如何解决?

I have two enums: one is backend-only, second is versioned and exposed to API consumers.我有两个枚举:一个是仅后端的,第二个是版本控制的,并向API使用者公开。

Backend version:后端版本:

   public enum SomeEnum  
   {
          One,
          Two,
          Three
   }

API exposed version API公开版本

   public enum SomeEnum 
   {
          One,
          Two
   }

I'm using Automapper 5.2.0 to map backend enum to api enum like this:我正在使用Automapper 5.2.0将后端枚举映射到api枚举,如下所示:

     CreateMap<SomeEnum, CommonEnums.SomeEnum>()

And I need to ignore SomeEnum.Three now.我需要忽略SomeEnum.Three。 So I changed code like:所以我将代码更改为:

    CreateMap<SomeEnum, CommonEnums.SomeEnum>()
          .ForMember(t => t == CommonEnums.SomeEnum.Three, p => p.Ignore());

And now I have error:现在我有错误:

AutoMapper.AutoMapperConfigurationException : Custom configuration for members is only supported for top-level individual members on a type. AutoMapper.AutoMapperConfigurationException:仅类型的顶级单个成员支持成员的自定义配置。

Why?为什么? How can I resolve it?我该如何解决?

所以基本上你可以简单地忽略那个新值和映射就可以了

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

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