在 C# Web 应用程序中嵌入 IronPython 2 的问题
首先一些背景知识(如果有帮助):
我的应用程序是一个基于 Web 的框架,最近升级到 .Net Framework v3.5,但不使用母版页/用户控件系统。 它更类似于 MVC 模式(尽管更旧),并从模板的响应流中输出纯 HTML。 Python 表达式允许实现一些规则和模板变体。
旧方法
当在 C# 中嵌入 IronPython 1.x 引擎时,我们能够执行以下代码:
PythonEngine pe = new PythonEngine();
Assembly a = Assembly.LoadFile("path to assembly");
pe.LoadAssembly(a);
pe.Import("Script");
ipy 2.0 中没有 Import() 方法,并且 ImportModule() 方法似乎也没有以同样的方式工作。 Import() 减少了在我们编写的每个 python 脚本中添加一行的需要,例如:
from MyAssembly import MyClass
MyClass 充满了静态方法,这意味着对 MyClass.MyMethod() 的调用工作得非常好。 我不能只是实例化一个对象并将其分配给范围内的变量,因为包含 MyClass 的程序集是在运行时动态加载的。
现在讨论问题
我已经整理了 IronPython 2.0 集成的所有其他部分,但不希望要求我的实现者在他们编写的每个脚本的顶部键入“from MyAssembly import MyClass”(只是看起来很愚蠢,因为在 ipy 1.x 中没有必要)并且也可能在一段时间内成为一个支持问题。
最后是问题
有人遇到过这个问题并解决了吗? 我是否以错误的方式为 DLR 做事? 还是我遗漏了一些明显的东西?
我不确定有人提供帮助所需的详细信息,但我希望这已经足够了。
First Some Background (incase it helps):
My application is a Web-based framework recently upgraded to v3.5 of the .Net Framework but does not use a Master Pages / User Controls system. It's more akin to the MVC pattern (although much older) and outputs pure HTML down the response stream from Templates. The Python expressions allow some rules and template variations to be achieved.
The old way
When embedding the IronPython 1.x engine in C#, we were able to do code such as:
PythonEngine pe = new PythonEngine();
Assembly a = Assembly.LoadFile("path to assembly");
pe.LoadAssembly(a);
pe.Import("Script");
there is no Import() method in ipy 2.0 and the ImportModule() method doesn't seem to work the same way. The Import() alleviated the need to put a line in every python script we write, such as:
from MyAssembly import MyClass
the fact that MyClass is full of static methods, means that calls to MyClass.MyMethod() work really well. I can't just instansiate an object and assign it to a variable in scope as the assembly that MyClass is contained in is dynamically loaded at runtime.
Now to the issue
I have sorted out all the other parts of the integration of IronPython 2.0 but would prefer not to require my implementers to type "from MyAssembly import MyClass" at the top of every script they write (just seems silly when it was not necessary in ipy 1.x) and likely to be a support issue for a while too.
And finally the question
Has anyone had this issue and resolved it? Am I doing things the wrong way for the DLR? or am I missing something obvious?
I'm not sure of the detail required for someone to help, but I hope this is enough.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
加载程序集后,您可以在用于运行脚本的
Scope
中执行导入:我们在我们的产品中执行类似的操作。
(希望这是有效的 C# - 我实际上在 IronPython 本身中尝试过它,因为它更方便。)
Once the assembly's loaded, you can execute the import in the
Scope
you're using to run the script:We do something similar in our product.
(Hopefully this is valid C# - I actually tried it in IronPython itself, because it was more convenient.)
谢谢 Wilberforce,
最后我做了以下事情:
这在 C# 中创建了我的类的对象,然后将其作为变量传递到 IronPython 作用域。
请注意,这是在 C# 范围 (AppDomain) 中创建对象并将其传递给 IronPython。 这似乎(到目前为止)适用于我的问题,因为我传递的对象仅充满静态方法,但可能不适用于具有状态的类。
Thanks Wilberforce,
In the end I did the following:
This made an object of my class in C# and then passed it to the IronPython scope as a variable.
Note, that this is creating the object in the C# scope (AppDomain) and just passing it to the IronPython. This seems (so far) to work for my problem because the object I am passing is full of only static methods but may not work for a class with state.