通过自动映射器将枚举的属性映射到数组中

问题描述 投票:1回答:1

给定是具有索引属性的类

    public class Foo
    {
        public int Bar1 { get; set; } = 17;
        public int Bar2 { get; set; } = 42;
        public int Bar3 { get; set; } = 99;
        ... 
                   Bar<n>
    }

结果是

int列表包含17,42,99 ...

如何配置这样可以使用Automapper的映射器

List<int> bars = mapper.Map<List<int>>(foo); 
c# automapper
1个回答
1
投票

您可以通过Reflection来实现

List<int> bars = new List<int>(foo.GetType()
                                  .GetProperties()
                                  .Where(x => x.PropertyType == typeof(int))
                                  .Select(x => (int)x.GetValue(foo)));
© www.soinside.com 2019 - 2024. All rights reserved.