How to force load reference assembly?

I have a third-party library that needs to download assembly A when I call their code. This assembly is usually installed in the GAC, so I have several options for loading it:

  • I can explicitly call Assembly.Load() . However, this requires a full name, which I do not like in the hard code in my program.
  • I can explicitly call Assembly.LoadWithPartialName() . Of course, this is an outdated API, and, of course, I don’t feel comfortable losing version control.
  • I can reference the assembly in the Visual Studio project file, so I always get the version I built with. However, this will not work if I do not create a dummy object in this assembly. The C # compiler simply ignores it if I do not.
  • Same problem if I call Assembly.GetReferencedAssemblies and force load consistent. The C # compiler simply will not reference my assembly, even if I put it in the list of links.

Now what I am doing is calling typeof(A.Foo).Assembly.GetName() and ignoring the return value. Is there a better way to do this?

+7
source share
1 answer

Option 1, for me, would have to reference it in the VS project.

But if you want a more passive approach, you can use the AppDomain.CurrentDomain.AssemblyResolve event handler. It runs when an assembly is required that is not found in the AppDomain. The args events will tell you about the Assembly you are looking for, and you can grab it at that moment using Assembly.Load ()

+1
source

All Articles