实际需要之前如何初始化一个C#静态类? [英] How to initialize a C# static class before it is actually needed?

查看:873
本文介绍了实际需要之前如何初始化一个C#静态类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个静态构造函数,需要一些时间(10-15秒)来执行,充分初始化类的静态类。为了提高性能,我已经决定启用该静态类被显式初始化时,首先需要它,以便它是准备去的时候,它实际上需要被用于代替。

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 enable this static class to be explicitly initialized instead of when it is first needed so that it is ready to go when it actually needs to be used.

我首先想到的是创建一个初始化()的类方法,但因为我已经有一个静态构造函数,这种方法似乎没有不需要做的是方法来调用明确初始化类,而无需访问它的任何其他公共方法或属性的任何其他。有没有什么直接似乎没有我的权利的方法。

My first thought was to create an Initialize() method for the class, but since I already have a static constructor, this method doesn't seem to need to do anything other than be the method to call to explicitly initialize the class without accessing any of its other public methods or properties. Having a method that does nothing directly doesn't seem right to me.

后来我想我可以从静态构造函数移动代码到这个初始化()方法,但我也很喜欢,首先需要的时候进行初始化类和初始化()方法WASN :T明确要求

Then I thought I can just move the code from the static constructor into this Initialize() method, but I'd also like the class to be initialized when it is first needed and the Initialize() method wasn't explicitly called.

要总结,我要遵守下列标准:

To summarize, I want the following criteria to be observed:


  • 我想允许(通过使用公共初始化()方法可能)要明确初始化静态类。

  • 我不希望有访问类的任何其他公共方法或属性时,我不需要他们,尽管这会初始化静态类。

  • 如果班里有没有明确初始化,我还是想初始化时,首先需要的时候(即访问与使用它们提供的功能或数据的意图,其他公共方法或属性时)。

  • 这是一个辅助类和使用Singleton设计模式是没有必要的,我的目的。

  • I want to allow the static class to be explicitly initialized (likely by using a public Initialize() method).
  • I don't want to have to access any other public methods or properties on the class when I don't need them, even though this would initialize the static class.
  • If the class has not been explicitly initialized, I still want to initialize it when it is first needed (i.e. when accessing other public methods or properties with the intent of using the functionality or data they provide).
  • This is a helper class and using the Singleton design pattern is not necessary for my purposes.

什么是遵守上述的正确方法对于静态类标准的C#编写的?这也适用于其他编程语言(如Java的),但我在C#编写的一个解决方案,个人兴趣。

What would be the proper way to observe the above criteria for a static class written in C#? This can also apply to other programming languages (e.g. Java), but I'm personally interested in a solution written in C#.

推荐答案

我可能只是去为初始化法 - 它的可以的做些有用的事情:

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


  • 它可以记录你正在试图明确初始化类,具有堆栈跟踪

  • 这可能如果类是抛出一个异常的的通过另一个初始化调用初始化

  • 您可以的可能的(有一点点努力和重组),以便在初始化过程中造成的任何异常都没有 TypeInitializationException 其中传播你通常会得到整理的东西。

  • 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.

这篇关于实际需要之前如何初始化一个C#静态类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆