ironpython是什么软件-运行在C#与IronPython的一个特定的Python函数

1586010002-jmsa.png

So far I have a simple class that wraps a python engine (IronPython) for my use. Although code looks big it's really simple so I copy it here to be more clear with my issue.

Here's the code:

public class PythonInstance

{

ScriptEngine engine;

ScriptScope scope;

ScriptSource source;

public PythonInstance()

{

engine = Python.CreateEngine();

scope = engine.CreateScope();

}

public void LoadCode(string code)

{

source = engine.CreateScriptSourceFromString(code, Microsoft.Scripting.SourceCodeKind.Statements);

source.Compile();

}

public void SetVariable(string key, dynamic variable)

{

scope.SetVariable(key, variable);

}

public void RunCode()

{

source.Execute(scope);

}

public void CallFunction(string function)

{

//?????? no idea what to call here

}

}

So, it works great but it only allows me to execute all python script at once... but what I would like to do is to be able to call particular functions from within a pythos script.

So, my question: How do I call particular function in the loaded script?

I was trying to find some information or tutorials but unfortunately couldn't find anything.

解决方案

Thanks to suggestion in comments I was able to figure out how to use it. Here's what I have now:

public class PythonInstance

{

private ScriptEngine engine;

private ScriptScope scope;

private ScriptSource source;

private CompiledCode compiled;

private object pythonClass;

public PythonInstance(string code, string className = "PyClass")

{

//creating engine and stuff

engine = Python.CreateEngine();

scope = engine.CreateScope();

//loading and compiling code

source = engine.CreateScriptSourceFromString(code, Microsoft.Scripting.SourceCodeKind.Statements);

compiled = source.Compile();

//now executing this code (the code should contain a class)

compiled.Execute(scope);

//now creating an object that could be used to access the stuff inside a python script

pythonClass = engine.Operations.Invoke(scope.GetVariable(className));

}

public void SetVariable(string variable, dynamic value)

{

scope.SetVariable(variable, value);

}

public dynamic GetVariable(string variable)

{

return scope.GetVariable(variable);

}

public void CallMethod(string method, params dynamic[] arguments)

{

engine.Operations.InvokeMember(pythonClass, method, arguments);

}

public dynamic CallFunction(string method, params dynamic[] arguments)

{

return engine.Operations.InvokeMember(pythonClass, method, arguments);

}

}

To test it:

PythonInstance py = new PythonInstance(@"

class PyClass:

def __init__(self):

pass

def somemethod(self):

print 'in some method'

def isodd(self, n):

return 1 == n % 2

");

py.CallMethod("somemethod");

Console.WriteLine(py.CallFunction("isodd", 6));

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值