简体   繁体   中英

Map enum to enum with automapper

I have two enums: one is backend-only, second is versioned and exposed to API consumers.

Backend version:

   public enum SomeEnum  
   {
          One,
          Two,
          Three
   }

API exposed version

   public enum SomeEnum 
   {
          One,
          Two
   }

I'm using Automapper 5.2.0 to map backend enum to api enum like this:

     CreateMap<SomeEnum, CommonEnums.SomeEnum>()

And I need to ignore SomeEnum.Three now. 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.

Why? How can I resolve it?

I have two enums: one is backend-only, second is versioned and exposed to API consumers.

Backend version:

   public enum SomeEnum  
   {
          One,
          Two,
          Three
   }

API exposed version

   public enum SomeEnum 
   {
          One,
          Two
   }

I'm using Automapper 5.2.0 to map backend enum to api enum like this:

     CreateMap<SomeEnum, CommonEnums.SomeEnum>()

And I need to ignore SomeEnum.Three now. 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.

Why? How can I resolve it?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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