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

查看:32
本文介绍了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() 函数的此页面说库和服务不需要作为入口点的主要方法".也不是说他们不能有默认入口点.

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 创建一个对象,但这似乎会使 starter 与它应该启动的程序更紧密地耦合.如果我可以使用 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天全站免登陆