简体   繁体   English

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

[英]Moving entry point to DLL in a WinForm app

I am trying to figure out a way to pre-process few things before my WinForm app loads. 我试图找到一种在WinForm应用程序加载之前进行一些预处理的方法。 I tried putting static void Main() in a form within a class library project and commented it out from Program.cs. 我尝试将静态void Main()放在类库项目中的表单中,并从Program.cs中将其注释掉。 Which generated a compile time error: "...does not contain a static 'Main' method suitable for an entry point". 生成了编译时错误:“ ...不包含适用于入口点的静态'Main'方法”。 It makes sense since the program is not loaded, the DLL is not loaded either. 这是有道理的,因为没有加载程序,也没有加载DLL。

So the question is, is there a way to do this at all? 所以问题是,有没有办法做到这一点? I want the form in the DLL to be able to determine which form to launch the application with: 我希望DLL中的表单能够确定使用哪种表单启动应用程序:

[STAThread]
static void Main()
{
   Application.EnableVisualStyles();
   Application.SetCompatibleTextRenderingDefault(false);

   if(condition1)
   {
      Application.Run(new Form1());
   }
   else if(condition2)
   {
      Application.Run(new Form2());
   }
}

This logic will be used in more than one app so it makes sense to put it in a common component. 此逻辑将在多个应用程序中使用,因此将其放在一个公共组件中很有意义。

Can you just add a static method in your DLL that your application calls instead of doing the processing in main? 您是否可以在应用程序调用的DLL中添加一个静态方法,而不是在main中进行处理?

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

// In program:
{
     [STAThread]
     public static void Main()
     {
          ApplicationStarter.Main();
     }
}

Keep you Main method in Program.cs. 将Main方法保留在Program.cs中。 Let it call a method in dll which instantiates a Form based on the condition and return it to Main. 让它调用dll中的方法,该方法根据条件实例化Form并将其返回给Main。

The "static void Main" method has to be within the "EXE" assembly, but you could have this method make a call to your shared assembly's version of "Main". “静态无效的Main”方法必须在“ EXE”程序集中,但是您可以让此方法调用共享程序集的“ Main”版本。 You just can't do it directly. 您就是无法直接做到。

static void Main() doesn't make sense in a class library, however your snippet of code should do exactly what you want if placed in Program.cs. 静态void Main()在类库中没有意义,但是如果将代码段放置在Program.cs中,则您的代码段应该完全符合您的要求。

Also, do you need a catch-all 'else' clause, just in case condition1 and condition2 aren't met? 另外,您是否需要一个包罗万象的“ else”子句,以防不满足condition1和condition2? May not be required, but in most cases I would expect some form of feedback rather than the application silently exiting - depends on what you are doing of course. 可能不是必需的,但是在大多数情况下,我希望获得某种形式的反馈,而不是应用程序以静默方式退出-当然,这取决于您在做什么。

Edit: This might do what you want, if you simply need to separate the logic into a library 编辑:这可能会做您想要的,如果您只需要将逻辑分离到一个库中

// Program.cs
[STAThread]
static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);

    if(MyLib.Condition1)
    {
        Application.Run(new Form1());
    }
    else if(MyLib.Condition2)
    {
        Application.Run(new Form2());
   }
}


// MyLib.cs
...
public static bool Condition1
{
    get
    {
         return resultOfLogicForCondition1;
    }
}
public static bool Condition2
{
    get
    {
         return resultOfLogicForCondition2;
    }
}

Essentially you are trying to create a custom factory for the form to use for the application. 本质上,您正在尝试为该应用程序使用的表单创建自定义工厂。 Something like the following: 类似于以下内容:

In the EXE: 在EXE中:

static void Main()
{
    Application.Run(new Factory.CreateForm());
}

and in your library: 并在您的图书馆中:

public static class Factory 
{
    public static Form CreateForm()
    {
        if( condition ) return new Form1();
        else return new Form2();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM