我遇到了通用扩展的问题。我有简单的IMapping
接口,所有映射都实现了这个接口。我想简化测试的模拟,所以我已经为它创建了扩展。但问题是我得到The type arguments for method cannot be inferred from the usage.
错误。我可以明确指定类型(然后它可以工作),但它不应该是必要的,因为恕我直言我可以从上下文推断(因为where
约束)。我错过了什么吗?
public static void SetupRandomMapping<TMapping, TSource, TDestination>(this Mock<TMapping> mock)
where TMapping : class, IMapping<TSource, TDestination>
{
// next line does not work
mock.SetupRandomMapping(It.IsAny<TSource>(), out var dummy);
}
public static void SetupRandomMapping<TMapping, TSource, TDestination>(this Mock<TMapping> mock, TSource source, out TDestination destination)
where TMapping : class, IMapping<TSource, TDestination>
{
// some code ...
}
IMapping
看起来像这样:
public interface IMapping<in TSource, out TDestination>
{
TDestination Map(TSource src);
}