即使缺少依赖dll,也要启动exe?

k3b*_*k3b 6 .net compiler-construction configuration attributes assemblies

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

使用Dotnet1.1和1.0,程序启动但稍后在尝试使用缺少的程序集的功能时崩溃.

我想知道是否有类似的东西

  • 编译开关,
  • 配置选项或
  • 一个dotnet [属性]

允许我在某些dll丢失时启动应用程序.

有没有moidfying源代码是可能的(通过应用一些Attriutes execpt)?

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

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

更新2010-12-25我在考虑复杂化.感谢来自@erinus的简单解决方案:

我只需要尝试捕获并且它有效:

    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);

            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

小智 1

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