I'd like to dynamically load the latest installed version of an assembly in the GAC using reflections.
So far I've found multiple working ways to accomplish this but they're all having their specific downsides.
The easiest solution is using the Assembly.LoadWithPartialName()
method. But this method is obsolete since .NET Framework 2:
var assembly = Assembly.LoadWithPartialName("Microsoft.WindowsAzure.ServiceRuntime");
Another possibility is to use Assembly.Load()
(as recommended by the obsolete warning) and call the different assembly versions with their fully qualified assembly name in a try/catch block to get the latest installed version. This screams for maintenance and just looks dirty:
Assembly assembly = null;
try
{
assembly = Assembly.Load("Microsoft.WindowsAzure.ServiceRuntime, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35");
}
catch (FileNotFoundException) { }
try
{
assembly = Assembly.Load("Microsoft.WindowsAzure.ServiceRuntime, Version=1.7.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35");
}
catch (FileNotFoundException) { }
Last but not least there's another solution I found here on SO using the Assembly.LoadFrom()
method and then basically importing the assembly manager module fusion.dll
to figure the path of the latest version. This seems to be way too much for such a "simple" task.
Isn't there any better solution to accomplish that without using an obsolete method, creating a maintenance hell with magic strings or by calling unmanaged code?
Thanks in advance!