Home > Mobile >  Can I add a reference to a .NET Framework DLL from a .NET 6 project?
Can I add a reference to a .NET Framework DLL from a .NET 6 project?

Time:11-18

Microsoft recently announced .Net 6.0 as major version. I didn't find any reference where we can use older .NET framework (> 4.7.*) references in .net 6.0 project?

Say, a .NET 6 project will have dll reference of .NET Framework 4.7.1 for windows application?

CodePudding user response:

This is a question with a complex answer.

Let me blunt. It is highly likely the answer to your question will be no, it can't be done, but that is because you have a very specific library in mind. The real answer is that it depends on the library in question, but chances are mostly geared towards this not working.

Yes, you can load .NET Framework assemblies into .NET Core 5 and 6.

However, depending on what that library does, and probably more important, what dependencies it has aka other libraries it wants to drag with it, it might not work properly for any sizable complex library.

There are classes that doesn't exist in .NET 5 , or even just single method overloads or properties. Depending on the exact parts of .NET Framework you access, it may in fact just be missing outright in .NET 5 .

There is more information here.

Your best option is probably to try to:

  • Re-compile the library for your particular .NET version (be it 5 or 6)
  • Re-compile the library targeting .NET Standard 2.1, as .NET 5 is compatible with that

However, you might in fact just get into the exact same problem with that re-compile as you will have to deal with those changed bits. But, then at least you would have more knowledge about what would and wouldn't work, and you have a chance to fix it.

If it's not your library to change, either replace it, or try and hope for the best.

CodePudding user response:

In general, no you can't. They're not really designed to be compatible. .NET Core and the subsequent .NET 5 and 6 are based on a complete re-write of .NET.

There may be some chance of things which work using the .NET Framework Compatibility mode but it's far from guaranteed.

If you want reliable, cross-framework compabitility for your libraries between .NET Framework and .NET Core / 6 you can try to target your libraries at .NET Standard

CodePudding user response:

The Problem

Assemblies that "target" (are compiled against/for) .NET Framework (1.0-4.8) may use APIs (types, methods) that are not present in .NET Standard or .NET Core.

So if you have a project that targets .NET Standard or .NET Core, and you want to add a reference to an assembly that targets .NET Framework, it may be that the assembly will throw exceptions at runtime, because it's missing method overloads or types. Those types are present in the Framework DLLs, but not in the .NET Core runtime assemblies.

Now if you know (through interpreting the code or through testing, preferably both) that the Framework-targeting assembly doesn't use APIs that are missing from .NET Standard or .NET Core, you're fine (for the difference, see What is the difference between .NET Core and .NET Standard Class Library project types?).

The fix (but not really)

If it's an assembly that's chucked in a lib folder in source control, you can add an assembly reference:

<Reference Include="../lib/path/To/Dll.dll" />

If it's a NuGet-packed dependency, you can install it and override the warning:

<PackageReference Include="Some.Framework.Package" Version="1.0.0" NoWarn="NU1701" />

The fix (for real this time)

Recompile the assembly to target .NET Standard 2.0, and package and distribute that through NuGet.

  • Related