C#.dll程序集可以包含入口点吗? [英] Can a C# .dll assembly contain an entry point?

查看:395
本文介绍了C#.dll程序集可以包含入口点吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是创建一个可启动影子复制应用程序的可执行文件。诀窍是,我希望该启动程序没有外部依赖性,并且不必包含有关必须启动的程序的任何知识。

My goal is to create an executable that will start a shadow copied application. The trick is, I want this starter program to have no external dependencies and not have to contain any knowledge about the program it has to start.

我也希望它成为目录中唯一的可执行文件。换句话说,我希望它运行 .dll程序集而不是 .exe程序集。 (我可以要求每次加载到新AppDomain中的.dll文件的名称都相同,例如Main.dll或类似的名称。)

I also want it to be the only executable in the directory. In other words, I want it to "run" a .dll assembly not an .exe assembly. (I can require that the name of the .dll file being loaded into a new AppDomain be the same everytime, like Main.dll or something like that.)

AppDomain.ExecuteAssembly 一样可以完全满足我的要求。它说它将在 .NET Framework标头中指定的入口点开始执行。

It looked like AppDomain.ExecuteAssembly would do exactly what I wanted. It says it will start execution at the "entry point specified in the .NET Framework header.".

当我尝试使用该函数时,出现错误入口点在程序集 DllApp中找不到。

When I try to use that function I get the error "Entry point not found in assembly 'DllApp'".

我有一个启动程序,只是试图运行该程序集:

The starter program I have, just trying to run the assembly:

static void Main()
{
    AppDomain domain = AppDomain.CreateDomain( "DllApp" );
    domain.ExecuteAssembly( "DllApp.dll" );
}

.dll文件中的应用程序代码,带有默认入口点:

The application code, in a .dll file, with a default entry point:

static class Program
{
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault( false );
        Application.Run( new Form1() );
    }
}

有关Main()函数的页面说:库和服务不需要Main方法作为入口点。它并没有说它们也不能具有默认的入口点。

This page on Main() functions says that "Libraries and services do not require a Main method as an entry point". It doesn't say they cannot have a default entry point either.

我已经尝试了public / private static void main的所有各种排列。 ,一个int返回类型,以string [] args作为参数,带有一个命名空间,没有命名空间,静态/非静态类,等等。

I have tried all the various permutations of public/private static void main, an int return type, string[] args as arguments, with a namespace, no namespace, static/non-static class, etc.

我能够更改我的从MarshalByRefObject继承的代码,然后使用CreateInstance创建对象,但这似乎会使启动器与应该启动的程序更加紧密地耦合。如果我可以使用ExecuteAssembly,则正在启动的应用程序将只需要一个静态的void Main,这真的很简单并且很难弄乱。

I was able to change my code to inherit from MarshalByRefObject and then use CreateInstance to create an object, but that seems like it will more tightly couple the starter to the program it is supposed to start. If I could use ExecuteAssembly, the application being started would just need a static void Main, and that is really simple and hard to mess up.

是否可以使用。 dll程序集具有默认入口点,并让ExecuteAssembly找到它,还是我只需要辞职就走另一条路线?

Is it possible for a .dll assembly to have a default entry point, and for ExecuteAssembly to find it, or do I just have to resign myself to going another route?

推荐答案

您可以将.NET应用程序编译为exe(这是程序集),然后将其重命名为.DLL,它将用作普通的.NET .dll程序集。然后它将有您的入口点。

You can compile a .NET app as an exe (which is an assembly) and rename it to a .DLL and it will act as a normal .NET .dll assembly. It will then have your Entry point.

这篇关于C#.dll程序集可以包含入口点吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆