IronPython integration with C#/ .NET

旧街凉风 提交于 2019-12-02 12:38:26

You can create a function to run your IronPythonScript and catch and re-raise exceptions that tell you what line of code is problematic as follows:

public static dynamic RunIronPythonScript(string fileName)
{
    var ipy = IronPython.Hosting.Python.CreateRuntime();
    try
    {
        dynamic test = ipy.ExecuteFile(fileName);
        return test;
    }
    catch (Exception e)
    {
        var engine = IronPython.Hosting.Python.GetEngine(ipy);
        ExceptionOperations eo = engine.GetService<ExceptionOperations>();
        string error = eo.FormatException(e);
        throw new Exception(error);
    }
}

Then you can call it as follows:

 static void Main(string[] args)
 {
    RunIronPythonScript("crack.py");
 }

This will at least show you which line of code is causing the error, making it easier to fix the script up.

Try:

Microsoft.Scripting.Hosting.ScriptEngine engine =
     IronPython.Hosting.Python.CreateEngine();

engine.ExecuteFile("crack.py");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!