简体   繁体   English

C#脚本(python)

[英]C# scripting (python)

Does a way to interoperate in C# and Python exist? 是否存在在C#Python进行互操作的方法?

I know and have used IronPython (obviously, it became really sweet with the dynamic keyword), and it allows to interpret Python scripts inside a C# application. 我知道并且已经使用过IronPython (显然,它使用dynamic关键字变得非常甜蜜),它允许在C#应用程序中解释Python脚本。

But I want this Python script to access C# classes and methods. 但我希望这个Python脚本能够访问C#类和方法。 I know, for example, this can be implemented using boost::python and C++ , but how should I (if it's possible ) do it with C# ? 我知道,例如,这可以使用boost::pythonC++ ,但是我应该如何(如果可能的话 )使用C#实现它?

Thanks. 谢谢。

Well you could compile your C# code to a DLL using csc and use ctypes to access the DLL from your script but if you can use IronPython, then that's the route I'd go with. 那么你可以使用csc 你的C#代码编译成DLL并使用ctypes从你的脚本访问DLL但是如果你可以使用IronPython,那么这就是我要使用的路径。

Using C# modules from IronPython is as simple as a call to clr.AddReference . 使用IronPython中的C#模块就像调用clr.AddReference一样简单。 For example, here's a script to make your computer talk to you (taken from the IronPython Cookbook ) 例如,这是一个让您的计算机与您通话的脚本(取自IronPython Cookbook

import clr
clr.AddReference('System.Speech')
from System.Speech.Synthesis import SpeechSynthesizer

spk = SpeechSynthesizer()
spk.Speak('Hello world!')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM