embding C# for scripting in rust
I'm looking for a way to host C# for scripting, like Unity and godot do. I looked for mono, but I learn that "mono is dead" (since it only support C#7 and we are at C#11) and I should use dotnet now. But mono api for embding was far more complete. How do I expose native function to C#, there is no way to avoid unmanagedCallerOnly on C# callbacks ? How to handle exception (from what i've read, they are only supported on window, any other platfrom will result to an UB)
4 Replies
you will probably want to start here https://learn.microsoft.com/en-us/dotnet/core/tutorials/netcore-hosting
Write a custom .NET runtime host - .NET
Learn to host the .NET runtime from native code to support advanced scenarios that require controlling how the .NET runtime works.
I read this document more than 10 times
aka the question on exception
Exceptions Interoperability - .NET
Learn more about: Working with Interop Exceptions in Unmanaged Code
unity and godot seam to handle them with thier dotnet runtime
so it must be a way do deal with it 🤔
(mono handle them easly)
and for my beloved
[MethodImplAttribute(MethodImplOptions.InternalCall)]
, it exist with hostfxr/coreCLR, but only usable when COM API are defined (using C++/Cli, which isn't cross platfrom)
I can pass function pointer and cast them in the C# side, but it get very ugly and unreadable
both way, use C# as a scripting language like unity does
I'm sorry if I'm a bit dry, it's just that I've been looking at how to implement this for months and months.