0

I have 2 versions of a vendor DLL that contain same classes but are different for reasons, and have slightly different names.

At any rate I either want to load DLLA or DLLB, I do not want to set these DLLs as copy local.

I had tried using AppDomain.CurrentDomain.Load(assemblyName), and setting a strong name so it could be found in GAC, only to find out the GAC is gone in .NET 5.0+ (not shedding a single tear for it).

So what is the "proper" way to do this now? Most docs I found on this are like 20 years old. I think I could just do a LoadFrom with file path?

8
  • LoadFrom only works with .net dll's. If not you will get an exception Commented Sep 23 at 23:27
  • or better yet, make it an embedded resource and load it using Load instead of LoadFrom if you don't want to use localcopy Commented Sep 23 at 23:32
  • 4
    From one of these listed methods? learn.microsoft.com/en-us/dotnet/core/dependency-loading/… Are you ever going to load both libraries into the same process? Commented Sep 24 at 2:50
  • 1
    What's the level of dynamic you need this to be? Is it a one-time decision at install, for example? Commented Sep 24 at 6:49
  • Each machine should only use DLLA or DLLB, won't be switching back and forth other than debug or if PC moves. We don't have an installer, just build the product and customer puts on machines, they only want 1 build config. So it sounds like hardcoding path or copying local are the way its done now? Commented Sep 24 at 12:58

1 Answer 1

0

Note: Old version of DLL was dotnet Framework, while new had both new dotnet and Standard, ended up using Std Dll and rolling back to 4.8, but from what I can tell this works in both

Best I can figure so far is to add a resolve event handler in the main routine

currentDomain.AssemblyResolve += new ResolveEventHandler(MyDLLLoadResolver);

Then catch the DLLs you care about and redirect as you please. (for instance different versions based of some other state)

private static Assembly MyDLLLoadResolver(object sender, ResolveEventArgs args)
{
        //load proper dll
        bool useOld = false;
        if (args.Name.Contains("Blah.Blah.dll, Version=1.96.0.0, Culture=neutral")
        {
                   if (useOld)
                   {
                       return Assembly.LoadFile(@"C:\Blah.Blah.dll");
                   }
                   else
                   {
                       return Assembly.LoadFile(@"C:\Blah.BlahV2.dll");
                   }
        }
        return null;
}

It's hardcoding paths and DLL names which is suboptimal but it solves my issue

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.