.NET引用来自其他位置的DLL

aer*_*ero 5 .net c# dll

我正在制作一个程序,取决于第三方程序中包含的一些DLL.我不允许自己分发这些DLL.必须安装第三方程序才能使我的程序正常运行.

我如何引用这些DLL?我通过程序设置的注册表项知道它们的确切位置.

我试图在Project-> References中添加文件并将CopyLocal设置为false但是当我启动i然后得到FileNotFoundException"无法加载文件或程序集".

我试图向AppDomain.CurrentDomain.AssemblyResolve添加一个事件并在那里加载文件,但问题是我在程序启动之前得到了异常.即使我在第一行放置断点,也会在断点被触发之前抛出异常.

sta*_*ica 9

来自C#3.0 in a Nutshell,3rd edition,Joseph和Ben Albahari,p.557-558:

在基本文件夹之外部署程序集

有时您可能选择将程序集部署到应用程序基目录以外的位置 [...] 为了使其工作,您必须帮助CLR查找基本文件夹之外的程序集.最简单的解决方案是处理AssemblyResolve事件.

(我们可以忽略这样一个事实:在您的情况下,除您之外的其他人正在部署程序集.)

你试过哪个.但是后来有一个非常重要的线索.阅读两个代码注释:

public static void Loader
{
    static void Main(string[] args)
    {
        AppDomain.CurrentDomain.AssemblyResolve += FindAssem;

        // We must switch to another class before attempting to use
        // any of the types in C:\ExtraAssemblies:
        Program.Go();
    }

    static Assembly FindAssem(object sender, ResolveEventArgs args)
    {
        string simpleName = new AssemblyName(args.Name).Name;
        string path = @"C:\ExtraAssemblies\" + simpleName + ".dll";

        if (!File.Exists(path)) return null;
        return Assembly.LoadFrom(path);
    }
}

public class Program
{
    public static void Go()
    {
        // Now we can reference types defined in C:\ExtraAssemblies
    }
}
Run Code Online (Sandbox Code Playgroud)

如您所见,解析外部程序集的类不得在任何地方引用任何外部DLL中的任何类型.如果确实如此,代码执行将在您AssemblyResolve有机会运行之前停止.