简体   繁体   中英

C# scripting (python)

Does a way to interoperate in C# and Python exist?

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.

But I want this Python script to access C# classes and methods. 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# ?

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.

Using C# modules from IronPython is as simple as a call to clr.AddReference . For example, here's a script to make your computer talk to you (taken from the IronPython Cookbook )

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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