我正在编写一个可以使用IronPython编写脚本的C#应用程序.我希望能够从C#代码发出非致命的Python警告(可以使用常规的Python警告捕获系统来处理).
我已经找到了PythonWarnings.warn方法IronPython.Modules,但它需要一个CodeContext,我无法弄清楚如何创建/获取其中之一.
当从IronPython调用.NET(即C#)时,如果被调用的代码选择接收它,则会自动注入CodeContext.假设您有一个程序集,类和方法,如下所示:
namespace ClassLibrary1
{
public class Class1
{
public static void MyMethod(CodeContext ctx, int number, string text)
{
var msg = string.Format("Warning: {0} and {1} ...", number, text);
PythonWarnings.warn(ctx, msg, null, 0);
}
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,MyMethod接受CodeContext作为其第一个参数,并且可以使用提供的最后两个参数从IronPython调用:
import clr
clr.AddReference("ClassLibrary1")
from ClassLibrary1 import Class1
Class1.MyMethod(1234, "test")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
121 次 |
| 最近记录: |