[FxCop.设计规则]15. 不要在范型类中声明静态成员
2005-08-01 20:37 Colin Han 阅读(1992) 评论(6) 编辑 收藏 举报15. 不要在范型类中声明静态成员
翻译概述:
又是一个关于范型的规则。据说微软公司开始很反对将范型引入到C#语言中来,认为范型会严重的影响代码的易理解性。但是范型所提供的强大功能,以及它确确实实提高了开发效率和降低了维护成本。在.NET 2.0中,微软不得不将范型的概念引入到C#中来。
但是范型过于强大的语法能力确确实实影响了代码的可读性,因此微软在C#中对标准C++中的范型语法作了裁减和限制。而且在FxCop中包含很多规则都是为了提高范型的可理解性而制定的。
这条规则中,微软强制性的认为,在范型类中声明静态成员是错误的(虽然C#语法支持这样写)。原因就是这样的写法会影响代码的可读性。
我在.NET 2.0的类库中找到一个比较好的例子,就是.NET 2.0中心提供的一个类型Nullable.
微软为我们提供了两个类型:
System.Nullable<T>
System.Nullable
并且将后一个类型声明为静态类型,其中包含很多对Nullable<T>类型操作的静态方法,从而给用户提供了一种和使用其他非范型类相同的语法环境去使用这些成员。
引起的原因:
一个公共范型类包含了静态成员。
描述:
当我们需要访问一个范型类的静态成员时,必须指定范型参数。同时,当调用一个不能自动推断出范型参数的范型方法时,同样需要明确的制定范型参数。这两种成员的访问语法是不同的,但是又非常接近,很容易对用户造成困扰。
下面的代码演示如何使用这两种方法:
[C#]






[VB]






一般来说,这两种声明方式都应该避免使用。应该不需要再调用一个方法时明确制定类型参数。一般情况下,应该保持范型类型的方法或范型方法的调用尽量使用和普通方法调用相同的语法。更多的内容可以参考:Generic methods should provide type parameter
修复:
删除这个静态成员,或者将它们修改成实例成员。
例外:
不要忽略这条规则。一般来说,提供范型给用户应该降低学习和使用的复杂度,从而提高用户的使用率。
原文引用:
Do not declare static members on generic types
Rule Description When a static member of a generic type is called, the type argument must be specified for the type. When a generic instance member that does not support inference is called, the type argument must be specified for the member. The syntax for specifying the type argument in these two cases is different and easily confused as the following calls demonstrate: [C#] ![]() ![]() ![]() ![]() ![]() ![]() [VB]
![]() ![]() ![]() ![]() ![]() ![]() Generally, both of the prior declarations should be avoided so that the type argument does not have to be specified when the member is called. This results in a syntax for calling members in generics that is no different from the syntax for non-generics. For more information, see Generic methods should provide type parameter. How to Fix Violations To fix a violation of this rule, remove the static member or change it to an instance member. When to Exclude Messages Do not exclude a message from this rule. 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 Avoid excessive parameters on generic types Collections should implement generic interface 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 |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix