c# - 如何在内部使用 IronPython 引用自包含的 C# 类库项目 (Visual Studio 2010)

标签 c# visual-studio-2010 ironpython dynamic-language-runtime

这个问题有点冗长,但我会尽力为您提供我认为找到答案所必需的详细信息。

我有一个 C# WPF 解决方案 (.Net 4),它由一个主项目组成,构建一个 WPF windows 应用程序,它依赖于同一个 Visual Studio 2010 解决方案中的几个类库项目。

其中一个类库项目封装了一些之前开发的python 我想通过 IronPython 和 Microsoft Dynamic Language Runtime 使用的代码。
我希望类库项目是自包含的,而不是依赖于 IronPython 的完整安装。

问题是我不知道如何以始终有效的方式引用包含 python 代码的封装库项目。

通常我会添加一个对类库项目的引用,正如这个问题中所讨论的那样:Visual Studio 2010: How refer to a C# .Net class library project with third part dependencies .然而它并没有帮助。

如何在 Visual Studio 中设置解决方案:

解决方案如下所示:

  • MainApp(windows WPF 应用程序项目)
  • ...
  • ClassLib1(C#类库项目)
  • ...
  • ClassLibWithPython(使用 IronPython 的 C# 类库项目)
  • C# 类
  • 库(目录)
  • IronPython.dll
  • IronPython.Modules.dll
  • Microsoft.Dynamic.dll
  • Microsoft.Scripting.dll
  • Microsoft.Scripting.Metadata.dll
  • pylib(包含一些使用过的 python 模块的目录)
  • os.py
  • ... .py
  • ctypes(包含一些使用过的 python 模块的目录)
  • 我的 pyton 类(目录)

  • ClassLibWithPython 引用了位于其本地 lib 文件夹(Copy Local 属性 True)中的 IronPython DLL。 MainApp 项目引用了 ClassLib1 项目和 ClassLibWithPython 项目(也具有 Copy Local 属性 True)。

    编译解决方案时,所有 DLL 和 MainApp.exe 文件都显示在 MainApp/bin/Debug 中,它在某些机器(XP 和 Win 7)上运行良好,但在其他一些机器(XP)上失败。在做了一些调试后,我发现 内置 IronPython 模块未正确加载 .导入 os 模块(pylib/os.py 像这个 http://pydoc.org/get.cgi/usr/local/lib/python2.5/os.py )时,由于缺少模块名称 ImportError, no os specific module found,我得到一个 python 异常( 'nt' ) .

    在比较它工作的地方和不工作的地方发生的事情时,我发现 sys.builtin_module_names与在其他机器上运行相同代码时得到的相比,只返回一些项目。

    有问题的机器有:
    sys.builtin_module_names = ['clr', 'future_builtins', 'imp', 'sys', '__builtin__', 'exceptions']
    一切正常的计算机具有:
    sys.builtin_module_names: ['clr', 'future_builtins', 'imp', 'sys', '__builtin__', 'exceptions', '_codecs', 'cmath', '_sha512', 'msvcrt', 'array', '_winreg', '_weakref', '_warnings', '_subprocess', '_ssl', '_sre', '_random', '_functools', 'xxsubtype', 'time', 'thread', '_struct', '_heapq', '_ctypes_test', '_ctypes', 'socket', '_sha256', '_sha', 'select', 're', 'operator', 'nt', '_md5', 'math', 'marshal', '_locale', '_io', 'itertools', 'gc', 'errno', 'datetime', 'cStringIO', 'cPickle', 'copy_reg', '_collections', 'binascii', 'zlib', 'signal', 'mmap']
    没有帮助的解决方法

    我尝试添加 using对 ClassLibWithPython 的 C# 代码的语句,以确保即使是隐式引用的程序集也被链接,但没有区别。

    有帮助的解决方法

    我发现了两种解决方法提供了一个可行的解决方案,但是它们都打破了封装原则并暴露了 ClassLibWithPython 的实现细节:
  • 而是将 ClassLibWithPython 中的所有代码放在 MainApp 项目中。
  • 将 ClassLibWithPython 保留在一个单独的项目中,但还将对 IronPython.dll 和 IronPython.Modules.dll 的引用添加到 MainApp 项目中。

  • 是什么使变通方法 #2 起作用?

    任何建议如何以一种干净的方式进行这项工作?

    谢谢你读到这里;-)

    最佳答案

    我不完全了解部署的布局 - 但请尝试以下操作。

    1) 对于您希望从 IronPython.Modules.dll 加载的模块,请确保此程序集在您的部署位置可用,和/或 Hook AssemblyResolve (see here)如果此程序集位于不同的位置,则事件。

    2) 对于您希望从 py 模块加载的模块。确保通过 sys 或从 DLR 托管 API 将探测位置添加到 sys.path。例如sys.path.append(...)

    关于c# - 如何在内部使用 IronPython 引用自包含的 C# 类库项目 (Visual Studio 2010),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7992481/

    相关文章:

    c# - HostingEnvironment.MapPath 返回错误路径

    c# - 使用 MongoDB 和 C# 查询数组中深度嵌套的对象

    c# - 为什么即使在 Azure 中配置所需的设置后,我也看不到应用服务中的日志?

    c# - 在加载项中使用 Visual Studio 主题

    sqlite - 将 sqlite3 与 IronPython 2.6 一起用于 .Net4

    python - 从文本中分割数字

    c# - String.Format 参数空异常

    c# - HTML 文档

    c++ - 使用 Visual Studio 2010 构建 Visual Studio 6 项目

    c# - 使用 IronPython 简化为 C# 应用程序编写的 DSL