All Projects → azurefx → DotNET.jl

azurefx / DotNET.jl

Licence: MIT license
Julia ❤️ .NET

Programming Languages

julia
2034 projects

Projects that are alternatives of or similar to DotNET.jl

swift-bridge
swift-bridge facilitates Rust and Swift interop.
Stars: ✭ 260 (+246.67%)
Mutual labels:  ffi, interop
clr-loader
Loader for different .NET runtimes
Stars: ✭ 16 (-78.67%)
Mutual labels:  ffi, clr
Pythonnet
Python for .NET is a package that gives Python programmers nearly seamless integration with the .NET Common Language Runtime (CLR) and provides a powerful application scripting tool for .NET developers.
Stars: ✭ 2,873 (+3730.67%)
Mutual labels:  ffi, clr
rust-lang-interop
Rust language interoperability with other languages - C, C++ etc.
Stars: ✭ 53 (-29.33%)
Mutual labels:  ffi, interop
elixir-ffi
Foreign Function Interface (FFI) for Elixir
Stars: ✭ 52 (-30.67%)
Mutual labels:  ffi
Python Mpv
Python interface to the awesome mpv media player
Stars: ✭ 245 (+226.67%)
Mutual labels:  ffi
Node Win32 Api
win32 api
Stars: ✭ 214 (+185.33%)
Mutual labels:  ffi
Rust Ffi Guide
A guide for doing FFI using Rust
Stars: ✭ 207 (+176%)
Mutual labels:  ffi
profiler-api
The portable version of JetBrains profiler API for .NET Framework / .NET Core / .NET / .NET Standard / Mono
Stars: ✭ 21 (-72%)
Mutual labels:  clr
efi
Ergonomic Rust framework for writing UEFI applications.
Stars: ✭ 44 (-41.33%)
Mutual labels:  ffi
cxx-cmake-example
An example repo to setup cxx (Rust FFI library) with the CMake build system.
Stars: ✭ 64 (-14.67%)
Mutual labels:  ffi
pedax
Reversi Board with edax, which is the strongest reversi engine.
Stars: ✭ 18 (-76%)
Mutual labels:  ffi
ArcaneManagedFbx
C# and .NET Framework wrapper for the Autodesk FBX SDK written in C++/CLI (managed C++).
Stars: ✭ 36 (-52%)
Mutual labels:  clr
Awesome Php Ffi
PHP FFI examples and use cases
Stars: ✭ 217 (+189.33%)
Mutual labels:  ffi
tflite native
A Dart interface to TensorFlow Lite (tflite) through dart:ffi
Stars: ✭ 127 (+69.33%)
Mutual labels:  ffi
Inline Rust
Use snippets of Rust inline in your Haskell programs
Stars: ✭ 210 (+180%)
Mutual labels:  ffi
rust-ffi-examples
FFI examples written in Rust
Stars: ✭ 42 (-44%)
Mutual labels:  ffi
terrafx.interop.windows
Interop bindings for Windows.
Stars: ✭ 183 (+144%)
Mutual labels:  interop
Curryrs
Bridge the gap between Haskell and Rust
Stars: ✭ 252 (+236%)
Mutual labels:  ffi
speckle-blender
speckle.systems/tag/blender/
Stars: ✭ 38 (-49.33%)
Mutual labels:  interop

DotNET.jl

Build Status PRs Welcome

This package provides interoperability between Julia and Common Language Runtime, the execution engine of .NET applications. Many languages run on CLR, including C#, Visual Basic .NET and PowerShell.

Prerequisites

  • Julia version 1.3+
  • .NET Core Runtime version 2.2+ (Download)

WinForms and other GUI-related features require a desktop runtime.

If the package fails to locate the runtime, set DOTNET_ROOT environment variable to the path containing the dotnet or dotnet.exe binary.

.NET Framework is not supported yet.

Installation

In the REPL, type ]add DotNET and press Enter.

(v1.x) pkg> add DotNET

Or use Pkg.add for more options:

julia> using Pkg

julia> Pkg.add(PackageSpec(url = "https://github.com/azurefx/DotNET.jl"))

Usage

julia> using DotNET

Types and Objects

DotNET.jl provides the T"AssemblyQualifiedTypeName" literal for type reference:

julia> Console = T"System.Console, mscorlib"
System.Console

Given a type object, you can access its properties or methods using the dot operator:

julia> Console.WriteLine("Hello from .NET!");
Hello from .NET!

To create an object, use the new syntax:

julia> T"System.Guid".new("CA761232-ED42-11CE-BACD-00AA0057B223")
System.Guid("ca761232-ed42-11ce-bacd-00aa0057b223")

All .NET objects are represented by CLRObjects in Julia, including types:

julia> typeof(Console)
CLRObject

julia> typeof(null)
CLRObject

null is a built-in object that does not refer to a valid .NET object. When you try to access a member on a null value, a System.NullReferenceException is thrown.

Arguments passed to .NET methods are automatically converted to CLRObjects, and return values are converted to corresponding Julia types:

julia> T"System.Convert".ToInt64("42")
42

Or you could do some explicit conversions:

julia> s = convert(CLRObject, "")
System.String("")

julia> DotNET.unbox(s)
""

To pass an argument by reference (out/ref in C#), wrap it into a Ref object:

julia> result = Ref{Int}()
Base.RefValue{Int64}(212700848)

julia> T"System.Int64".TryParse("1970", result)
true

julia> result[]
1970

julia> result = Ref(null)
Base.RefValue{CLRObject}(null)

julia> T"System.Int64".TryParse("2022", result)
true

julia> result[]
System.Int64(2022)

Arrays and Collections

To copy a multidimensional array from .NET to Julia, use collect method:

julia> arr = convert(CLRObject, reshape(1:8, 2, 2, 2))
System.Int64[,,]("System.Int64[,,]")

julia> collect(arr)
2×2×2 Array{Int64, 3}:
[:, :, 1] =
 1  3
 2  4

[:, :, 2] =
 5  7
 6  8

CLI Array elements are stored in row-major order, thus the equivalent definition in C# is

public static long[,,] Get3DArray() {
  return new long[2, 2, 2] {
    {{1, 2}, {3, 4}},
    {{5, 6}, {7, 8}}
  };
}

To index into arrays, use arraystore and arrayload methods. Note that CLI Arrays use zero-based indexing.

julia> DotNET.arraystore(arr, (1, 1, 1), 0)
null

julia> DotNET.arrayload(arr, (1, 1, 1)) == collect(arr)[2, 2, 2]
true

If an object implements IEnumerable interface, you can call GetEnumerator to iterate over the array:

julia> ch = Channel() do it
           e = arr.GetEnumerator()
           while e.MoveNext()
               put!(it, e.Current)
           end
       end
Channel{Any}(0) (1 item available)

julia> collect(ch)
8-element Vector{Any}:
 18

Or just use the for-in loop:

for x in arr
    println(x)
end

Loading External Assemblies

If you have a DLL file, you can load it using reflection:

julia> T"System.Reflection.Assembly".LoadFrom(raw"C:\Users\Azure\Desktop\test.dll")
System.Reflection.RuntimeAssembly("test, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null")

Now you have access to types defined in the assembly.

Generics

Generic types are be expressed in the following ways:

julia> ListT = T"System.Collections.Generic.List`1"
System.Collections.Generic.List`1[T]

julia> ListInt64 = T"System.Collections.Generic.List`1[System.Int64]"
System.Collections.Generic.List`1[System.Int64]

The number 1 after the backtick indicates the type System.Collections.Generic.List<T> has one type parameter. ListT has a free type variable, just like Vector{T} where T in Julia. A type that includes at least one type argument is called a constructed type. ListInt64 is a constructed type.

One can substitute type variables and make a constructed type by calling makegenerictype method:

julia> DotNET.makegenerictype(ListT, T"System.String")
System.Collections.Generic.List`1[System.String]

To invoke a generic method, put type arguments into square brackets:

julia> list = ListT.new[T"System.Int64"]()
System.Collections.Generic.List`1[System.Int64]("System.Collections.Generic.List`1[System.Int64]")

Delegates

To create a delegate from a Julia method, use delegate method:

julia> list = ListT.new[T"System.Int64"](1:5)
System.Collections.Generic.List`1[System.Int64]("System.Collections.Generic.List`1[System.Int64]")

julia> list.RemoveAll(delegate(iseven, T"System.Predicate`1[System.Int64]"))
2

julia> collect(list)
3-element Vector{Int64}:
 1
 3
 5
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].