How to initialize a C# static class before it is actually needed?

后端 未结 5 1361
北海茫月
北海茫月 2021-02-05 10:54

I have a static class with a static constructor that takes some time (10-15 seconds) to execute and fully initialize the class. In order to improve performance, I\'ve decided to

5条回答
  •  臣服心动
    2021-02-05 11:22

    I would probably just go for the Initialize method - and it can do something useful:

    • It can log that you're explicitly trying to initialize the class, with a stack trace
    • It might throw an exception if the class is already initialized via another Initialize call
    • You could possibly (with a bit of effort and reorganization) sort things so that any exceptions caused during initialization were propagated without the TypeInitializationException which you'd normally get.

提交回复
热议问题