在C#.NET中调用Ruby或Python API

时间:2023-01-29 20:46:14

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?

我有许多我在Ruby和Python中开发的API / Classes,我想在我的.NET应用程序中使用它们。是否可以在C#中实例化Ruby或Python对象并调用其方法?

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?

似乎像IronPython这样的图书馆就是这样做的。这意味着,他们允许Python使用.NET对象,但不是我正在寻找的这个的倒数...我在这里遗漏了什么?

Any ideas?

4 个解决方案

#1


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.

这是动态语言运行时应该做的两件事之一:每个人都认为DLR仅适用于语言实现者,以便更容易在CLI上实现动态语言。但是,它也适用于应用程序编写者,以便更容易在其应用程序中托管动态语言。

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:

在DLR之前,每种语言都有自己的托管API。现在,DLR有一个标准化的托管规范,对每种语言都是一样的,并且在C#4和VB.NET 10中支持动态类型对象,它比以往更容易:

// 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.

在这里你可以看到在各个方向传递的东西。 C#代码在Ruby对象上调用一个甚至不存在的方法,Ruby代码在.NET数组上迭代并将其内容打印到控制台。

#2


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.

如果您可以等待C#4.0(您现在可以使用测试版),它将带有“dynamic”关键字,您可以按照此处的说明调用IronRuby或IronPython代码。

#3


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).

IronRuby和IronPython都允许您调用Ruby和Python本机模块,函数和类。两者都支持.NET中的或多或少的一流语言,特别是在DLR(动态语言运行时)下。

#4


-1  

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

我已经看到了从c#调用Ruby / Python的方法。但反过来说更容易。

#1


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.

这是动态语言运行时应该做的两件事之一:每个人都认为DLR仅适用于语言实现者,以便更容易在CLI上实现动态语言。但是,它也适用于应用程序编写者,以便更容易在其应用程序中托管动态语言。

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:

在DLR之前,每种语言都有自己的托管API。现在,DLR有一个标准化的托管规范,对每种语言都是一样的,并且在C#4和VB.NET 10中支持动态类型对象,它比以往更容易:

// 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.

在这里你可以看到在各个方向传递的东西。 C#代码在Ruby对象上调用一个甚至不存在的方法,Ruby代码在.NET数组上迭代并将其内容打印到控制台。

#2


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.

如果您可以等待C#4.0(您现在可以使用测试版),它将带有“dynamic”关键字,您可以按照此处的说明调用IronRuby或IronPython代码。

#3


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).

IronRuby和IronPython都允许您调用Ruby和Python本机模块,函数和类。两者都支持.NET中的或多或少的一流语言,特别是在DLR(动态语言运行时)下。

#4


-1  

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

我已经看到了从c#调用Ruby / Python的方法。但反过来说更容易。