I have a simple .Net Core Console application referencing a .Net Framework 4.6.2 project in the same solution. When I build, I get the following error:
error CS0012: The type 'Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
Am I trying to do something that is not allowed?
Is there some way to reference a .Net Framework 4.6 (or Mono) assembly (or P/Invoke it) from a .Net Core application?
Using Visual Studio 2017, .Net Core 1.1
.Net Core and .Net 4.0 has different core libraries. The type
System.Object
from old .Net libraries is defined inmscorlib
, which clearly hasn't been referenced.If you do reference that library, you probably will get the name resolving error, because the same types will be defined in the same namespaces and with same type names in different libraries. So yes, this is not allowed.
Edit: However, there is a common approach known as
Shared project
, you can read about it here. Also this quaestion may be useful for you:.net core classlibrary calling .net framework class library
Also you may try to create a portable class library, which can be referenced either from .Net Core and .Net 4.6 library.
Edit #2: As @EricErhardt mentioned, you can recompile your .Net 4.6 library with different targeting, which may be not an option if you don't have a source code.
In .NET Core 1.x, you are not allowed to reference a .NET Framework 4.6 (or Mono) assembly. .NET Core 1.x and .NET Framework are 2 different runtimes, and their types don't mix - like the build error message says.
Instead, what you can do is to recompile that .NET Framework 4.6 assembly targeting ".NET Standard". Read more about .NET Standard. Compiling a library for .NET Standard allows that library to run on .NET Framework, Mono, or .NET Core. Another option is to multi-target that .NET Framework 4.6 assembly, so it builds both for .NET Framework 4.6 and for .NET Core 1.1. You should only do this if the APIs your library requires are not supported by .NET Standard. This is roughly the same as @VMAtm's answer, but you can do it with a single .csproj in VS 2017.
The good news is, what you are trying to do will be supported on .NET Core 2.0, which is slated for release later this year.