C# 泛型类的扩展方法

C# 泛型类的扩展方法,c#,generics,extension-methods,C#,Generics,Extension Methods,可能的重复项: 是否可以为泛型类声明扩展方法 public class NeedsExtension<T> { public NeedsExtension<T> DoSomething(T obj) { // .... } } 公共类需要张力 { 公众需求压力剂量测定法(T obj) { // .... } } ss 公共静态类需要张力 { 公共静态字符串DoSomething(此MyType v) {返回“”;} //或 公

可能的重复项:

是否可以为泛型类声明扩展方法

public class NeedsExtension<T>
{
    public NeedsExtension<T> DoSomething(T obj)
    {
        // ....
    }
}
公共类需要张力
{
公众需求压力剂量测定法(T obj)
{
// ....
}
}
ss

公共静态类需要张力
{
公共静态字符串DoSomething(此MyType v)
{返回“”;}
//或
公共静态无效剂量仪(该型号为v型)
{   
//...
}
}

是的,但是您忘记了
这个
关键字。查看提供集合上所有LINQ运算符的Queryable。

确定

public static void SomeMethod<T>(this NeedsExtension<T> value) {
  ...
}
publicstaticvoidsomemethod(此需要张力值){
...
}
扩展任何类

public static class Extensions
{
    public static T DoSomething<T>(this T obj)
    {
        //...
    }
}
公共静态类扩展
{
公共静态T剂量测量(本T obj)
{
//...
}
}
扩展特定泛型类的步骤

public static NeedExtension<T> DoSomething<T>(this NeedExtension<T> obj)
{
    //...
}
publicstaticneedextensiondosomething(此needextensionobj)
{
//...
}

此代码不会编译扩展方法必须是静态方法,它不能是泛型类型。编译器错误:扩展方法必须在非泛型静态类中定义。这不会编译,因为扩展方法必须在静态非泛型类中。您的类应该是静态的。对于我来说,我忘记了在需要扩展之后的。更具体地说,我想为generic Dictionary类编写一个扩展,所以我需要。
public static NeedExtension<T> DoSomething<T>(this NeedExtension<T> obj)
{
    //...
}