整个程序集的静态构造函数
我的程序集中有许多入口点,并且我希望在运行此程序集中的任何其他代码之前,每个 AppDomain 执行一次一些初始化代码。最好的方法是什么?
我看到的一个解决方案是拥有一个带有静态构造函数的类,并继承它所拥有的每个入口点。像这样的事情:
public class Initializer
{
static Initializer()
{
EnsureInitialized(); // Calls initialization code once and only once
}
}
public class EntryPointOne : Initializer, IEntryPoint
{
// Some code here
}
public class EntryPointTwo : Initializer, IEntryPoint
{
// Some code here
}
// etc.
这让我可以避免在每个入口点编写样板静态构造函数,但如果没有多重继承,这并不总是可能的。您还能想到其他更好的选择吗?
I have many entry points in my assembly and I want some initialization code to be executed once per AppDomain prior to running any other code from this assembly. What would be the best way to do it?
One solution I see is to have a class with static constructor and inherit every entry point I have from it. Something like this:
public class Initializer
{
static Initializer()
{
EnsureInitialized(); // Calls initialization code once and only once
}
}
public class EntryPointOne : Initializer, IEntryPoint
{
// Some code here
}
public class EntryPointTwo : Initializer, IEntryPoint
{
// Some code here
}
// etc.
This lets me avoid writing boiler plate static constructors in every entry point but without multi-inheritance this is not always possible. Can you think of any other better options?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
检查C# 中的模块初始值设定项。
Check Module initializers in C#.