ColinChang.InteropNet 1.0.1

.NET Standard 2.1
Install-Package ColinChang.InteropNet -Version 1.0.1
dotnet add package ColinChang.InteropNet --version 1.0.1
<PackageReference Include="ColinChang.InteropNet" Version="1.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ColinChang.InteropNet --version 1.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: ColinChang.InteropNet, 1.0.1"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. 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.1

// Install ColinChang.InteropNet as a Cake Tool
#tool nuget:?package=ColinChang.InteropNet&version=1.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.


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):


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.


Only Windows and Linux(CentOS 8,Debian 9,Ubuntu 20.04 are tested) are supported.

We use to load library(*.so), it means some Linux release version like alpine doesn't support it.

the file is a library in most of Linux. Generally,it's in a system path configured in system environment variables.


Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows
.NET Core netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard2.1
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen60
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
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 399 12/1/2020
1.0.0 266 12/1/2020

update the name of to