.net – 即使缺少依赖dll也启动exe?
发布时间:2020-12-13 20:46:40 所属栏目:百科 来源:网络整理
导读:在Dotnet2.0及更高版本中,如果其中一个依赖(静态引用)的dll丢失,程序将拒绝启动. 使用Dotnet1.1和1.0,程序启动但稍后在尝试使用缺少的程序集的功能时崩溃. 我想知道是否有类似的东西 编译开关, 配置选项或 一个dotnet [属性] 允许我在什么时候启动应用程序
在Dotnet2.0及更高版本中,如果其中一个依赖(静态引用)的dll丢失,程序将拒绝启动.
使用Dotnet1.1和1.0,程序启动但稍后在尝试使用缺少的程序集的功能时崩溃. 我想知道是否有类似的东西 >编译开关, 允许我在什么时候启动应用程序 有没有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); } } }
使用非托管代码.在try {…} catch {…}块中调用Windows API:LoadLibrary.如果缺少dll,则处理异常并保持进程运行.
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |