Interoperability with .NET

nice job!

I was looking through the source and i noticed this - does this fail on non-windows? https://github.com/HyperSphereStudio/JULIA.Net/blob/25a8ad39b1802288dc07480de21ea5a48444960a/src/csharp/Julia.cs#L20

I am using the .net sdk on macosx.

I don’t know C# but if this works I might have a go and trying to write a F# typeprovider

Oh lol I forgot about that part. Any thoughts on multiplatform Julia Dir lookup? I provided a method to give your own dir but it would probaly be a good idea to have a default lookup. Also there is a kernel32 call which is not multiplatform.

I think those are the only two things that make it not multiplatform. Unfortunately I dont know anything about macOS. If anyone knows how to do the setdlldirectory / finding julia dir on macos, let me know

I think I wrote it in .Net framework aswell since I made this for another project. Ill port it over to .Net 5

1 Like

This looks really cool! Thanks for making it.

I actually just finished developing an interface between Julia and .NET for an internal project based on information from @oheil .
When I started that, I was a newcomer to both Julia and C#/VB.net so it seemed infeasible with the C interface.
So I did it using communications over pipes, like a client-server communication.
Though this takes time, (de-)serialization seems to be fine (i.e negligible to the user) in most cases, even with relatively large arrays (about tens of thousands of double precision numbers).

I do not know if you tackled this issue already but the main missing part for me in the documentation about julia’s C interface was how to use it properly to load and interact with modules and how to handle special types. I was completely lost i the whole packing / unpacking of pointers to structures…

Also, there are some base types that exist in Julia that do not in .NET, so some conversions need to be done explicitely. For instance I discovered that my .NET (maybe it is a matter of version) did not support Int8.

Keep up the work ! Cheers !

EDIT : See this answer from @Shuhua maybe its worth converging on this Correct process to use julia code from C# or VB .NET - #17 by Shuhua

3 Likes

I found almost no documentation, I just straight up read the Julia source code instead lol.

Regarding primitive types that are not cannot be converted, I personally have not run into any issues but I can have it promote them into larger containers if need be (Int8 → Int32 etc) but I havent had any issues so far.

I have just released the .NET 5.0 version of the project

Thanks for the link to the other thread, Ill see if at @Shuhua is willing to work together as I see he has done some work on natively converting C# arrays to Julia etc

Btw, Thoughts on implementing an abstract method interface for functions like what RuntimeMethod has and stuff? Can make it seem like a normal “method” object

Ok I think I made it multiplatform now. I have no way to test it on linux though.

Change → SetDllDirectory to Environment.CurrentDirectory
Change → “where.exe” → Windows ? “where.exe” : “which”

Does this look like it would work?

I will try it tonight.

which seems like the right command to me.

Once GitHub - JuliaLang/juliaup: Julia installer and version multiplexer becomes more widely deployed you can just execute juliaup api getconfig1 and get back a JSON with information about installed Julia versions. At the moment this only works on Windows and if the user installed Julia from the Windows store, but I am very, very close to releasing the Mac and Linux version of this as well. The VS Code extension already tries this method to find the Julia binary, and then falls back to other ways if that doesn’t work.

So, this is more a FYI, maybe not entirely ready yet to be used, but should be much more ready in a few weeks.

2 Likes

Sorry but I am not that familiar with .NET at the moment ! :wink:

Take a look at the DotNET.jl.
It provides a more friendly call syntax.

julia> T"System.String".new('6',Int32(3))
"666"

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

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

Thanks, will do!

I will try to implement something like


@CSusing System 


strT = T"String"
my_str = strT("Test")
str_len = my_str.Length

For the using implementation, probaly have it store them all in a list for each “using JuliaInterface” context? Is it possible to create a variable when a module is “used”?

Then read the list in reverse order so last added using statement is checked first?

Added.

Here is the current Julia.NET .NET → Julia Interface:

  @netusing Test
   @netusing System
   val1 = T"ReflectionTestClass".TestStateField[]   #< Requires [] to actually get the field. If you dont put [] or () then it will just return the FieldInfo object
   val2= T"ReflectionTestClass".StaticMethod[]() #To call a method. If you dont put [] or () then it will just return the MethodInfo object
   val3 = T"ReflectionTestClass".StaticGenericMethod[T"Int64"]() "To call a generic method, put the generic types in []

item = T"ReflectionTestClass"".new[](3)     #To call a constructor.  If you dont put [] or () then it will just return the ConstructorInfo object
   val4 = item.InstanceMethod[]();   #To call a instance method
   val5 = item.g[]       #To Access a instance field
   myGenericItem = T"ReflectionGenericTestClass`1".new[T"Int64"](3)    #To Create a generic instance of an object, put the generic types in [].
   boxed5 = sharpbox(5)   #Will return the sharp object of the long value "5"
   shouldBe5 = shapunbox(boxed5) #Will unbox the sharp object and return to native julia value
   myClass = T"Test.ReflectionTestClass"   #<= Perform Assembly Search and Return the Sharp Type
   myClass2 = P"Test.ReflectionTestClass"   #<= Perform one time assembly search and store the sharp type in a internal array (Reccommended for fast lookups)
   myClass3 = G"Test.ReflectionTestClass"   #Get from internal array
   myClass4 = R"Test.ReflectionTestClass"   #Remove from internal array
   handle = pin(sharpbox(5)) #Pin the Sharp object in the Sharp GC
   free(handle) #Will also auto free. You can also treat it like stream and put it in do end block
1 Like

I tried it on OSX. It seemed to throw in this function.

Glanced at the source and it seems like your approach for Linux should work in any POSIX-like systems .net supports (OSX, FreeBSD: OSPlatform.OSX Property (System.Runtime.InteropServices) | Microsoft Learn)

https://github.com/HyperSphereStudio/JULIA.Net/blob/main/src/csharp/OperatingEnvironment.cs#L21

welcome Experts,
Recently we have developed GUI using Avalonia C# + Julia
Akkugif

waiting for your suggestion & Support :slight_smile:

GitHub link: https://github.com/PiResearchTech/Ayudha

Recently works well in Windows & Ubuntu
Thank you.

2 Likes

Hello ! I’ve been (somewhat lazily) following your work on JULIA.Net and I see you have been pushing v2.0.0 recently.

I was wondering if you intend to publish a NuGet package or some other packaged version of this library, or is it designed to remain in this form ?

Currently it seems your effort is the most advanced one (that I know of) and that would probably be of help to a lot of potential users. See What don't you like about Julia for "serious work"? - #2 by Oscar_Smith for instance !

Maybe in the future I will make a nuget package. For now this project is a result of me working on another commerical project.

I moved Julia.NET to JuliadotNET to support it being a julia package.

I made a lot of changes to it recently. It was quite the effort trying to launch .NET from Julia lol.
But it seems to work! So you can now launch c# from julia and julia from c#. The julia->c# still needs a lot of work but it works in my tests

One thing I noticed is that you cant launch .NET from Julia if your in REPL for whatever reason. (Any thoughts on why this may be and how to fix it?)

I will be posting the package to the julia repo in the coming weeks

4 Likes

Have a look at DotNET.jl, I think it is possible to launch .NET but it needs to be .NET version 6.0 or greater. The .NET Framework is a different animal and doesn’t work, if I remember correctly.

Btw, your project looks amazing! I’ve also created something similar with struct mapping so you can use your Julia structs in C#. I’ll have a closer look and compare notes with you when I have some time.

I have been spending some time designing my own fork of julia from scratch (for fun!.. I think…). If I ever get it to work I could theoretically allow you to add a custom JIT compiler. I could add a CLR JIT Compiler with this directly allowing to inline julia into .NET.