自动映射器。在CreateMap

时间:2018-10-24 09:44:51

标签: c# mapping automapper

我有以下映射:

CreateMap<Entity, EntityResponse>(); // #1

CreateMap<Source<Entity>, Dest<EntityResponse>>()
    .ConvertUsing(source =>
    {
        // here I need to use mapping from line 1#
        EntityResponse resp = <mapping>; 
        var tmp = new Source<Entity>(resp);
        return new Dest<EntityResponse>(tmp);
    });

Dest<T>包含构造函数Dest<T>(Source<T>)

我需要在第二个映射中使用第一个映射(第1行),但是我不知道该怎么做。

1 个答案:

答案 0 :(得分:0)

您不需要struct MetaArray<M: Meta>: Codable, ExpressibleByArrayLiteral { let array: [M.Element] init(array: [M.Element]) { self.array = array } init(arrayLiteral elements: M.Element...) { self.array = elements } struct ElementKey: CodingKey { var stringValue: String init?(stringValue: String) { self.stringValue = stringValue } var intValue: Int? { return nil } init?(intValue: Int) { return nil } } init(from decoder: Decoder) throws { var container = try decoder.unkeyedContainer() var elements: [M.Element] = [] while !container.isAtEnd { let nested = try container.nestedContainer(keyedBy: ElementKey.self) guard let key = nested.allKeys.first else { continue } let metatype = M(rawValue: key.stringValue) let superDecoder = try nested.superDecoder(forKey: key) let object = try metatype?.type.init(from: superDecoder) if let element = object as? M.Element { elements.append(element) } } array = elements } func encode(to encoder: Encoder) throws { var container = encoder.unkeyedContainer() try array.forEach { object in var nested = container.nestedContainer(keyedBy: ElementKey.self) let metatype = M.metatype(for: object) if let key = ElementKey(stringValue: metatype.rawValue) { let superEncoder = nested.superEncoder(forKey: key) let encodable = object as? Encodable try encodable?.encode(to: superEncoder) } } } }

CreateMap<Source<Entity>, Dest<EntityResponse>>()