将入口点移动到WinForm应用程序中的DLL

cod*_*ove 3 c# dll entry-point

我试图找到一种方法来预先处理我的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());
   }
}
Run Code Online (Sandbox Code Playgroud)

此逻辑将在多个应用程序中使用,因此将其放在通用组件中是有意义的.

Ree*_*sey 7

你能在你的应用程序调用的DLL中添加一个静态方法,而不是在main中进行处理吗?

// In DLL
public static class ApplicationStarter
{
     public static void Main()
     {
          // Add logic here.
     }
}

// In program:
{
     [STAThread]
     public static void Main()
     {
          ApplicationStarter.Main();
     }
}
Run Code Online (Sandbox Code Playgroud)