C# 无法从用法推断类型参数,但这似乎很明显
当我调用C# 无法从用法推断类型参数,但这似乎很明显,c#,C#,当我调用DoSomething时,为什么类型推断在以下情况下不起作用 public class A { } public class B<T> { } public static class Extensions { public static void DoSomething<TWrapper, TInner>(this TWrapper thing) where TWrapper : B<TInne
DoSomething
时,为什么类型推断在以下情况下不起作用
public class A { }
public class B<T> { }
public static class Extensions
{
public static void DoSomething<TWrapper, TInner>(this TWrapper thing)
where TWrapper : B<TInner>
{
}
public static void Test()
{
new B<A>().DoSomething();
}
}
公共类A{}
公共类B{}
公共静态类扩展
{
公共静态无效剂量仪(这是一个TWrapper装置)
其中,说唱歌手:B
{
}
公共静态无效测试()
{
新的B()。DoSomething();
}
}
在我看来,在
测试中方法TWrapper
必须是B
,因此TInner
必须是A
。为什么编译器不能解决这个问题?如果您试图为B
编写扩展,那么它将是:
public class A { }
public class B<T>{}
public static class Extensions
{
public static void DoSomething<T>(this B<T> thing) {}
}
public static void Test()
{
var test = new B<A>();
test.DoSomething();
}
公共类A{}
公共类B{}
公共静态类扩展
{
公共静态无效DoSomething(这个B东西){}
}
公共静态无效测试()
{
var测试=新的B();
试验。剂量测定();
}
@RufusL,因为我将B
类型的对象传入DoSomething。