Invoking a .NET Framework 4 (or Mono) assembly from a .Net Core application

I have a simple .Net Core Console application referencing a .Net Framework 4.6.2 project in the same solution. When I create, 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 the assembly 'mscorlib, Version = 4.0.0.0, Culture = neutral, PublicKeyToken = b77a5c561934e089'.

Am I trying to do something that is not allowed?

Is there a way to reference the .Net Framework 4.6 (or Mono) (or P / Invoke it) assembly from a .Net Core application?

Using Visual Studio 2017, .Net Core 1.1

+5
source share
2 answers

In .NET Core 1.x, you are not allowed to reference the assembly of the .NET Framework 4.6 (or Mono) .. NET Core 1.x and the .NET Framework are two different modes of operation, and their types do not mix, as the message about build error.

Instead, you can recompile the targeting of the .NET Framework 4.6 ".NET Standard" assembly. Learn more about the .NET standard . Compiling a library for .NET Standard allows you to use this library in the .NET Framework, Mono, or .NET Core. Another option is the multi-purpose assembly of the .NET Framework 4.6, so it creates both for the .NET Framework 4.6 and for .NET Core 1.1. You should only do this if the API that your library requires is not supported by .NET Standard. This is roughly the same as @VMAtm answer, but you can do it with one .csproj in VS 2017.

The good news is that what you are trying to do will be supported on .NET Core 2.0, which is slated for release later this year .

+7
source

.Net Core and .Net 4.0 have different main libraries. The System.Object type from older .Net libraries is defined in mscorlib , which is not explicitly referenced.

If you reference this library, you are likely to get a name resolution error, because the same types will be defined in the same namespaces and with the same type names in different libraries. So yes, this is not allowed.

Edit: However, there is a general approach known as the Shared project , you can read about it here . Also this question may be useful for you:

.net core classlibrary calls .net class class library

You can also try creating a portable class library that can be referenced either from the .Net Core and .Net 4.6 libraries.

Portable class libraries are great when your shared code is platform independent, and also for reusable libraries where specific platform code can be accounted for.

Edit # 2: As @EricErhardt mentioned, you can recompile your .Net 4.6 library with different targeting, which may not be an option if you don't have source code.

+1
source

Source: https://habr.com/ru/post/1265289/


All Articles