【发布时间】:2022-01-20 16:01:08
【问题描述】:
假设我有两个类 A 和 ADto。 A 继承自 ABase,ADto 继承自 ADtoBase。
我有两种扩展方法可以手动将 ABase 映射到 ADtoBase,反之亦然。 当我想编写一个扩展方法来将 A 映射到 ADto 时,有没有办法重用基本对话以避免重复?即,我不想为基类中存在的字段正确映射并为此重用基本映射器。
例子:
Class ABase:
+ string Id;
Class ADtoBase:
+ string Id;
Class A: ABase:
+ string Name;
Class ADto:ADtoBase
+ string Name
public static ADto ToADto(this ADtoBase)
{
// I somehow want to use the base mapper (extension) so that I don't have to map Id again and only map Name.
}
【问题讨论】:
-
如果你用实际的类定义而不是准 UML(或你想在那里做的任何事情)来呈现它会更好。
标签: c# inheritance mapping extension-methods dto