简体   繁体   中英

Loading latest version of an assembly

I am targeting version 10 of SQL Server SMO libraries. I'd like to set up my application such that it uses the latest version of the SMO libraries that are installed on the machine, as the end user may have version 10 or 11.

How can I set up my Visual Studio C# project to behave this way? I have the assemblies loaded in the usual way, and Specific Version = false. It seems Specific Version is just a compile time change, but at runtime, it will still look for the assembly with a specific version.

While testing on a machine that only has version 11 of hte assemblies, my application , which targets version 10, fails to find the version 10 assemblies, and ultimately fails to load the assemblies.

Is there a way to get the program to load whatever the latest version of the assembly is? I've been looking at AppDomain.AssemblyResolve, but am not sure if simply loaidng the Assembly again, perhaps not specifying a version, would work.

AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;

    private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
    {
        Logger.Error("Failed to load assembly: " + args.Name + " from " + args.RequestingAssembly);
        var assembly = Assembly.Load(args.Name);            
        return assembly;
    }

It depends on the target .Net Framework Version you are using. Try targeting the .Net Framework Version that the assemblies that you want to load are for and use Specific Version = false. It will automatically load the required version.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM