Is it possible to call C# functions with Julia?


#1

Hi all,
I am new to Julia, coming from python, and very excited to learn this wonderful language. Like to ask is there a library for Julia to call C# functions (in .Net framework) from Julia, like how Cxx.jl calls C++ from Julia. In python I used to use pythondotnet, which worked flawlessly. Searching through forums and google, what i understand is that julia’s FFI applies only to c and fortran. I have seen a few posts online about calling julia from within C#. Is there a way to go the other way? Calling C# from Julia?
Thank you all in advance,
and Merry Christmas and a Happy New Year!


#2

As a general rule, anything you can easily call from C will be callable from Julia. For example, there was some early discussion on calling Rust from Julia via ccall over in Interfacing rust? which was apparently quite successful.

I haven’t used C#, but as long as it’s possible to generate a library that you can call from C, you should be able to use that same library from Julia with ccall. Perhaps something like https://stackoverflow.com/a/6795403 would work?


#3

Have you considered or tried using pythondotnet via PyCall.jl?


#4

Thank you for your prompt replies. Much appreciated. Sorry for my late reply…

@ rdeits : I have looked at ccall, but as I am not fluent in C or C#, am not sure exactly how to go about this. Is there any online resource that might help to understand how to do this?

@garrison : Yes, I have. However, it seems like an inefficient way, calling python to call C#. I was hoping to see if I can find a way to call C# directly, make my code base all Julia. Having said that, this is my fallback plan in the event I cannot find a way to call C# directly from Julia


#5

As rdeits mentioned, there’s a couple solutions, depending on where you want to put the conversion codes. Earlier in the SO thread, someone mentions there’s 4 common patterns: https://stackoverflow.com/a/4428336/1712368. Anything mentioned about how to do this from C is directly applicable for doing this in Julia.


#6

Hi Jameson,
Thank you for the link. I will look into the 4 patterns and post my solutions once I figure out a workable solution. Thanks again, you guys are so helpful