Initialize reference type static fields inline
来源:互联网 发布:mysql union all 排序 编辑:程序博客网 时间:2024/06/02 19:55
TypeName
InitializeReferenceTypeStaticFieldsInline
CheckId
CA1810
Category
Microsoft.Performance
Breaking Change
NonBreaking
A reference type declares an explicit static constructor.
When a type declares an explicit static constructor, the just-in-time (JIT) compiler adds a check to each of the type's static methods and instance constructors to ensure that the static constructor was previously called. Static initialization is triggered when any static member is accessed or when an instance of the type is created. However, static initialization is not triggered if you declare a variable of the type but do not use it, which can be important if the initialization changes global state.
When all static data is initialized inline and an explicit static constructor is not declared, Microsoft intermediate language (MSIL) compilers add the beforefieldinit flag and an implicit static constructor, which initializes the static data, to the MSIL type definition. When the JIT compiler encounters the beforefieldinit flag, in most cases the static constructor checks are not added. Static initialization is guaranteed to occur at some time before any of the static fields are accessed but not before a static method or instance constructor is invoked. Note that static initialization can occur at any time after a variable of the type is declared.
Static constructor checks can reduce performance. Often a static constructor is used only to initialize static fields, in which case it is only necessary to ensure that static initialization occurs before the first access of a static field. The beforefieldinit behavior is appropriate for these and most other types. It is only inappropriate when static initialization affects global state and one of the following is true:
-
The affect on global state is expensive and is not needed if the type is not used.
-
The global state effects can be accessed without accessing any static fields of the type.
To fix a violation of this rule, initialize all static data when it is declared and remove the static constructor.
It is safe to exclude a warning from this rule if performance is not a concern; or if global state changes due to static initialization are expensive, or must be guaranteed to occur before a static method of the type is called or an instance of the type is created.
The following example shows a type, StaticConstructor, that violates the rule and a type, NoStaticConstructor, that replaces the static constructor with inline initialization to satisfy the rule.
Imports SystemImports System.ResourcesNamespace PerformanceLibrary Public Class StaticConstructor Shared someInteger As Integer Shared resourceString As String Shared Sub New() someInteger = 3 Dim stringManager As New ResourceManager("strings", _ System.Reflection.Assembly.GetExecutingAssembly()) resourceString = stringManager.GetString("string") End Sub End Class Public Class NoStaticConstructor Shared someInteger As Integer = 3 Shared resourceString As String = InitializeResourceString() Shared Private Function InitializeResourceString() Dim stringManager As New ResourceManager("strings", _ System.Reflection.Assembly.GetExecutingAssembly()) Return stringManager.GetString("string") End Function End ClassEnd Namespace
using System;using System.Reflection;using System.Resources;namespace PerformanceLibrary{ public class StaticConstructor { static int someInteger; static string resourceString; static StaticConstructor() { someInteger = 3; ResourceManager stringManager = new ResourceManager("strings", Assembly.GetExecutingAssembly()); resourceString = stringManager.GetString("string"); } } public class NoStaticConstructor { static int someInteger = 3; static string resourceString = InitializeResourceString(); static string InitializeResourceString() { ResourceManager stringManager = new ResourceManager("strings", Assembly.GetExecutingAssembly()); return stringManager.GetString("string"); } }}
Note the addition of the beforefieldinit flag on the MSIL definition for the NoStaticConstructor class.
Output
.class public auto ansi StaticConstructor extends [mscorlib]System.Object{} // end of class StaticConstructor.class public auto ansi beforefieldinit NoStaticConstructor extends [mscorlib]System.Object{} // end of class NoStaticConstructor
Initialize value type static fields inline
- Initialize reference type static fields inline
- Advanced searching - fields reference
- static inline
- static inline
- inline;static
- static inline
- static inline
- static inline
- static inline
- static inline
- static inline
- static、static inline、entern inline
- static和static inline
- Cannot make a static reference to the non-static method setInAnimation(Animation) from the type View
- 泛型类与静态泛型方法 Cannot make a static reference to the non-static type
- Cannot make a static reference to the non-static method getLocalActivityManager() from the type Acti
- qemu-type-object-initialize
- static inline 和 extern inline
- [转]Linux入侵检查
- 多个文件上传
- 常用的eclipse插件
- 精
- 中层管理技能
- Initialize reference type static fields inline
- 谈非模态对话框的销毁
- 有关XML的编码问题
- 我的USB设备有问题
- 我就不信CSDN没有高人能做,悬赏5K求算法
- 住在我隔壁储藏室的大学刚毕业的小夫妻(上)
- 获取快捷方式的文件路径
- ArcEngine预定义的命令说明(9.1版本全集)
- initgraph:初始化图形系统