Loading a Framework 3.5 library on Framework 4.0 runtime
I would like to know what I need to bear in mind if I create libraries that can run either on a .NET Framework 开发者_运维百科3.5 or .NET Framework 4.0 installed run-time. I want some backward compatibility like Enterprise Library does (but only with 3.5 nothing before that). I saw that they've compiled the libraries on 3.5 but is able to run on 4.0 apps. I would like the same effect, hopefully with MINIMAL project maintenance effort. I hope my question is clear enough. Please let me know via a comment if not.
EDIT: I found that if I set the app.config to recognize both the 2.0 and 4.0 run-times, my EXE actually runs fine on either run-times. I tested it by uninstalling both .NET 3.5 and 4.0 individually to see if each one can separately run it. There might be some things to take into account using each of those frameworks but for small tool'ish types of apps and libs, I think it should be fine. Anyone to disagree?
Thanks
The runtimes are backwards compatible. So the 4.0 runtime can run a 3.5, and 2.0 based assembly, but it is not the other way around. If you tried to run a 4.0 assembly on the 2.0 runtime you would get a BadImageFormatException, because the 2.0 runtime cannot load a 4.0 assembly. Side-by-side execution is also another option, but applies to the later versions of the runtime (4.0+ I believe) and last I heard Microsoft is still working on that for future runtime versions that can be isolated. You should not have to compile multiple versions of your assemblies, but you do need to bear in mind the minimum runtime version that will be required for your application. If you have a single module that requires .NET 3.5 for example, than that should be the minimum required runtime for your entire application.
MS has an article on this http://msdn.microsoft.com/en-us/library/ms171868.aspx
In-Process Side-by-Side Execution This feature enables an application to load and start multiple versions of the .NET Framework in the same process. For example, you can run applications that load add-ins (or components) that are based on the .NET Framework 2.0 SP1 and add-ins that are based on the .NET Framework 4 in the same process. Older components continue to use the older .NET Framework version, and new components use the new .NET Framework version. For more information, see In-Process Side-by-Side Execution.
精彩评论