Reference a .net framework 4.5.1 assembly in a 4.0 project

前端 未结 3 957
深忆病人
深忆病人 2020-12-09 16:52

How can i make a 4.0 project have a 4.5 reference. In the unit tests, i cant build the solution and it\'s giving me this warning.

Warning 2 The pr

相关标签:
3条回答
  • 2020-12-09 17:28

    You aren't able to reference a 4.5.1 assembly in a project that targets 4.0 . But ... you can call the method of a 4.5.1 assembly in a project that targets 4.0 by calling it dynamically, assuming 4.5.1 is installed:

    var assembly= Assembly.LoadFrom(...);
    var type = assembly.GetType(...);
    var method = type.GetMethod(...);
    var res = method.Invoke(null, args);
    

    Note that there may be limitations to this approach, but I found it useful for calling Roslyn routines while still using VS2010.

    0 讨论(0)
  • 2020-12-09 17:33

    .Net frameworks (v2.0 or higher) are not forward compatible. . You can't reference a .Net 4.5 assembly in .Net 4.0 project.

    See: Version Compatibility in the .NET Framework

    You may also see: Version Compatibility

    The degree of .NET Framework support for backward and forward compatibility is version-specific. The .NET Framework supports both backward and forward compatibility for applications created using version 1.1 only. It does not support forward compatibility in applications created using version 2.0. In the context of the .NET Framework, backward compatibility means that an application created using an early version of the .NET Framework will run on a later version. Conversely, forward compatibility means that an application created using a later version of the .NET Framework will run on an earlier version.

    The .NET Framework provides a high degree of support for backward compatibility. For example, most applications created using version 1.0 will run on version 1.1 and applications using version 1.1 will run on version 2.0. The .NET Framework also supports forward compatibility for version 1.1 only. However, for forward compatibility you might need to modify an application so that the application runs as expected. Applications created with version 2.0 will not run on earlier versions of the .NET Framework. For both backward and forward compatibility, a change to the .NET Framework that helps improve security, correctness, or functionality might also raise compatibility issues.

    0 讨论(0)
  • 2020-12-09 17:47

    Sounds like you need to change the framework of the library. And since it is only a unit tests project, I don't see why you wouldn't.

    In Visual Studio:

    • Right-click on your project
    • Select Properties
    • Select the Application tab
    • Change the Target Framework to the desired framework

    If you are not seeing .NET Framework 4.5.1 as an option there, ensure you have it installed.

    0 讨论(0)
提交回复
热议问题