c# - 在 WinForm 应用程序中将入口点移动到 DLL

标签 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());
   }
}

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

最佳答案

您能否只在您的 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/

相关文章:

spring-boot - 我为Spring Boot Docker容器添加负载平衡的选项是什么

c# - 从后面的代码访问用户控件 (ascx) 的父 DIV

c# - 如何使用 C# Windows 应用程序读取 onedrive 文件夹

c++ - OpenSSL RSA 库抛出异常

c++ - 加载共享库时出错 : No such file or directory even after using LD_LIBRARY_PATH and changing $PATH

c# - delphi 导入具有指定入口点的dll函数

c# - MVC URL 模式中的 OR 运算符

javascript - HttpModule Filter_OnTransformString 导致部分静态文件失效(即css、js

c - 打开 DOS 窗口并从 DLL 中喷出调试消息

c++ - 制作一个 VB-dll 并将其加载到 C++ 应用程序中