Reputation: 3269
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:
Assembly.Load()
. However that requires the full name which I don't feel comfortable to hard code in my program. Assembly.LoadWithPartialName()
. That is an obsolete API of course, and of course I don't feel comfortable to lose control in versioning.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?
Upvotes: 7
Views: 3738
Reputation: 10390
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()
Upvotes: 1