2

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

例如,宿主应用程序有一个内部静态类“全局”,其中包含许多静态公共成员,它们是我想要访问的各种全局对象:

static class Global
{
  public static FeederSystem Feed ...
  public static LightingSystem Lighting ...
  public static IOSystem Io ...
  ... etc
}

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

是否有一个 IronPythonic 等效于“InternalsVisibleTo”,我可以使用它来允许 Python 代码查看主机应用程序的内部类型?还是我需要将它们全部公开?

4

1 回答 1

3

好的,所以我在 DLR 规范的帮助下自己解决了这个问题,从这里https://github.com/IronLanguages/dlr/blob/master/Docs/dlr-spec-hosting.pdf并通过查看 IP /DLR 源。

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

但这一个有效:

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

_engine = Python.CreateEngine(options);
于 2008-11-15T02:53:19.927 回答