### Call a managed function with default signature from Rust using netcorehost Source: https://github.com/openbytedev/netcorehost/blob/master/README.md This example illustrates how to call a C# method from Rust using `netcorehost` when the C# method uses the default `ComponentEntryPoint` signature (`IntPtr args, int sizeBytes`). It shows the C# method definition and the Rust code to load the assembly, get the function delegate, and invoke it. ```C# public delegate int ComponentEntryPoint(IntPtr args, int sizeBytes); using System; namespace Test { public static class Program { public static int Hello(IntPtr args, int sizeBytes) { Console.WriteLine("Hello from C#!"); return 42; } } } ``` ```Rust let hostfxr = nethost::load_hostfxr().unwrap(); let context = hostfxr.initialize_for_runtime_config(pdcstr!("Test.runtimeconfig.json")).unwrap(); let fn_loader = context.get_delegate_loader_for_assembly(pdcstr!("Test.dll")).unwrap(); let hello = fn_loader.get_function_with_default_signature( pdcstr!("Test.Program, Test"), pdcstr!("Hello"), ).unwrap(); let result = unsafe { hello(std::ptr::null(), 0) }; assert_eq!(result, 42); ``` -------------------------------- ### Call a managed function with UnmanagedCallersOnly from Rust using netcorehost Source: https://github.com/openbytedev/netcorehost/blob/master/README.md This snippet demonstrates how to invoke a C# method marked with the `[UnmanagedCallersOnly]` attribute from Rust using `netcorehost`. This attribute allows direct unmanaged calls without specifying a signature. The example includes both the C# method definition and the Rust code to load and call it. ```C# using System; using System.Runtime.InteropServices; namespace Test { public static class Program { [UnmanagedCallersOnly] public static void UnmanagedHello() { Console.WriteLine("Hello from C#!"); } } } ``` ```Rust let hostfxr = nethost::load_hostfxr().unwrap(); let context = hostfxr.initialize_for_runtime_config(pdcstr!("Test.runtimeconfig.json")).unwrap(); let fn_loader = context.get_delegate_loader_for_assembly(pdcstr!("Test.dll")).unwrap(); let hello = fn_loader.get_function_with_unmanaged_callers_only::( pdcstr!("Test.Program, Test"), pdcstr!("UnmanagedHello"), ).unwrap(); hello(); // prints "Hello from C#!" ``` -------------------------------- ### Run a .NET application using netcorehost in Rust Source: https://github.com/openbytedev/netcorehost/blob/master/README.md This snippet demonstrates how to initialize the .NET Core runtime, load a specified DLL (e.g., `Test.dll`), and execute its `Main` method using the `netcorehost` library in Rust. It shows the basic steps for hosting a .NET application. ```Rust let hostfxr = nethost::load_hostfxr().unwrap(); let context = hostfxr.initialize_for_dotnet_command_line(pdcstr!("Test.dll")).unwrap(); let result = context.run_app().value(); ``` -------------------------------- ### Define and Call Custom Delegate Type in C# and Rust Source: https://github.com/openbytedev/netcorehost/blob/master/README.md This snippet demonstrates how to define a custom delegate type and a static method in C# that can be called from Rust. It then shows the Rust code required to load the .NET runtime, obtain a delegate loader, and invoke the C# function using its assembly qualified name and the custom delegate type. ```C# using System; namespace Test { public static class Program { public delegate void CustomHelloFunc(); public static void CustomHello() { Console.WriteLine("Hello from C#!"); } } } ``` ```Rust let hostfxr = nethost::load_hostfxr().unwrap(); let context = hostfxr.initialize_for_runtime_config(pdcstr!("Test.runtimeconfig.json")).unwrap(); let fn_loader = context.get_delegate_loader_for_assembly(pdcstr!("Test.dll")).unwrap(); let hello = fn_loader.get_function::( pdcstr!("Test.Program, Test"), pdcstr!("CustomHello"), pdcstr!("Test.Program+CustomHelloFunc, Test") ).unwrap(); hello(); // prints "Hello from C#!" ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.