C# 如何将泛型对象作为方法参数传递

C# 如何将泛型对象作为方法参数传递,c#,generics,C#,Generics,这可能是一个非常基本的问题,但它仍然让我感到困惑(谷歌也无能为力);-)如何将泛型对象作为参数传递给函数 例如,我有一个类CoolGeneric 现在我需要一个方法DoSomethingWithAGeneric(CoolGeneric g)。 在这里,编译器不断抱怨需要一个具体的类型参数。但是该方法应该适用于所有类型参数 我该怎么做?谢谢 简单地说: DoSomethingWithAGeneric<T>(CoolGeneric<T> g) DoSomethingWith

这可能是一个非常基本的问题,但它仍然让我感到困惑(谷歌也无能为力);-)如何将泛型对象作为参数传递给函数

例如,我有一个类
CoolGeneric

现在我需要一个方法
DoSomethingWithAGeneric(CoolGeneric g)
。 在这里,编译器不断抱怨需要一个具体的类型参数。但是该方法应该适用于所有类型参数

我该怎么做?谢谢

简单地说:

DoSomethingWithAGeneric<T>(CoolGeneric<T> g)
DoSomethingWithAGeneric(CoolG)
或者,如果该方法位于声明泛型类型的类中:

class MyClass<T> {

    DoSomethingWithAGeneric(CoolGeneric<T> g)


}
class-MyClass{
DoSomethingWithAGeneric(CoolG)
}
您需要:

DoSomethingWithAGeneric<T>(CoolGeneric<T> g)
(通常)与以下内容相同:

DoSomethingWithAGeneric<int>(foo);
dosomethingwhithageneric(foo);
DoSomethingWithAGeneric<int>(foo);