8

I have a lot of APIs/Classes that I have developed in Ruby and Python that I would like to use in my .NET apps. Is it possible to instantiate a Ruby or Python Object in C# and call its methods?

It seems that libraries like IronPython do the opposite of this. Meaning, they allow Python to utilize .NET objects, but not the reciprocal of this which is what I am looking for... Am I missing something here?

Any ideas?

robotshapes
  • 779
  • 2
  • 10
  • 13
  • You don't see the reverse because it's typically not done in reverse. I would state that the same situation occurs with Python/C and Jython/Java. – Min Nov 05 '09 at 22:57
  • @Min, C and Python works both ways. In fact my introduction to Python was embedding it in a C app. – John La Rooy Nov 05 '09 at 23:11

4 Answers4

9

This is one of the two things that the Dynamic Language Runtime is supposed to do: everybody thinks that the DLR is only for language implementors to make it easier to implement dynamic languages on the CLI. But, it is also for application writers, to make it easier to host dynamic languages in their applications.

Before the DLR, every language had their own hosting API. Now, the DLR has a standardized hosting specification that works the same for every language, and with support for dynamically typed objects in C# 4 and VB.NET 10, it gets easier than ever:

// MethodMissingDemo.cs
using System;
using IronRuby;

class Program
{
    static void Main()
    {
        var rubyEngine = Ruby.CreateEngine();
        rubyEngine.ExecuteFile("method_missing_demo.rb");
        dynamic globals = rubyEngine.Runtime.Globals;

        dynamic methodMissingDemo = globals.MethodMissingDemo.@new();

        Console.WriteLine(methodMissingDemo.HelloDynamicWorld());

        methodMissingDemo.print_all(args);
    }
}

# method_missing_demo.rb
class MethodMissingDemo
  def print_all(args)
    args.map {|arg| puts arg}
  end

  def method_missing(name, *args)
    name.to_s.gsub(/([[:lower:]\d])([[:upper:]])/,'\1 \2')
  end
end

Here you see stuff getting passed around in every possible direction. The C# code is calling a method on the Ruby object which doesn't even exist and the Ruby code is iterating over a .NET array and printing its contents to the console.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653
3

If you can wait for C# 4.0 (you can use the beta right now), it will come with the "dynamic" keyword, and you can call IronRuby or IronPython code as described here.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Brian Sullivan
  • 27,513
  • 23
  • 77
  • 91
1

Both IronRuby and IronPython allow you to call Ruby and Python native modules, functions and classes. Both are supported as more or less first class languages in .NET, specifically under the DLR (Dynamic Language Runtime).

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Soviut
  • 88,194
  • 49
  • 192
  • 260
-1

I have seen ways to call into Ruby / Python from c#. But it's easier the other way around.

Nestor
  • 13,706
  • 11
  • 78
  • 119