2008-11-15 29 views
1

我在C#/ Winforms應用程序中託管IronPython 2.0。我希望Python能夠訪問宿主應用程序中的各種全局靜態對象。如何從託管的IronPython訪問內部對象?

舉個例子,主機應用程序有一個內部的靜態類「全球性」,它包含了一些靜態公共成員,這是在全球不同的對象,我想訪問:

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

我希望能夠在Python代碼中引用Global.Lighting.xxx,就像我在C#應用程序中一樣。

是否存在IronPythonic的'InternalsVisibleTo'等價物,我可以使用它來允許Python代碼查看主機應用程序的內部類型?還是我需要讓它們全部公開?

回答

2

好吧,所以我在DLR規範的幫助下,從https://github.com/IronLanguages/dlr/blob/master/Docs/dlr-spec-hosting.pdf以及IP/DLR源代碼中,自己完成了這項工作。

這不是很優雅,並且使用PrivateRintimeSetup對象並將PrivateBinding屬性設置爲True可能會比使用CreateEngine更簡潔。

但是這一個工程:

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

_engine = Python.CreateEngine(options); 
+0

好找,謝謝! – 2009-10-20 18:22:33

相關問題