为了账号安全,请及时绑定邮箱和手机立即绑定

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 关注
  • 224 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号