【发布时间】:2011-02-21 21:22:26
【问题描述】:
我正在尝试使用以下签名编写扩展方法
public static D GetModelFor<S, D>(this S source)
where S : BusinessBase
我有以下课程
public class Order : BusinessBase
我希望能够在 Order 类的实例上调用扩展方法
Order o = new Order();
SomeOtherClass s = o.GetModelFor<SomeOtherClass>();
但这不起作用。 C# 编译器要求我指定 S 和 D 的类型。在本例中为 Order 和 SomeOtherClass。我在这里做错了吗?
======== 更多内部实现细节=========
public static D GetModelFor<S, D>(this S source)
where D : IMappingProvider, new()
{
D d = new D();
d.CreateMap();
return Mapper.Map<S, D>(source);
}
这里的IMappingProvider 是一个接口,它为类提供了一种为自动映射器注册地图的方法。如您所见,我需要输入S 才能在Mapper.Map<> 中使用
【问题讨论】:
标签: c# .net generics extension-methods