这似乎只是一个我无法理解的简单错误。我在一个C#WPF应用程序中使用IronPython,在试图从一个定制的C#类运行函数时遇到以下错误:AttributeError: 'MyScriptingFunctions' object has no attribute 'Procedure'
。在
我运行的python脚本非常简单,有两行代码。第1行执行良好,错误发生在第2行。在
txt.Text = "some text"
MyFunc.Procedure(5)
在MyScriptingFunctions.cs公司名称:
^{pr2}$下面是我如何设置IronPython引擎:
private void btnRunScript_Click(object sender, RoutedEventArgs e)
{
MyScriptingFunctions scriptFuncs = new MyScriptingFunctions();
ScriptEngine engine = Python.CreateEngine();
ScriptScope scope = engine.CreateScope();
ScriptRuntime runtime = engine.Runtime;
runtime.LoadAssembly(typeof(String).Assembly);
runtime.LoadAssembly(typeof(Uri).Assembly);
//Set Variable for the python script to use
scope.SetVariable("txt", fullReadResultsTextBox);
scope.SetVariable("MyFunc", scriptFuncs);
string code = this.scriptTextBox.Text;
try
{
ScriptSource source = engine.CreateScriptSourceFromString(code, SourceCodeKind.Statements);
source.Execute(scope);
}
catch (Exception ex)
{
ExceptionOperations eo;
eo = engine.GetService<ExceptionOperations>();
string error = eo.FormatException(ex);
MessageBox.Show(error, "There was an Error");
return;
}
}
我只需设置两个变量:txt
,其类型为System.Windows.Controls.TextBox
,和{MyScriptingFunctions
的对象。在
我做错了什么?为什么python脚本正确地执行TextBox方法而不是我的自定义类的方法?在
我唯一能想到的可能是一个问题,或者只是一个复制粘贴错误,就是
MyScriptingFunctions
不是public
。这不应该是一个问题,因为您传递的是一个实例,而不是试图导入类,但是值得一试。否则一切看起来都很好。在相关问题 更多 >
编程相关推荐