我正在尝试找出一种方法来在我的 WinForm 应用程序加载之前预处理一些事情。我尝试将 static void Main() 放入类库项目中的表单中,并从 Program.cs 中将其注释掉。这产生了编译时错误:“...不包含适合入口点的静态'Main'方法”。这是有道理的,因为程序没有加载,DLL 也没有加载。
所以问题是,有没有办法做到这一点?我希望 DLL 中的表单能够确定使用哪个表单启动应用程序:
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
if(condition1)
{
Application.Run(new Form1());
}
else if(condition2)
{
Application.Run(new Form2());
}
}
此逻辑将在多个应用中使用,因此将其放在一个通用组件中是有意义的。
最佳答案
您能否只在您的 DLL 中添加一个您的应用程序调用的静态方法,而不是在 main 中进行处理?
// In DLL
public static class ApplicationStarter
{
public static void Main()
{
// Add logic here.
}
}
// In program:
{
[STAThread]
public static void Main()
{
ApplicationStarter.Main();
}
}
关于c# - 在 WinForm 应用程序中将入口点移动到 DLL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1358976/