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.

2
  • 2
    IronPython should be able to use C# classes as they're all .NET Commented Dec 3, 2010 at 21:55
  • possible duplicate of How to use a C# dll in IronPython Commented Dec 3, 2010 at 21:55

1 Answer 1

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!')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.