4

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.

Yippie-Ki-Yay
  • 22,026
  • 26
  • 90
  • 148

1 Answers1

6

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!')
Community
  • 1
  • 1
Aphex
  • 7,390
  • 5
  • 33
  • 54