To use ClassLibrary2 in ClassLibrary1, you need to make sure that both class libraries target the same .NET framework version. You can either upgrade ClassLibrary1 to .NET framework v4.6.1 or above, or downgrade ClassLibrary2 to .NET standard v1.6 or below, which supports .NET framework v4.5.2 and above.
There are a few other ways to resolve this issue:
- Use a different version of .NET Standard: If you can, try using a different version of .NET Standard in your ClassLibrary2 project. This may resolve the conflict with System.Runtime and allow you to reference the project in ClassLibrary1 without issues.
- Use a different version of .NET Framework: If possible, try targeting a different version of .NET Framework in your ClassLibrary1 project. This may allow you to reference ClassLibrary2 without running into runtime errors.
- Use assembly binding redirects: You can use assembly binding redirects in your app.config or web.config file to redirect references to a specific version of System.Runtime to another version that is compatible with your project. This will allow you to reference both ClassLibrary1 and ClassLibrary2 without errors.
- Use multi-targeting: If you're using Visual Studio 2019 or later, you can use multi-targeting to target both .NET Framework and .NET Standard in the same project. This will allow you to reference ClassLibrary2 directly in ClassLibrary1 without any conflicts.
To "Use assembly binding redirects", you need to add an app.config
file to your application or library project. In this file, you can specify the redirection rules for each assembly that needs to be redirected. Here is an example of what the app.config
file might look like:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="4.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
Or, to "Use multi-targeting", in Visual Studio, follow these steps:
- Right-click on the project and select Properties.
- In the Properties window, select the Application tab.
- In the Target Framework dropdown, click on the "..." button.
- In the "Target Frameworks" window, select the frameworks you want to target. You can select multiple frameworks by holding down the Ctrl key while clicking.
- Click OK to close the "Target Frameworks" window.
- Save the changes to the project properties.
After following these steps, you will be able to write code that targets multiple frameworks in the same project. Note that when you build your project, Visual Studio will create a separate build output for each targeted framework.