我不确定这是否解决了您的问题,但我认为基于C的当前特性,以下可能是最接近您想要的东西:
public interface INavigationService
{
//The implementation of this would simply create an instance
//of something similar to the NavigationServiceStep2 class and return it
INavigationServiceStep2<TArgument> NavigateAsync<TArgument>(TArgument argument)
where TArgument : INavigationArgument;
}
public interface INavigationServiceStep2<TArgument> where TArgument : INavigationArgument
{
Task ForReceiver<TReceiver>() where TReceiver : INavigationReceiver<TArgument>;
}
public class NavigationServiceStep2<TArgument> : INavigationServiceStep2<TArgument>
where TArgument : INavigationArgument
{
private readonly TArgument argument;
public NavigationServiceStep2(TArgument argument)
{
this.argument = argument;
}
public Task ForReceiver<TReceiver>() where TReceiver : INavigationReceiver<TArgument>
{
//real implementation here
}
}
public class SomeConsumer
{
public SomeConsumer()
{
INavigationService service = null;
service.NavigateAsync(new OneParam()).ForReceiver<SomeViewModel>();
}
}
因为C当前不允许您指定某些类型参数并自动推断其他类型参数,所以解决此问题的唯一方法是将单个方法调用转换为两个方法调用。
有一个支持这一点的建议:
https://github.com/dotnet/csharplang/issues/1349