.NET Reference dll from another place

I make a program depending on some DLLs included in a third-party program. I am not allowed to distribute these DLLs myself. For my program to work, you need to install a third-party program.

How can I link to these dlls? I know their exact location through the registry key installed by the program.

I tried to add files in Project-> References and set CopyLocal to false, but when I started i, then get a FileNotFoundException "Could not load file or assembly."

I tried adding an event to AppDomain.CurrentDomain.AssemblyResolve and uploading the files there, but the problem is that I get an exception before my program even starts. Even if I set a breakpoint on the first line, an exception will be thrown before the breakpoint is removed.

+5
source share
2 answers

From C # 3.0 in a Nutshell , 3rd Edition, Joseph and Ben Albahari , p. 557-558:

Deploying assemblies outside the base folder

Sometimes you may want to deploy assemblies to locations other than the application’s base directory [...] In order to do this, you must help the CLR in finding assemblies outside the base folder. The simplest solution is to handle the event 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
    }
}

, , , DLL . , AssemblyResolve - .

+9

, JIT , . Main(). , Main , , . Release, . :

using System;
using System.Runtime.CompilerServices;

class Program {
    static void Main(string[] args) {
        AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
        DelayedMain(args);
    }

    [MethodImpl(MethodImplOptions.NoInlining)]
    static void DelayedMain(string[] args) {
        // etc..
    }


    static System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args) {
        // etc...
    }
}

, . . GAC , , gacutil dev.

+4

All Articles