How do I access internal objects from hosted IronPython?
I am hosting IronPython 2.0 in a C # / Winforms application. I would like Python to be able to access various global static objects in the host application.
As an example, the host application has an internal static class "Global" that contains several static public members that represent the various global objects that I would like to access:
static class Global
{
public static FeederSystem Feed ...
public static LightingSystem Lighting ...
public static IOSystem Io ...
... etc
}
I want to be able to reference Global.Lighting.xxx in Python code as I can in a C # application.
Does IronPythonic have an "InternalsVisibleTo" equivalent that I can use to let Python code see the internal types of the host application? Or do I need to make them public?
source to share
Ok so I did it myself using the DLR spec from here https://github.com/IronLanguages/dlr/blob/master/Docs/dlr-spec-hosting.pdf and looking at the IP / DLR source.
It's not very elegant, and using a ScriptRuntimeSetup object with the PrivateBinding property set to True is likely to be faster than using CreateEngine.
But this works:
Dictionary<string, object> options = new Dictionary<string, object>();
options.Add("PrivateBinding", true);
_engine = Python.CreateEngine(options);
source to share