似乎只有一個簡單的錯誤,我無法弄清楚。我在C#WPF應用程序中使用IronPython,嘗試從自定義C#類運行函數時遇到以下錯誤:AttributeError: 'MyScriptingFunctions' object has no attribute 'Procedure'。我正在運行的python腳本非常簡單,有兩行。第1行執行正常,錯誤發生在第2行。 txt.Text = "some text" MyFunc.Procedure(5)MyScriptingFunctions.cs:class MyScriptingFunctions{ public MyScriptingFunctions() {} public void Procedure(int num) { Console.WriteLine("Executing procedure " + num); }}這是我設置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哪個是type System.Windows.Controls.TextBox,MyFunc哪個是我的自定義類的對象MyScriptingFunctions。我在做什么錯,為什么python腳本正確執行TextBox方法而不是自定義類的方法?
1 回答

HUH函數
TA貢獻1836條經驗 獲得超4個贊
我唯一可以看到的可能是問題,或者只是復制粘貼錯誤,原因是MyScriptingFunctions
in not public
。這不應該是一個問題,因為您要傳入一個實例,而不是嘗試導入該類,但這值得一試。否則,一切看起來都很好。
添加回答
舉報
0/150
提交
取消