亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

IronPython-帶有自定義類的“ AttributeError:對象沒有屬性”

IronPython-帶有自定義類的“ AttributeError:對象沒有屬性”

一只斗牛犬 2021-03-29 09:33:48
似乎只有一個簡單的錯誤,我無法弄清楚。我在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個贊

我唯一可以看到的可能是問題,或者只是復制粘貼錯誤,原因是MyScriptingFunctionsin not public。這不應該是一個問題,因為您要傳入一個實例,而不是嘗試導入該類,但這值得一試。否則,一切看起來都很好。


查看完整回答
反對 回復 2021-04-02
  • 1 回答
  • 0 關注
  • 258 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號