7

I have a third-party library that requires an assembly A to be loaded when I call into their code. That assembly is typically installed in the GAC, so I have several options to load it:

  1. I can explicitly call Assembly.Load(). However that requires the full name which I don't feel comfortable to hard code in my program.
  2. I can explicitly call Assembly.LoadWithPartialName(). That is an obsolete API of course, and of course I don't feel comfortable to lose control in versioning.
  3. I can reference the assembly in my Visual Studio project file, so I always get the version I built against. However, that won't work unless I create a dummy object in that assembly. The C# compiler simply ignores it if I don't.
  4. Same problem if I call Assembly.GetReferencedAssemblies and force load the matched one. The C# compiler simply won't reference my assembly even if I put it in the references list.

Now what I'm doing is to call typeof(A.Foo).Assembly.GetName() and ignore the return value. Is there a better way to do it?

Todd Li
  • 3,209
  • 21
  • 19
  • How about option #1 but put the Assembly full name in app.config (or other similar config file)? That way you get the best of both worlds: - an explicit load of the assembly you want, yet the flexibility to update without a recompile should you need to – dkackman Jul 07 '11 at 00:44
  • @dkackman - that's something I need to manually update too. I wanted Visual Studio to pick up the current version from the version control, so I get the version at build time, instead of coding time or run time. – Todd Li Jul 07 '11 at 00:56
  • @focey - hmmm, has a fishy smell but... post-build step? get assembly, ferret out version, write to config file? Probably best to just keep doing what you're doing though i don't like that either – dkackman Jul 07 '11 at 01:04

1 Answers1

1

Option 1, for me, would be 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 executes when an assembly is needed that is not found in the AppDomain. The event args will tell you the Assembly that is being sought and you can go grab it at that point using Assembly.Load()

Glenn Ferrie
  • 10,290
  • 3
  • 42
  • 73
  • 1
    Thanks - unfortunately the 3rd party library calls `AppDomain.CurrentDomain.GetAssemblies()` and throws if the required one wasn't found. – Todd Li Jul 07 '11 at 00:39