4. 避免太多的范型参数
原文引用:
Avoid excessive parameters on generic types
Rule Description The more type parameters a generic type contains, the more difficult it is to know and remember what each type parameter represents. It is usually obvious with one type parameter, as in List<T>, and in certain cases with two type parameters, as in Dictionary<TKey, TValue>. If there are more than two type parameters, the difficulty becomes too great for most users, for example, TooManyTypeParameters<T, K, V> in C# or TooManyTypeParameters(Of T, Of K, Of V) in Visual Basic. How to Fix Violations To fix a violation of this rule, change the design to use no more than two type parameters. When to Exclude Messages Do not exclude a message from this rule unless the design absolutely requires more than two type parameters. Providing generics in a syntax that is easy to understand and use reduces the time that is required to learn and increases the adoption rate of new libraries. Related Rules Collections should implement generic interface Do not declare static members on generic types Do not nest generic types in member signatures Generic methods should provide type parameter Use generic event handler instances Use generics where appropriate See Also |
引起的原因:
一个输出的范型类型拥有多于2个范型参数。
描述:
如果一个范型类拥有太多类型参数,使用它就会变得很困难,你必须了解和记住每一个参数的意义。这样明显不如一个类型参数的范型类(例如:List<T>)或在某些必要的场景中的两个参数的范型类(例如:Dictionary<TKey,TValue>)。如果使用多于两个参数,对于用户来说,使用难度就会变得很大,例如:TooManyTypeParameters<T, K, V>。
修复:
修复这个问题需要修改设计,使用少于两个参数的范型类。
例外:
除非设计必须使用多于两个类型参数的范型类型,尽量避免违反这条规则。编写和设计范型的时候,要尽量让它容易理解和使用,这样可以减少学习的时间,并且还可降低因使用新库带来的开发上的成本。