2015-05-26 3 views
0

у меня есть это:Почему я получаю предупреждение InitializeReferenceTypeStaticFieldsInline от 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 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) 
} 
+0

Я думаю, что проблема не в коде, который вы опубликовали. Можете ли вы включить какой-то контекст (и, в частности, выделить всю строку кода, для которой поднято сообщение)? Вы говорите, что инициализируете переменную, но не показываете область видимости или где вы ее инициализируете, и это то, о чем сообщение ссылается как проблема ... –

+0

@DanPuzey, whoops. Ты прав.... –

ответ

0

Подробное сообщение правила довольно четко. Короче: инициализируйте все свои статические (Shared) переменные inline, где они объявлены (например, вы используете с userName), и избавитесь от статического конструктора (Shared New), потому что это может быть медленнее.

Смежные вопросы