如何从托管的IronPython访问内部对象?

时间:2022-10-03 23:59:32

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.

我在C#/ Winforms应用程序中托管IronPython 2.0。我希望Python能够访问宿主应用程序中的各种全局静态对象。

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.

我希望能够在Python代码中引用Global.Lighting.xxx,就像在C#应用程序中一样。

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?

是否存在IronPythonic等效的'InternalsVisibleTo',我可以使用它来允许Python代码查看宿主应用程序的内部类型?或者我是否需要将它们全部公开?

1 个解决方案

#1


2  

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.

好了,我的工作这一点我自己,与DLR规格的帮助下,从这里https://github.com/IronLanguages/dlr/blob/master/Docs/dlr-spec-hosting.pdf并通过查看IP / DLR来源。

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.

这不是很优雅,并且使用PrivateBinding属性设置为True的ScriptRuntimeSetup对象可能比使用CreateEngine更简洁。

But this one works:

但是这个有效:

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

_engine = Python.CreateEngine(options);

#1


2  

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.

好了,我的工作这一点我自己,与DLR规格的帮助下,从这里https://github.com/IronLanguages/dlr/blob/master/Docs/dlr-spec-hosting.pdf并通过查看IP / DLR来源。

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.

这不是很优雅,并且使用PrivateBinding属性设置为True的ScriptRuntimeSetup对象可能比使用CreateEngine更简洁。

But this one works:

但是这个有效:

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

_engine = Python.CreateEngine(options);