可以通过显式指定泛型类型来解决。例如:
public class Example
{
public void Method(U u) where U : T
{
// method implementation
}
}
public class Program
{
public static void Main(string[] args)
{
Example example = new Example();
example.Method("Hello, World!");
}
}
在上面的示例中,我们声明了一个泛型类Example,其泛型参数为T。我们还定义了一个方法Method,该方法使用泛型参数U,并使用where从句指定U是T的子类。在Main方法中,我们实例化一个Example对象,并调用Method方法,传递一个字符串作为参数。由于我们将Example的泛型参数指定为string,编译器可以推断出U为string,因此我们无需显式指定泛型类型。