C# - 使用 Python 文件中的方法

标签 c# python .net ironpython python.net

我有两个文件,一个常规 python (.py) 文件和一个 Visual Studio C# 文件 (.cs)。我想在 python 文件中使用一个返回字符串的函数,并在 C# 文件中使用该字符串。

基本上:

#This is the Python file!
def pythonString():
   return "Some String"

//This is the C# file!
namespace VideoLearning
{
    string whatever = pythonString(); // This is from the Python file.
}

如何将这两个文件链接在一起?谢谢。

最佳答案

看看 IronPython! :)

良好的声明: https://blogs.msdn.microsoft.com/charlie/2009/10/25/running-ironpython-scripts-from-a-c-4-0-program/

希望这有帮助,这很漂亮;)

var ipy = Python.CreateRuntime();
dynamic test = ipy.UseFile("Test.py");
test.pythonString(); //python-function to execute

只需将“pythonString()”替换为您的函数(已经这样做了)。

关于C# - 使用 Python 文件中的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39820443/

相关文章:

c# - .NET 中 NSOperationQueue 的等价物

c# - 如何为 SQL 查询编写自动化测试?

python - 如何配置 python cffi 库以使用 mingw?

javascript - 在 python 中将多种类型的元组表示为 JSON

c# - 将 C# 代码转换为 PowerShell

c# - 用于远程 Web 服务的 Windows 虚拟磁盘

C#:将设置导入/导出文件

c# - Rx .NET - 强制缓冲区发出

c# - C#中涉及集合、泛型和接口(interface)的设计问题

python - Django 上静态 STATIC_URL 和 STATIC_ROOT 之间的区别