我正在尝试实现两个泛型接口。ITwoWayMapper<T,U>
同时实现IOneWayMapper<T,U>
和IOneWayMapper<U,T>
是有意义的。如果我尝试这样做:
public interface IOneWayMapper<T, U>
{
U Map(T source);
}
public interface ITwoWayMapper<T, U> :
IOneWayMapper<T, U>,
IOneWayMapper<U, T>
{
TTwo Map(TOne source);
TOne Map(TTwo source);
}
我遇到了一个错误:
Interface ITwoWayMapper<T,U> cannot implement both IOneWayMapper<T,U> and IOneWayMapper<U,T> because they may unify for some type parameter substitutions
。我认为这是有道理的,因为它不能确定哪个接口被满足。
我的问题是:是否可能使用类型约束来表达类似以下的内容?:
public interface ITwoWayMapper<T, U> :
IOneWayMapper<T, U>,
IOneWayMapper<U, T>
where T: !U
{
TTwo Map(TOne source);
TOne Map(TTwo source);
}