开发者

Passing multiple arguments from IronPython to .NET method

I have a class in .NET (C#):

public class MyHelper {
    public object exec( st开发者_Python百科ring script, params object[] arguments ) {
        // execute script with passed arguments in some external enviroment
    }
}

I'm using IronPython runtime in my code to run python scripts, which should in some cases call the "exec" method. I would like serve the comfortable way to call the "exec" method. Something like:

helper.exec( "someExternalFunction( {0}, {1}, {3} )", var01, var02, var03 )

But I don't know how to declare the "exec" method in C# to achieve this. In python I can use a "*args" argument:

def exec( script, *args ):
    ... do something ...

I don't want have separate Python method "exec" from "MyHelper" class, because the "MyHelper" class provides complex functionality "in one place".

How should I write the "exec" method declaration in C# to achieve that? Or what other solution should I use?

Thanks


The problem here is that "exec" is a keyword in Python so you can't use that as your function name. You could use "exec_" or execute or something like that instead. Alternately you could write:

getattr(helper, 'exec')(...)


According to this FAQ, the MyHelper.exec you've defined should accept both an array as the second argument, or any number of objects following the first string.

If your example call to helper does not invoke as expected, that is probably a limitation of the IronPython interpreter, and probably needs to be filed as a bug. Before filing it as a bug, however, please create a minimal runnable C# script that demonstrates what you're trying to do (show how it works as expected in C#), and an IronPython script that attempts to do the same thing but fails. This will be invaluable toward making the problem work.

In the meantime, why not just call

helper.exec( "someExternalFunction( {0}, {1}, {3} )", [var01, var02, var03] )

?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜