2009-02-12 65 views
1

您可以將此代碼從C#轉換爲Python以便在IronPython上運行嗎?將代碼從C#轉換爲Python所需的幫助

我對Python沒有任何經驗。

using System; 
using Baz; 

namespace ConsoleApplication 
{ 
    class Program 
    { 
    static void Main() 
    { 
     Portal foo = new Portal("Foo"); 
     Agent bar = new Agent("Bar"); 

     foo.Connect("127.0.0.1", 1234); 
     foo.Add(bar); 

     bar.Ready += new Agent.ReadyHandler(bar_Ready);    
    } 

    static void bar_Ready(object sender, string msg) 
    {  
     Console.WriteLine(msg.body); 
    } 
} 
} 

回答

5

實例化不需要類型定義。稱爲相同的方法,直接分配代表。以前的答案是絕對正確的,爲了將C#應用程序「轉換」爲Python,您需要更多上下文;它不僅僅是語法。

foo = Portal("Foo") 

bar = Agent("bar") 

foo.Connect("ip", 1234) 

foo.Add(bar) 

bar.Ready = bar_Ready 

def bar_Ready(sender, msg): 

    print msg.body 
+0

之間的轉換這是正確的,除了事件勾線應該是「bar.Ready + = bar_Ready',如果你認爲門戶和代理類仍然在C#中實現。 – babbageclunk 2009-02-13 14:15:49

相關問題