2

I'm 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', which contains a number of static public members, which are are the various global objects I'd 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 refer to Global.Lighting.xxx in Python code, just as I can in the C# application.

Is there an IronPythonic equivalent of 'InternalsVisibleTo' which I can use to allow Python code to see the internal types of the host application? Or do I need to make them all public?

1 Answer 1

4

Ok, so I worked this out myself, with the aid of the DLR spec, from here https://github.com/IronLanguages/dlr/blob/master/Docs/dlr-spec-hosting.pdf and by looking at the IP/DLR source.

This isn't very elegant, and using a ScriptRuntimeSetup object with the PrivateBinding property set True would probably be a neater route than using CreateEngine.

But this one works:

Dictionary<string, object> options = new Dictionary<string, object>();
options.Add("PrivateBinding", true);

_engine = Python.CreateEngine(options);
Sign up to request clarification or add additional context in comments.

1 Comment

Note: check github.com/IronLanguages/ironpython2/blob/master/Src/IronPython/…: class members (properties/methds/....) which are internal are also exposed but not by their original name, instead by _<type>__<member> so suppose Feed in the example code above wasn't public, it would be accessed in Python by Global._Global__Feed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.