【问题标题】:Calling C# object method from IronPython从 IronPython 调用 C# 对象方法
【发布时间】:2011-01-28 09:08:36
【问题描述】:

我正在尝试在我的游戏中嵌入脚本引擎。因为我是用 C# 编写的,所以我认为 IronPython 会非常合适,但是我能够找到的示例都集中在调用 IronPython 中的 C# 方法而不是 IronPython 脚本中的 C# 方法。

更复杂的是,我在 Windows 7 64 位上使用 Visual Studio 2010 RC1。

IronRuby 的工作方式与我预期的一样,但我对 Ruby 或 Python 语法不是很熟悉。

我在做什么:

        ScriptEngine engine = Python.CreateEngine();
        ScriptScope scope = engine.CreateScope();

        //Test class with a method that prints to the screen.
        scope.SetVariable("test", this); 

        ScriptSource source = 
          engine.CreateScriptSourceFromString("test.SayHello()", Microsoft.Scripting.SourceCodeKind.Statements);

        source.Execute(scope);

这会产生错误,“'TestClass' 对象没有属性 'SayHello'”

虽然使用“self.test.SayHello()”,但这个确切的设置在 IronRuby 上运行良好

我对使用 IronRuby 持谨慎态度,因为它看起来不像 IronPython 那样成熟。如果它足够接近,我可能会去。

有什么想法吗?我知道这一定很简单。

【问题讨论】:

  • 从 IronPython 调用 C# 方法对我来说效果很好。看不出您的 sn-p 有什么问题,也许可以将其完整发布,以便我们对其进行测试。我也使用 IronPython 和 IronRuby,IronPython 的稳定性要高 10 倍。如果你开始推送 IronRuby,你就会遇到问题。

标签: c# ironpython ironruby


【解决方案1】:

这可能是因为您没有将“test”类声明为“public”以使其对 IronPython 可见。

【讨论】:

    【解决方案2】:

    我刚刚有这个......你没有发布你所有的代码,但我猜 SayHello 是公开的,这是正确的,但是包含 SayHello 函数的类也需要公开,以便 Python 可以看到它。

    【讨论】:

    • 如果是这样的话,IronRuby为什么能看到方法呢?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-19
    • 2010-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多