C#とC言語の連携は.NET同士はもちろんのこと,ネイティブなC言語のライブラリでも容易に連携できます。C#のコードにDllImportなどいくつか記述を追加するのみです。Microsoftの以下のドキュメントに詳細に説明されています。
https://learn.microsoft.com/ja-jp/dotnet/framework/interop/creating-prototypes-in-managed-code
#!/bin/bash
rm -rf net2c
dotnet new console -o net2c
cat << EOF > net2c/Program.cs
using System;
using System.Runtime.InteropServices;
public class Net2C
{
[DllImport("libadd.so")]
private static extern int add(int x, int y);
public static void Main(string[] args)
{
Console.WriteLine("START");
Console.WriteLine(add(1,2));
Console.WriteLine("END");
}
}
EOF
gcc --shared -o net2c/libadd.so -xc - << EOS
int add(int x, int y) { return x + y; }
EOS
cd net2c
dotnet build
if [ $? -eq 0 ]; then
dotnet run
fi
cd ..
