简体   繁体   中英

Calling C# object method from IronPython

I'm trying to embed a scripting engine in my game. Since I'm writing it in C#, I figured IronPython would be a great fit, but the examples I've been able to find all focus on calling IronPython methods in C# instead of C# methods in IronPython scripts.

To complicate things, I'm using Visual Studio 2010 RC1 on Windows 7 64 bit.

IronRuby works like I expect it would, but I'm not very familiar with Ruby or Python syntax.

What I'm doing:

        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);

This generates an error, "'TestClass' object has no attribute 'SayHello'"

This exact set up works fine with IronRuby though using "self.test.SayHello()"

I'm wary using IronRuby though because it doesn't appear as mature as IronPython. If it's close enough, I might go with that though.

Any ideas? I know this has to be something simple.

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

这可能是因为您没有声明您的班级“test”是“公开的”,以使其对IronPython可见。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM