如果你正在为泛型类实现通用扩展方法,是否有更好的方法呢?
因为自然而然地会像这样调用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
}
}