简体   繁体   中英

Target.NET framework version vs .NET framework version installed

We have a C# project and in Visual Studio the according "Target framework" is currently set to ".NET Framework 4.5". We have a second client library (written in C++) that calls this C# library to do something. For some new features we want to use the framework 4.6.1+. But we don't worn that C# project directly.

So some question in mind:

Generally, if we upgrade "Target framework" to .NET Framework 4.6.1 and on the machines where our app is running the actual framework is pre 4.6.1 (eg, 4.5). What will happen? Will it throw some exception when the app is started?

If we stick to .NET Framework 4.5 as the target version, is there an option to automatically use the latest version actually installed on the machine where the app is run?

Thanks.

on the machines where our app is running the actual framework is pre 4.6.1 (eg, 4.5). What will happen? Will it throw some exception when the app is started?

It will not throw an exception when the app started... The only time it could throw an exception is if ( and when ) your library calls a method that exists in .NET 4.6.1 but does not exist in .NET 4.5... You will get a MissingMethodException , other than that everything should work fine.

Something you can do is inspect what is the .NET Framework that is being run in the process that loaded your library.

string version = AppDomain.CurrentDomain.SetupInformation.TargetFrameworkName;

You can do that at the very start of the method being called in the library. And if the .NET Framework is < 4.6.1 you can decide what to do... Show an error message, or change the behavior as to not call into some functions (and avoid MissingMethodException ).

One more thing you can also do determine which .NET Framework versions are installed on the machine and make some decisions on what to do.

.NET Framework 4.5+ is backward compatible. If app will be 4.6.1, but OS will have 4.5 then application error is possible (no forward compatibility). Documentation about version compatibility says:

By default, an app runs on the version of .NET Framework that it was built for. If that version isn't present and the app configuration file doesn't define supported versions, a .NET Framework initialization error may occur. In this case, the attempt to run the app will fail.

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