.net 为什么我从FxCop收到InitializeReferenceTypeStaticFieldsInline警告?

.net 为什么我从FxCop收到InitializeReferenceTypeStaticFieldsInline警告?,.net,vb.net,fxcop,.net,Vb.net,Fxcop,我有这个: Public Class Foo Private Shared ReadOnly machineName As String Private Shared ReadOnly userName As String = Environment.UserName Private Shared ReadOnly domainName As String Private Shared ReadOnly events As New List(Of ManualRe

我有这个:

Public Class Foo
    Private Shared ReadOnly machineName As String
    Private Shared ReadOnly userName As String = Environment.UserName
    Private Shared ReadOnly domainName As String
    Private Shared ReadOnly events As New List(Of ManualResetEvent)()

    Shared Sub New()

        AddHandler AppDomain.CurrentDomain.ProcessExit,
            New EventHandler(AddressOf Wait)
        'wait for all tasks to complete before the process terminates

        Try
            machineName = Environment.MachineName
        Catch ex As InvalidOperationException
            machineName = ""
        End Try

        Try
            domainName = Environment.UserDomainName
        Catch ex As InvalidOperationException
            domainName = ""
        Catch ex As PlatformNotSupportedException
            domainName = ""
        End Try

    End Sub
End Class
FxCop让我在这里做什么

CriticalWarning, Certainty 90, for InitializeReferenceTypeStaticFieldsInline
{
    Target       : #.cctor()  (IntrospectionTargetMember)
    Location     : file://blah/blah/blah
    Resolution   : "Initialize all static fields in 'Namespace' 
                   when those fields are declared and remove the explicit 
                   static constructor."
    Help         : http://msdn2.microsoft.com/library/ms182275(VS.90).aspx  (String)
    Category     : Microsoft.Performance  (String)
    CheckId      : CA1810  (String)
    RuleFile     : Performance Rules  (String)
    Info         : "Static fields should be initialized when declared. 
                   Initializing static data in explicit static constructors 
                   results in less performant code."
    Created      : 5/26/2015 9:46:09 PM  (DateTime)
    LastSeen     : 5/26/2015 9:46:09 PM  (DateTime)
    Status       : Active  (MessageStatus)
    Fix Category : NonBreaking  (FixCategories)
}

该规则的详细信息非常明确。简言之:在声明静态共享变量的位置内联初始化所有静态共享变量,就像使用userName一样,并去掉静态构造函数sharednew,因为它可能会更慢

我认为问题不在你发布的代码中。您是否可以包含一些上下文,特别是突出显示该消息的完整代码行?你说你正在初始化一个变量,但没有显示变量的范围或初始化它的位置,这就是消息中暗示的问题…@DanPuzey,哎呀。你说得对。。。。