Экспорт функции из библиотеки netstandard2.0C#

Место общения программистов C#
Ответить
Anonymous
 Экспорт функции из библиотеки netstandard2.0

Сообщение Anonymous »

Я пытаюсь экспортировать функцию из библиотеки net8.0. Для этого я использую проект экспорта DLL 3F (https://github.com/3F/DllExport).

Код: Выделить всё

    public class Class1
{
[DllExport]
public static int AddOne(int x)
{
Console.WriteLine("Inside AddOne");
return x + 1;
}
}
Код тестовых программ .net Core и .net Framework следующий:

Код: Выделить всё

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);
Импорт DLL в тестовое приложение .net Core и в .net Framework приводит к ошибке:

Код: Выделить всё

.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.
Чего я хочу достичь в итоге? Мне нужна DLL .net Core и ее функции, которые мне нужно использовать в библиотеке netstandard2.0. (На данный момент мы не можем обновить библиотеку из-за устаревших зависимостей.
Полное решение, готовое к клонированию, находится здесь: 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
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C#»