通用类的扩展方法
可能重复:
C# – 通用扩展方法
你如何为一般类型的类编写C#扩展方法
是否可以为generics类声明扩展方法?
public class NeedsExtension { public NeedsExtension DoSomething(T obj) { // .... } }
扩展任何类
public static class Extensions { public static T DoSomething(this T obj) { //... } }
扩展特定的generics类
public static NeedExtension DoSomething (this NeedExtension obj) { //... }
是的,但你忘记了this
关键字。 查看Queryable,它提供集合上的所有LINQ运算符。
当然
public static void SomeMethod(this NeedsExtension value) { ... }
你如何为一般类型的Clas编写C#扩展方法
public static class NeedsExtension { public static string DoSomething (this MyType v) { return ""; } // OR public static void DoSomething (this MyType v) { //... } }