Forcing class load

前端 未结 2 640
清歌不尽
清歌不尽 2021-02-13 21:36

Is there a way in C# or .net IL to force a class that has a type initializer (static constructor) to load itself, without accessing any of its parameters?

Assuming I\'ve

2条回答
  •  梦毁少年i
    2021-02-13 22:19

    Rummaging in the CLI spec, I found a reference to the method RuntimeHelpers.RunClassConstructor

    If a language wishes to provide more rigid behavior — e.g., type initialization automatically triggers execution of base class’s initializers, in a top-to-bottom order — then it can do so by either:

    • defining hidden static fields and code in each class constructor that touches the hidden static field of its base class and/or interfaces it implements, or
    • by making explicit calls to System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor (see Partition IV).

提交回复
热议问题