3

您能否将此代码从 C# 转换为 Python 以在 IronPython 上运行?

我对 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);  
    }
}
}
4

4 回答 4

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
于 2009-02-12T19:52:06.867 回答
2

如果您查看以下链接,我认为它最适合您:

http://www.learningpython.com/2006/10/02/ironpython-hello-world-tutorial/ http://msdn.microsoft.com/en-us/magazine/cc300810.aspx

于 2009-02-12T19:51:16.040 回答
2

或者,如果您真的很懒惰,可以使用开发人员融合中的C# 到 Python 转换器!

于 2010-06-02T13:52:47.147 回答
-1

如果其他人有这个问题,SharpDevelop 有一个转换实用程序可以在 C# 和 IronPython、VB.NET 或 Boo 之间进行转换 http://community.sharpdevelop.net/blogs/mattward/archive/2009/05/11/ConvertingCSharpVBNetCodeToIronPython.aspx

于 2010-11-17T20:40:08.953 回答