调用Ruby或Python API在C#.NET [英] Call Ruby or Python API in C# .NET

查看:186
本文介绍了调用Ruby或Python API在C#.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多的API /我已经开发了Ruby和Python,我想在我的.NET应用程序使用的类的。是否有可能实例化一个Ruby或Python对象在C#中并调用其方法?

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?

看起来像IronPython的图书馆做这样的相反。意思是,他们让Python来使用.NET对象,但是这不是倒数这正是我期待的......我失去了一些东西呢?

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?

任何想法?

推荐答案

这是两件事情之一是,动态语言运行时应该做的事:每个人都认为,德国航空航天中心只对语言实现,使之更易于实现动态语言上的CLI。但是,它也可用于应用程序的作者,以使其更容易承载动态语言在其应用

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.

在德国航空航天中心之前,每一种语言都有自己的托管API。现在,德国航空航天中心有一个标准化托管规范的作品相同的的语言,并在C#4和VB.NET 10动态类型对象的支持,它变得比以往任何时候都更容易:

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

在这里可以看到在每一个可能的方向得到的东西传来传去。在C#code被调用的Ruby对象的一个​​方法的这根本不​​存在的和红宝石code为遍历一个.NET数组并打印其内容到控制台。

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.

这篇关于调用Ruby或Python API在C#.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆