ColinChang.InteropNet 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package ColinChang.InteropNet --version 1.0.0
NuGet\Install-Package ColinChang.InteropNet -Version 1.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ColinChang.InteropNet" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ColinChang.InteropNet --version 1.0.0
#r "nuget: ColinChang.InteropNet, 1.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install ColinChang.InteropNet as a Cake Addin
#addin nuget:?package=ColinChang.InteropNet&version=1.0.0

// Install ColinChang.InteropNet as a Cake Tool
#tool nuget:?package=ColinChang.InteropNet&version=1.0.0

InteropNet

The library allows you to work with native libraries. Standard approach with the DllImport attribute may be inconvenient if you want to build AnyCPU assembly with MS.NET/Mono support. The Interoptor class can generate implementation of interface with target signatures of native methods.

For example, let's create a native library (NativeLib) with the function int sum(int a, int b) { return a + b; } and build it in four configuration (Windows/Unix, x86/x64):

x86/NativeLib.dll
x86/libNativeLib.so
x64/NativeLib.dll
x64/libNativeLib.so

Now we can write the following code:

public interface INative
{
    [RuntimeDllImport("NativeLib", CallingConvention = CallingConvention.Cdecl, EntryPoint = "sum")]
    int Sum(int a, int b);
}

class Program
{
    static void Main()
    {
        var native = Interoptor.Create<INative>();
        Console.WriteLine("2 + 3 = " + native.Sum(2, 3));
    }
}

In the program, we declared interface INative with signatures of the target native methods. Each signature should be marked with the RuntimeDllImport attribute with name of a native library and other options. The instance of Interoptor helped us to create instance of the INative interface on the fly. The implementation of the sum method call corresponding native method from library that correspond to current architecture and OS. The Interoptor generates the following code:

namespace Interoptor.NativeInstance
{
  [UnmanagedFunctionPointer(CallingConvention.Cdecl, BestFitMapping = false, CharSet = (CharSet) 0, SetLastError = false, ThrowOnUnmappableChar = false)]
  [StructLayout(LayoutKind.Auto, CharSet = CharSet.Auto)]
  public delegate int SumDelegate(int a, int b);

  public class NativeImplementation : INative
  {
    private SumDelegate SumField;

    public NativeImplementation(LibraryLoader loader)
    {
      IntPtr dllHandle = loader.LoadLibrary("NativeLib", (string) null);
      this.SumField = (SumDelegate) Marshal.GetDelegateForFunctionPointer(loader.GetProcAddress(dllHandle, "sum"), typeof (SumDelegate));
    }

    public int Sum(int a, int b)
    {
      return this.SumField(a, b);
    }
  }
} 

As a result, we received a single .NET cross-platform AnyCPU-program with calls of native methods because of the LibraryLoader class loaded handles for specific user environment.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.1 659 12/1/2020
1.0.0 450 12/1/2020

translate it to netstandard2.1