• [FxCop.设计规则]15. 不要在范型类中声明静态成员


    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#]

    // Static method in a generic type.
    GenericType<int>.StaticMethod();

    // Generic instance method that does not support inference.
    someObject.GenericMethod<int>();

    [VB]

    ' Shared method in a generic type.
    GenericType(Of Integer).SharedMethod()

    ' Generic instance method that does not support inference.
    someObject.GenericMethod(Of Integer)()

    一般来说,这两种声明方式都应该避免使用。应该不需要再调用一个方法时明确制定类型参数。一般情况下,应该保持范型类型的方法或范型方法的调用尽量使用和普通方法调用相同的语法。更多的内容可以参考:Generic methods should provide type parameter

    修复:

    删除这个静态成员,或者将它们修改成实例成员。

    例外:

    不要忽略这条规则。一般来说,提供范型给用户应该降低学习和使用的复杂度,从而提高用户的使用率。

    原文引用:

    Do not declare static members on generic types

    TypeName:

    DoNotDeclareStaticMembersOnGenericTypes

    CheckId:

    CA1000

    Category:

    Microsoft.Design

    Message Level:

    Error

    Certainty:

    95%

    Breaking Change:

    Breaking


    Cause: An externally visible generic type contains a static (Shared in Visual Basic) member.

    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#]

    // Static method in a generic type.
    GenericType<int>.StaticMethod();

    // Generic instance method that does not support inference.
    someObject.GenericMethod<int>();

    [VB]

     

    ' Shared method in a generic type.
    GenericType(Of Integer).SharedMethod()

    ' Generic instance method that does not support inference.
    someObject.GenericMethod(Of Integer)()

    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 expose generic lists

    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

    Generics DesignGuidelines

     

  • 相关阅读:
    A CIRCULAR PROGRESSBAR STYLE USING AN ATTACHED VIEWMODEL
    First MarkDown Blog
    Folder Recursion with C#
    39. Volume Rendering Techniques
    Service Station
    WPF Wonders: Transformations (and Robots!)
    Flipping elements with WPF
    FoLlow 的技术博客
    利用索引降低并发事务引起的锁【转】
    企业级应用架构模型-并发,事务,锁
  • 原文地址:https://www.cnblogs.com/Cajon/p/205042.html
Copyright © 2020-2023  润新知