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

后端 未结 5 1362
北海茫月
北海茫月 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:09

    I'm not sure if you can specify when a static constructor is loaded.

    From MSDN "A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced."

    http://msdn.microsoft.com/en-us/library/k9x6w0hc(v=vs.80).aspx

    *EDIT: * Would adding the singleton pattern help here? The getter can call Initialize() by checking a flag in the class, IsLoaded=true. Subsequent calls will not call Initialize()

提交回复
热议问题