Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法从用法推断类型参数,但这似乎很明显_C# - Fatal编程技术网

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。