Call a managed function with a custom delegate type
masterFor custom signatures, define a delegate in C# and provide its assembly-qualified name to AssemblyDelegateLoader::get_function. The third argument to get_function should be the assembly-qualified name of the delegate type (e.g., "Namespace.ClassName+DelegateName, AssemblyName").
// C# Implementation
using System;
namespace Test {
public static class Program {
public delegate void CustomHelloFunc();
public static void CustomHello() {
Console.WriteLine("Hello from C!");
}
}
}// Rust Implementation
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::<fn()>(
pdcstr!("Test.Program, Test"),
pdcstr!("CustomHello"),
pdcstr!("Test.Program+CustomHelloFunc, Test")
).unwrap();
hello(); // prints "Hello from C!"