即使缺少依赖 dll 也能启动 exe 吗?

发布于 2024-10-08 09:27:51 字数 1533 浏览 1 评论 0原文

在 Dotnet2.0 及更高版本中,如果程序的依赖(静态引用)dll 之一丢失,则程序将拒绝启动。

在 Dotnet1.1 和 1.0 中,程序可以启动,但后来在尝试使用缺少的程序集的功能时崩溃了。

我想知道是否有类似

  • 编译器开关、
  • 配置选项或
  • dotnet [属性]

之类的东西可以让我在以下情况下启动应用程序: 某些 dll 丢失。

是否可以不修改源代码(除了应用一些属性)?

我不想通过程序代码手动加载程序集或使用 IOC-Framworks。

更新:对于“静态引用的dll”,我的意思是与使用反射和Assembly.Loadxxxx()在我自己的程序代码中动态加载dll相反。

更新2010-12-25 我想得太复杂了。感谢@erinus 的简单解决方案:

我只需要把 try catch 放在周围就可以了:

    using System;
    using System.IO;
    using log4net; // log4net.dll might be missing

    namespace ConsoleAppWithMissingDll
    {
        class Program
        {
            static bool dllIsInstalled = true;
            static void Main(string[] args)
            {
                Console.WriteLine("Hello missing dll");

                try
                {
                    OutputViaLog4Net("hello log4net");
                }
                catch (FileNotFoundException)
                {
                    dllIsInstalled = false;
                    Console.WriteLine("Log4net-dll not found");
                }
                Console.WriteLine("Program continued");
    #if DEBUG
                Console.WriteLine("Press any key to exit");
                Console.ReadKey();
    #endif
            }

            private static void OutputViaLog4Net(string message)
            {

                ILog logger = LogManager.GetLogger("MyLogger");

                logger.Debug(message);

            }
        }
    }

In Dotnet2.0 and later a program refuses to start if one of its dependent (static referenced) dlls are missing.

With Dotnet1.1 and 1.0 the program started but crashed later when trying to use functionality of the missing assembly.

I wonder if there is something like a

  • compiler switch ,
  • configuration option or
  • a dotnet [attribute]

to allow me to start the app when
certain dlls are missing.

Is it possible without moidfying the sourcecode (execpt by applying some Attriutes)?

I don't want to manualy load assemblies by programcode or use IOC-Framworks.

Update: With "static referenced dlls" i mean the opposite of dynamicly loading a dll in my own programcode using reflection and Assembly.Loadxxxx().

Update 2010-12-25 I was thinking to complicated. Thanks simple solution from @erinus:

i just have to put try catch around and it worked:

    using System;
    using System.IO;
    using log4net; // log4net.dll might be missing

    namespace ConsoleAppWithMissingDll
    {
        class Program
        {
            static bool dllIsInstalled = true;
            static void Main(string[] args)
            {
                Console.WriteLine("Hello missing dll");

                try
                {
                    OutputViaLog4Net("hello log4net");
                }
                catch (FileNotFoundException)
                {
                    dllIsInstalled = false;
                    Console.WriteLine("Log4net-dll not found");
                }
                Console.WriteLine("Program continued");
    #if DEBUG
                Console.WriteLine("Press any key to exit");
                Console.ReadKey();
    #endif
            }

            private static void OutputViaLog4Net(string message)
            {

                ILog logger = LogManager.GetLogger("MyLogger");

                logger.Debug(message);

            }
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冰雪之触 2024-10-15 09:27:51

“静态引用的 dll”是一个矛盾修辞法,dll 中的 d 表示“动态”。有隐式引用的 dll,但只有非托管代码才使用它们。如果缺少这样的 DLL,则无法启动程序,该 DLL 在程序的入口点开始执行之前加载。

.NET 按需加载 dll,由 JIT 编译器触发。一旦它编译了存储在该 DLL 中的类型的方法,该 DLL 就会被加载。传送缺少此类 DLL 的代码在技术上是可能的,您必须小心编写代码,以便永远不会使用此类 DLL 中的类型。这是桌面版本的行为,不太确定 CF 版本是否也有同样的情况。

A "statically referenced dll" is an oxymoron, the d in dll means "dynamic". There are implicitly referenced dlls but only unmanaged code uses those. You cannot start a program with such a DLL missing, the dll is loaded before the program's entrypoint starts executing.

.NET loads dlls on demand, triggered by the JIT compiler. As soon as it compiles a method of a type that's stored in that DLL will the DLL be loaded. Shipping code with such a DLL missing is technically possible, you have to be careful to write your code so that a type from such a DLL is never used. This is the desktop version behavior, not quite sure if the CF version works the same way.

携余温的黄昏 2024-10-15 09:27:51

使用非托管代码。在 try{...}catch{...} 块中调用 Windows API:LoadLibrary。如果缺少 dll,则处理异常并保持进程运行。

Use unmanaged code. Call Windows API: LoadLibrary in try{...}catch{...} block. If dll is missing, handle exception and keep the process run.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文