gpt4 book ai didi

c# - 从 IronPython 调用 C# 对象方法

转载 作者:行者123 更新时间:2023-11-30 14:43:00 27 4
gpt4 key购买 nike

我正在尝试在我的游戏中嵌入脚本引擎。因为我是用 C# 编写的,所以我认为 IronPython 非常适合,但我找到的示例都集中在 C# 中调用 IronPython 方法,而不是 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 成熟。如果足够接近,我可能会接受。

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

最佳答案

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

关于c# - 从 IronPython 调用 C# 对象方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2459080/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com