简体   繁体   English

C #autheatpper检查所有枚举值是否在map中

[英]C# automapper check all enum values are in map

I am mapping from my domain model to a databaseDto. 我正在从我的域模型映射到databaseDto。 Both hold an enum representing a range of states. 两者都有一个代表一系列状态的枚举。

Is there any way of checking all the values in one enum can be mapped with automapper once the mapper has been built from its configuration (or maybe in a unit-test) 有没有办法检查一个枚举中的所有值,一旦映射器从其配置构建(或者可能在单元测试中),就可以使用automapper进行映射

You can use example code below; 您可以使用下面的示例代码;

    public class Program
    {
        public static void Main(string[] args)
        {
            CreateMaps();

            Lender src = new Lender()
            {
                Id = 1,
                Name = "Bob",
                ClaimTypes = ClaimType.A | ClaimType.C
            };

            LenderServiceModel dest = Mapper.Map<LenderServiceModel>(src);

            Console.WriteLine("{0}: {1}", dest.Id, dest.Name);

            foreach(var claimType in dest.ClaimTypes)
            {
                Console.WriteLine(claimType);
            }
        }

        private static void CreateMaps()
        {
            Mapper.CreateMap<Lender, LenderServiceModel>()
                .ForMember(dest => dest.ClaimTypes, opts => opts.MapFrom(src =>
                    src.ClaimTypes.ToString().Split(new string[]{", "}, StringSplitOptions.None).ToList()));
        }
    }

    [Flags]
    public enum ClaimType : int
    {
        A = 1,
        B = 2,
        C = 4
    }

    public class Lender
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public ClaimType ClaimTypes { get; set; }
    }

    public class LenderServiceModel 
    {
        public int Id { get; set; }

        public string Name { get; set; }

        public List<string> ClaimTypes { get; set; }
    }

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

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