Reputation: 11
I have Specific Version set to False for a bunch of DLLs as references to a C# console project. I'm aware this is a compile time check.
So how can I allow previous versions of the DLLs during run time?
Thanks!
Upvotes: 0
Views: 2168
Reputation: 30590
From MSDN - Assembly Versioning:
The runtime distinguishes between regular and strong-named assemblies for the purposes of versioning. Version checking only occurs with strong-named assemblies.
If you do have a strong-named assembly, you can redirect the reference to it. See MSDN - Redirecting Assembly Versions.
In your case, you could use AppDomain.AssemblyResolve
. Here is an example:
AppDomain.CurrentDomain.AssemblyResolve += (sender, eventArgs) =>
{
var fullName = new AssemblyName(eventArgs.Name);
// should check that the assembly is the one we support old versions for
var wantedDLL = fullName.Name + ".dll";
// locate the DLL here... your path will vary
var found = Assembly.LoadFile(Path.Combine(Environment.CurrentDirectory, wantedDLL));
return found;
};
Alternately, if you know the exact old version number, you can load it with the Assembly.Load(AssemblyName)
overload.
var fullName = new AssemblyName(eventArgs.Name)
{
Version = new Version(1, 0, 0, 0)
};
return Assembly.Load(fullName);
You should also be careful with errors here. Throwing exceptions can result in weird behaviour, and if the assembly fails to be loaded, then the method will recurse infinitely resulting in StackOverflowException
.
Upvotes: 5