Пишу fluent интерфейс для конфигурации.
Проблема в выводе типов метода Action2, хотя типы в Action, Action3 и SomeCfgAction2 выводятся прекрасно.
public class Cfg<T>
{
public void SomeCfgAction(Func<T> arg, string s)
{
}
public void SomeCfgAction2<T2>(Func<T, T2> arg, string s)
{
}
}
public static class FluentCfgExts
{
public static TCfg Action<TCfg, T>(this TCfg cfg, Func<T> arg, string s)
where TCfg: Cfg<T>
{
return cfg;
}
public static TCfg Action2<TCfg, T, T2>(this TCfg cfg, Func<T, T2> arg, string s)
where TCfg: Cfg<T>
{
return cfg;
}
public static Cfg<T> Action3<T, T2>(this Cfg<T> cfg, Func<T, T2> arg, string s)
{
return cfg;
}
static void Main(string[] args)
{
var cfg = new Cfg<int>();
cfg.Action(() => 1, ""); // ok
cfg.Action2(i => 1.0, ""); // The type arguments for method cannot be inferred from the usage
cfg.Action3(i => 1.0, ""); // ok
cfg.SomeCfgAction2(i => 1.0, ""); // ok
}
}
public class CfgExt<T> : Cfg<T>
{
public void SomeExtCfgAction(T arg)
{
}
}
Как написать метод Action2 чтобы он работал для Cfg и CfgExt и возвращал соответственно их же?