2012-06-26 73 views
1

我想用嵌入式IronPython腳本編寫C#代碼。然後要分析腳本的內容,即列出所有變量,函數,類及其成員/方法。分析IronPython範圍

有一個簡單的方式開始,假設我有定義的範圍和代碼它已經執行:

dynamic variables=pyScope.GetVariables(); 
foreach (string v in variables) 
{ 
    dynamic dynamicV=pyScope.GetVariable(); /*seems to return everything. variables,  functions, classes, instances of classes*/ 
} 

但是我怎麼找出一個變量的類型是什麼?對於以下蟒 '對象',

dynamicV.GetType() 

將返回不同的值:

X = 5 --system.Int32

Y = 「ASDF」 --system.String

def func():... --IronPython.Runtime.PythonFunction

z = class() - IronPython.Runtime.Types.OldInstance,我該如何識別實際的python類是什麼?

class NewClass - 拋出錯誤,GetType()不可用。

這幾乎是我在找的東西。我可以捕獲不可用時引發的異常,並假定它是一個類聲明,但似乎不太清楚。有更好的方法嗎?

要發現的一類,它看起來像我可以使用的成員/方法:

ObjectOperations op = pyEngine.Operations; 
object instance = op.Call("className"); 
foreach (string j in op.GetMemberNames("className")) 
{ 
    object member=op.GetMember(instance, j); 
    Console.WriteLine(member.GetType()); 
    /*once again, GetType() provides some info about the type of the member, but returns null sometimes*/ 
} 

而且,我如何獲取這些參數的方法?

謝謝!

回答

1

這不是真的支持,但所有的信息都在那裏。您有兩種選擇:

  1. 將對象投射到IronPython的類,如PythonFunction,並使用它們上的方法。這些都沒有記錄,並且所有必要的方法可能不公開,但您可以閱讀源代碼以瞭解如何使用它們。
  2. 使用normal Python techniques for introspection,如__class____dict__。這可能是更好的選擇。