go-dotnet

Go wrapper for the .NET Core Runtime.

Github星跟蹤圖

go-dotnet

GoDoc
MIT License
Build status

This is a PoC Go wrapper for the .NET Core Runtime, this project uses cgo and has been tested under OSX. It covers two basic use cases provided by the CLR Hosting API:

  • Load and run an .exe, using its default entrypoint, just like corerun and coreconsole do, check ExecuteManagedAssembly.

  • Load a .dll, setup delegates and call them from your Go functions.

I've tried calling both C# and VB.NET methods, of course you need to generate the assembly first, check below for more details!

Note: After some tweaks it seems to work fine under Linux! Remember to install the SDK first :)

Capture

An example

package main

import (
	"github.com/matiasinsaurralde/go-dotnet"

	"fmt"
	"os"
)

func main() {
	fmt.Println("Hi, I'll initialize the .NET runtime.")

	/*
		If you don't set the TRUSTED_PLATFORM_ASSEMBLIES, it will use the default tpaList value.
		APP_PATHS & NATIVE_DLL_SEARCH_DIRECTORIES use the path of the current program,
		this makes it easier to load an assembly, just put the DLL in the same folder as your Go binary!
		You're free to override them to fit your needs.
	*/

	properties := map[string]string{
		// "TRUSTED_PLATFORM_ASSEMBLIES": "/usr/local/share/dotnet/shared/Microsoft.NETCore.App/1.0.0/mscorlib.ni.dll:/usr/local/share/dotnet/shared/Microsoft.NETCore.App/1.0.0/System.Private.CoreLib.ni.dll",
		// "APP_PATHS":                     "/Users/matias/dev/dotnet/lib/HelloWorld",
		// "NATIVE_DLL_SEARCH_DIRECTORIES": "/Users/matias/dev/dotnet/lib/HelloWorld",
	}

	/*
		CLRFilesAbsolutePath sets the SDK path.
		In case you don't set this parameter, this package will try to find the SDK using a list of common paths.
		It seems to find the right paths under Linux & OSX, feel free to override this setting (like the commented line).
	*/

	runtime, err := dotnet.NewRuntime(dotnet.RuntimeParams{
		Properties:                  properties,
		// CLRFilesAbsolutePath: "/usr/share/dotnet/shared/Microsoft.NETCore.App/1.0.0"
	})
	defer runtime.Shutdown()

	if err != nil {
		fmt.Println("Something bad happened! :(")
		os.Exit(1)
	}

	fmt.Println("Runtime loaded.")

	SayHello := runtime.CreateDelegate("HelloWorld", "HelloWorld.HelloWorld", "Hello")

    // this will call HelloWorld.HelloWorld.Hello :)
	SayHello()
}

Preparing your code (C#)

I've used dmcs (from Mono) to generate an assembly file, the original code was something like:

using System;

namespace HelloWorld {

	public class HelloWorld {
    	public static void Hello() {
      		Console.WriteLine("Hello from .NET");
    	}
	}

}

And the command:

dmcs -t:library HelloWorld.cs

Preparing your code (VisualBasic)

I did a quick test with this program, using the VB.NET compiler from Mono:

vbnc -t:library HelloWorld.vb

I'm not sure about the status of Roslyn but it could be interesting to try it.

Setup

Coming soon!

Ideas

  • Run some benchmarks.
  • Add/enhance net/http samples, like this.
  • Provide useful callbacks.
  • Support blittable types.
  • CSharpScript support.
  • Code generation tool (with go generate), a few notes here.
  • Add tests.

I'm open to PRs, Go/.NET swag, suggestions, etc.

Additional resources

Build Status

Linux x64 / Go 1.9/1.10 / .NET Core 1.0/2.0 - OS X / Go 1.9/1.10 - .NET Core 1.0/2.0

Linux and OS X build status

Windows / Go 1.9 / .NET Core 2.x

Windows build status

License

MIT

主要指標

概覽
名稱與所有者matiasinsaurralde/go-dotnet
主編程語言C++
編程語言Go (語言數: 5)
平台
許可證MIT License
所有者活动
創建於2016-07-24 10:08:21
推送於2018-05-24 12:04:33
最后一次提交2018-05-24 01:22:35
發布數0
用户参与
星數487
關注者數28
派生數41
提交數124
已啟用問題?
問題數10
打開的問題數6
拉請求數3
打開的拉請求數0
關閉的拉請求數1
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?