gpt4 book ai didi

c# - IronPython:更改 SourceCodeKind.InteractiveCode 的行为

转载 作者:太空宇宙 更新时间:2023-11-03 10:50:09 26 4
gpt4 key购买 nike

我可以在 C# 中执行 IronPython:

ScriptSource Source = PyEngine.CreateScriptSourceFromString(Code, SourceCodeKind.InteractiveCode);
CompiledCode Compiled = Source.Compile();
Compiled.Execute(PyScope);

这意味着我可以执行 A=1然后 A .然后这将输出 1 .

有没有办法可以覆盖此行为 A将被重定向到 C# 函数,比如 CustomPrinter打印前?我知道我可以“重载” print功能如 https://stackoverflow.com/a/13871861/1447657 所述但我觉得应该有更好的方法。

编辑 1

感谢您的回答,但我对我的目标有点不清楚。

我知道我可以用 PyEngine.Runtime.IO.SetOutput(stream, encoding) 重定向输出但我希望在将其转换为字符串之前检索输出值。所以如果A=[1,2,3] IronPython.Runtime.List而不是 string已收到。

我不确定如果不更改 IronPython 源代码或使用正则表达式在 A 中自行包装变量(如 CustomPrinter.Write),这是否可行功能

最佳答案

您可以 PyEngine.Runtime.IO.SetOutput(stream, encoding) 将发送到 stdout 的内容定向到自定义流(另请参见 .SetErrorOutput 用于 stderr.SetInput 用于 stdin)。

关于c# - IronPython:更改 SourceCodeKind.InteractiveCode 的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21737913/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com