【发布时间】:2011-11-19 16:15:45
【问题描述】:
如果您正在为泛型类实现泛型扩展方法,是否有更好的方法?
因为将 func2 完全调用为 func1<V>() 而不是 func2<T, V>() 是很自然的,即省略 T 参数并像 func2<V>() 一样调用它
public class A<T> where T : class {
public V func1<V>() {
//func1 has access to T and V types
}
}
public static class ExtA {
// to implement func1 as extension method 2 generic parameters required
// and we need to duplicate constraint on T
public static V func2<T, V>(this A<T> instance) where T : class {
// func2 has access to V & T
}
}
【问题讨论】:
-
扩展方法仅适用于非通用静态类。
-
@MBen 没有。扩展方法可以在非泛型静态类中声明
标签: c# generics extension-methods