"通过实例访问共享成员,常量成员,枚举成员或嵌套类型"

Pet*_*váč 11 .net vb.net warnings shared instance-variables

我想知道为什么 Visual Studio会提出这个警告:

通过实例访问共享成员,常量成员,枚举成员或嵌套类型

我的代码:

Dim a As ApplicationDeployment = deployment.Application.ApplicationDeployment.CurrentDeployment

If System.Deployment.Application.ApplicationDeployment.IsNetworkDeployed Then
    If a.IsNetworkDeployed Then
        ' do something   
    End If
End If
Run Code Online (Sandbox Code Playgroud)

什么暗示"通过一个实例"?另外,为什么这是一个"警告"?

Zan*_*non 12

显示警告是一种设计选项.在C#中,使用instance(this)关键字调用静态时会抛出错误.

问题是你应该调用对象来正确描述它是什么.

MSDN上更有用的信息.

通过实例变量访问共享成员可以通过模糊成员共享这一事实来使您的代码更难理解.

(......)

纠正此错误

  • 使用定义Shared成员的类或结构的名称来访问它,如以下示例所示.

    Public Class testClass
        Public Shared Sub sayHello()
            MsgBox("Hello")
        End Sub
    End Class
    
    Module testModule
        Public Sub Main()
            ' Access a shared method through an instance variable.
            ' This generates a warning.
            Dim tc As New testClass
            tc.sayHello()
    
            ' Access a shared method by using the class name.
            ' This does not generate a warning.
            testClass.sayHello()
        End Sub
    End Module
    
    Run Code Online (Sandbox Code Playgroud)