我有以下课程:
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].X
和Bar[i].Y
。数组的长度将始终相同。使用内置功能的AutoMapper是否可能?理想情况下,我希望避免以编程方式显式地映射每个成员。
作为额外的好处,我还想使用RecognizePostfixes("Postfix")
和以下版本的Foo
在源代码上支持后缀:
class Foo
{
public int XPostfix[];
public int YPostfix[];
public int ZPostfix[];
}
在另一个问题上,使用@LucianBargaoanu的pointer in the right direction和this answer,我得以提出一种使用ITypeConverter
和IEnumerable
扩展方法的解决方案。
这是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。