GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing pythonnet.github.io — Usage - clr-loader documentation Usage¶Getting a runtime¶To get a from clr_loader import get_coreclr runtime = get_coreclr() After this, the runtime will usually already be initialized. The initialization is delayed for .NET Core to allow adjusting the runtime properties beforehand. Information on the runtime, its version and parameters can be retrieved using
Getting a callable function¶A wrapped assembly can be retrieved from the runtime by calling
The following example class is provided in the repository: using System.Text; using System.Runtime.InteropServices; using System; namespace Example { public class TestClass { public static int Test(IntPtr arg, int size) { var buf = new byte[size]; Marshal.Copy(arg, buf, 0, size); var bufAsString = Encoding.UTF8.GetString(buf); var result = bufAsString.Length; Console.WriteLine($"Called {nameof(Test)} in {nameof(TestClass)} with {bufAsString}, returning {result}"); Console.WriteLine($"Binary data: {Convert.ToBase64String(buf)}"); return result; } } } Assuming it has been compiled to assembly = runtime.get_assembly("path/to/assembly.dll") Note This does not guarantee that the DLL is already loaded and will not necessarily trigger an error if that is not possible. Actually resolving the DLL only happens (for all implementations but Mono) when retrieving the concrete function. The function = assembly.get_function("Example.TestClass", "Test") function = assembly.get_function("Example.TestClass.Test") This function can now be called with a Python result = function(b"testy mctestface") The Called Test in TestClass with testy mctestface, returning 16 Binary data: dGVzdHkgbWN0ZXN0ZmFjZQ==
Warning While the buffer can theoretically also be changed in the .NET function, this is not tested. |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |