使用AutoMapper将成员数组转置为具有相应单个成员的对象的集合

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

我有以下课程:

class Foo
{
    public int X[];
    public int Y[];
    public int Z[];
}

class Bar
{
    public int X;
    public int Y;
    public int Z;
}

我希望创建以下AutoMapper映射:

CreateMap<Foo, IEnumerable<Bar>>

这是将单个Foo对象映射到Bar的集合,以便Foo.X[i]Foo.Y[i]将映射到Bar[i].XBar[i].Y。数组的长度将始终相同。使用内置功能的AutoMapper是否可能?理想情况下,我希望避免以编程方式显式地映射每个成员。

作为额外的好处,我还想使用RecognizePostfixes("Postfix")和以下版本的Foo在源代码上支持后缀:

class Foo
{
    public int XPostfix[];
    public int YPostfix[];
    public int ZPostfix[];
}
c# arrays collections automapper transpose
1个回答
0
投票

在另一个问题上,使用@LucianBargaoanu的pointer in the right directionthis answer,我得以提出一种使用ITypeConverterIEnumerable扩展方法的解决方案。

这是ITypeConverter

class TransposeConverter<TSource, TDestination> : ITypeConverter<TSource, IEnumerable<TDestination>> where TDestination : class, new()
{
    public IEnumerable<TDestination> Convert(TSource source, IEnumerable<TDestination> destination, ResolutionContext context)
    {
        // Zip all the member collections from the source object together into a single collection then map to the destination based on the property names.
        return typeof(TSource).GetProperties()
            .Select(p => ((IEnumerable)p.GetValue(source)).Cast<object>().Select(item => (item, p.Name)))
            .Zip(s => context.Mapper.Map<TDestination>(s.ToDictionary(k => k.Name, e => e.item)));
    }
}

这是Zip扩展方法:

public static IEnumerable<TResult> Zip<T, TResult>(this IEnumerable<IEnumerable<T>> collections, Func<IEnumerable<T>, TResult> resultSelector)
{
    var enumerators = collections.Select(s => s.GetEnumerator()).ToArray();
    while (enumerators.All(e => e.MoveNext()))
    {
        yield return resultSelector(enumerators.Select(e => e.Current));
    }
}

但是,这仅解决了问题的第一部分。它无法解决我希望在属性名称上处理后缀的“增加的奖金”部分。为此,我提出了another question

© www.soinside.com 2019 - 2024. All rights reserved.