我有一堂课,看起来像下面这样:

Public Class Utilities
    Public Shared Function blah(userCode As String) As String
        'doing some stuff
    End Function
End Class


我在上面运行FxCop 10,它说:

"Because type 'Utilities' contains only 'static' (
'Shared' in Visual Basic) members, add a default private
constructor to prevent the compiler from adding a default
public constructor."


好的,您是对的FxCop先生,我将添加一个私有构造函数:

Private Utilities()


现在我有:

"It appears that field 'Utilities.Utilities' is
never used or is only ever assigned to. Use this field
or remove it."


我应该怎么做才能摆脱两种警告?

最佳答案

在C#中,可以通过将类标记为静态来解决此问题,例如

public static class Utilities
{
   ...
}


静态类只能包含静态(在VB共享中)成员。

我相信VB.NET中的等效项是使用模块。

请参见Marking A Class Static in VB.NET

10-08 14:05