将程序集从另一个程序集导入 IronPython

发布于 2024-09-26 12:18:22 字数 624 浏览 0 评论 0原文

我正在编写一个 IronPython 2.6/2.7 脚本,它导入了很多程序集。

换句话说,它在脚本的顶部执行此操作...

clr.AddReference( "System.Xml" )
import System.Xml

只不过它不是针对 1 个程序集执行此操作,而是针对 10 个程序集执行此操作。

有些模块是内置的 .NET 程序集,有些是我制作的程序集。

我想简化我的脚本,以便它加载我将构建的一个程序集。然后我想调用该程序集中的一个方法,该方法将为 10 个程序集执行“AddReference”和“导入”操作。所有这一切的主要目标是最大限度地减少脚本的长度/复杂性。

所以最后我会看到它像这样工作

clr.AddReferenceToFileAndPath( "d:\\myassembly" )
import MyAssembly
MyAssembly.ImportAllAssembliesIReallyWant()

我的核心问题是尽管阅读了我能在 ScriptRuntime、ScriptEngine、范围等上找到的所有信息 - 我仍然不知道如何在“MyAssembly”中编写影响的方法调用脚本中加载了哪些模块。

I have an IronPython 2.6/2.7 script I am writing which imports a lot of assemblies.

In other words, at the top of the script it does this...

clr.AddReference( "System.Xml" )
import System.Xml

Except it doesn't do this for 1 assembly, but for 10 assemblies.

Some of the modules are built-in .NET assembllies and some are assemblies I have made.

I'd like to simplify my script so that it loads one assembly that I will build. I want to then call a method in that assembly that will do the "AddReference" and "import" for the 10 assemblies. The primary goal of all this is to minimize the length/complexity of the script.

So in the end I would see it working like this

clr.AddReferenceToFileAndPath( "d:\\myassembly" )
import MyAssembly
MyAssembly.ImportAllAssembliesIReallyWant()

My core problemis despite reading all the information I could find on ScriptRuntime, ScriptEngine, scopes, etc. - I still can't figure out how to write a method in "MyAssembly" that affects what modules are loaded in the calling script.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

铃予 2024-10-03 12:18:22

解决此问题的一种方法是创建一个执行此操作的内置模块。您可以通过以下方式执行此操作:

[assembly: PythonModule("mymodule", typeof(MyModuleType)]

public static class MyModuleType {
    [SpecialName]
    public static void PerformModuleReload(PythonContext context, PythonDictionary dict) {
         context.DomainManager.LoadAssembly(typeof(TypeInAssemblyToLoad));
    }
}

只需为您关心的所有程序集添加适当的 LoadAssembly 调用即可。该程序集还可以填充您想要可用的字典中的成员。

另一种(可能更简单)的方法是简单地使用一个 .py 文件来执行您需要的所有 clr.AddReference 调用,并让每个模块导入该文件。导入机制将进行适当的缓存,因此它只会加载一次,但将确保所有程序集对于需要它们的每个模块都是可用的。

One way to go about this would be to create a built-in module which does this. You can do this with:

[assembly: PythonModule("mymodule", typeof(MyModuleType)]

public static class MyModuleType {
    [SpecialName]
    public static void PerformModuleReload(PythonContext context, PythonDictionary dict) {
         context.DomainManager.LoadAssembly(typeof(TypeInAssemblyToLoad));
    }
}

Just add appropriate LoadAssembly calls for all of the assemblies you care about. The assembly could also populate members in dict that you want available.

Another (and possibly simpler) way would be to simply have a .py file which does all of the clr.AddReference calls you need and have every module import that one file. The import mechanism will do the appropriate caching so it will only load once but will ensure all of the assemblies are available for each module which needs them.

陌上青苔 2024-10-03 12:18:22

我认为做到这一点的唯一方法是从 ImportAllAssemblies() 方法中访问 ScriptEngine 并执行通常会执行的命令。您应该能够根据要加载的程序集和引用的程序集等动态生成语句。

希望这有助于为您指明正确的方向。

I think that the only way to do this is going to be to access the ScriptEngine from within your ImportAllAssemblies() method and execute the commands that would normally be executed. You should be able to dynamically generate the statements based on what assemblies you want to load and that are referenced etc.

Hope that helps point you in the right direction.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文