AutoMapper 对象到 ICollection<object>

AutoMapper object to ICollection<object>

我有一个 class,它是在一行中定义的 id 值类型。像这样:

public class PropertiesModel
{
    public decimal property1 { get; set; }
    public decimal property2 { get; set; }
    ...
    public decimal propertyz { get; set; }
}

我想将它映射到一个 Id 值数组,如下所示,创建这个 class 的简单集合:

public class IdValue
{
    public string Id { get; set; }
    public decimal Value { get; set; }
}

结果值应该是这样的对象:

IdValue[] exampleResult = new IdValue[] 
{
    new IdValue {
       Id = property1, // THe name of the first field in Property model
       Value = PropertiesModel.property1 // The actual value of the property 1 on the model
    },
    new IdValue {
       Id = property2, // THe name of the second field in Property model
       Value = PropertiesModel.property2 // The actual value of the property 2 on the model
    },
    ...
    new IdValue {
       Id = propertyz, // THe name of the z-field in Property model
       Value = PropertiesModel.propertyz // The actual value of the property z on the model
    }
}

我一直在尝试使用 AutoMapper 执行此操作:

CreateMap<PropertiesModel, ICollection<IdValue>>()
    .ForMember(x => x,
        y => y.MapFrom(z => new IdValue
        {
            Id = "Property 1",
            Value = z.property1
         }))
    .ForMember(x => x,
        y => y.MapFrom(z => new IdValue
        {
            Id = "Property 2",
            Value = z.property2
         }))
     ...
    .ForMember(x => x,
        y => y.MapFrom(z => new IdValue
        {
            Id = "Property Z",
            Value = z.propertyz
         }))

但这行不通,可以这样做自动映射器吗?我确定我在这里遗漏了一些东西。已尝试阅读文档,但没有找到与我正在尝试做的类似的示例。

一种方法是使用 ConvertUsing

CreateMap<PropertiesModel, ICollection<IdValue>>()
    .ConvertUsing(obj => obj.GetType()
                        .GetProperties()
                        .Select(prop => new IdValue
                          {
                              Id = prop.Name,
                              Value = (decimal)prop.GetValue(obj)
                          })
                        .ToArray());