How can I run a static constructor?

前端 未结 10 1343
面向向阳花
面向向阳花 2020-12-08 02:15

I\'d like to execute the static constructor of a class (i.e. I want to \"load\" the class) without creating an instance. How do I do that?

Bonus question: Are there

10条回答
  •  借酒劲吻你
    2020-12-08 02:54

    As others have said, static constructors run automatically. If you need to be explicit, maybe you should refactor it into a static method which you can run explicitly?

    Explicitly calling a static method would also, of course, ensure that the static constructor had been executed.

    edit

    Static constructors are run when any static members are referenced. You could simply create a dummy method called initialize which did nothing but ensure that the framework calls the static constructor.

提交回复
热议问题