将泛型类型传递给非泛型方法

Passing generic type to non generic methods

我想知道当来自通用类型方法时如何混合调用特定类型方法?

我知道给出的例子是多余的。它仅用于说明目的。我想象的最小综合上下文如下:

The method BitConverter.GetBytes has multiple overloads for value-types. But if I were to code a method that goes static byte[] GetBytes<T>(T value) where T : struct that would return BitConverter.GetBytes(value), I get an error message saying that the best overloaded match for has some invalid arguments.

我想这与值是通用类型而不是特定类型有关。

有什么方法可以将泛型类型赋予非泛型方法吗?通过一些施法技巧或其他什么?

暂时不能。 您应该将泛型类型约束为编译器可以缩小到 BitConverter.GetBytes(value) 的重载之一。您用来约束的结构是一个比其中一个重载更大的组,因此它不会让您继续。

对于 c#10,您可能有机会使用该功能 'extend everything',但它尚未发布,现在谈论它没有多大意义。