Код: Выделить всё
public class Class1
{
[DllExport]
public static int AddOne(int x)
{
Console.WriteLine("Inside AddOne");
return x + 1;
}
}
Код: Выделить всё
static void Main(string[] args)
{
Console.WriteLine("Hello, DotNetCore");
try
{
Console.WriteLine("1+1=" + AddOne(1));
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
Console.ReadKey();
}
[DllImport("ClassLibrary.dll")]
private static extern int AddOne(int x);
Код: Выделить всё
.net Core:
System.Runtime.InteropServices.SEHException (0x80004005): External component has thrown an exception.
at ConsoleCore.Program.AddOne(Int32 x)
at ConsoleCore.Program.Main(String[] args) in C:\work\stackoverflow\core and framework\Console\ConsoleCore\Program.cs:line 12
Код: Выделить всё
.net Framework:
System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
File name: 'System.Runtime, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
at ConsoleApp.Program.AddOne(Int32 x)
at ConsoleApp.Program.Main(String[] args) in C:\work\stackoverflow\core and framework\Console\Console\Program.cs:line 17
=== Pre-bind state information ===
LOG: DisplayName = System.Runtime, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
(Fully-specified)
LOG: Appbase = file:///C:/work/stackoverflow/core and framework/Console/Console/bin/Debug/
LOG: Initial PrivatePath = NULL
Calling assembly : (Unknown).
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\work\stackoverflow\core and framework\Console\Console\bin\Debug\Console.exe.Config
LOG: Using host configuration file:
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config.
LOG: Post-policy reference: System.Runtime, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
LOG: Attempting download of new URL file:///C:/work/stackoverflow/core and framework/Console/Console/bin/Debug/System.Runtime.DLL.
LOG: Attempting download of new URL file:///C:/work/stackoverflow/core and framework/Console/Console/bin/Debug/System.Runtime/System.Runtime.DLL.
LOG: Attempting download of new URL file:///C:/work/stackoverflow/core and framework/Console/Console/bin/Debug/System.Runtime.EXE.
LOG: Attempting download of new URL file:///C:/work/stackoverflow/core and framework/Console/Console/bin/Debug/System.Runtime/System.Runtime.EXE.
Полное решение, готовое к клонированию, находится здесь: https://github.com/JYPDWhite/DllExportTest
Изменить: tl;dr;
Библиотека, написанная на netstandard, должна использоваться в библиотеке netstandard2.0 через P/Invoke Этот netstandard библиотека позже используется в приложениях .net Core и .net Framework.
Приложение .net Core в этом примере предназначено только для устранения неполадок и это не окончательный вариант использования.
Подробнее здесь: https://stackoverflow.com/questions/793 ... -0-library
Мобильная версия