在C#中调用Ruby代码
2009-08-24 09:33
435 查看
Here is some code that we put together for the ASP.NET MVC team. They used it to prototype their IronRuby integration that we showed at Tech Ed 2008. Here's how you can execute a simple file:
view plaincopy to clipboardprint?
using Microsoft.Scripting.Hosting;
var runtime = ScriptRuntime.Create();
runtime.ExecuteFile("MyController.rb")
Where MyController.rb contains:
view plaincopy to clipboardprint?
class MyController
def do_foo a, b
puts a, b
end
end
This will define the MyController class and the do_foo method. Here's some code that instantiates the controller and retrieves the action method:
view plaincopy to clipboardprint?
var engine = runtime.GetEngine("Ruby");
// TODO: should check that the values are identifiers
var code = String.Format("{0}.new.method :{1}", "MyController", "do_foo");
var action = engine.CreateScriptSourceFromString(code).Execute();
The action variable now holds on do_foo method bound to the controller instance. You can invoke it by:
view plaincopy to clipboardprint?
var result = engine.Operations.Call(action, 1, 2);
The definitive reference is the DLR hosting specification.
Retrieved from "http://www.ironruby.net/Documentation/.NET/Hosting"
view plaincopy to clipboardprint?
using Microsoft.Scripting.Hosting;
var runtime = ScriptRuntime.Create();
runtime.ExecuteFile("MyController.rb")
using Microsoft.Scripting.Hosting; var runtime = ScriptRuntime.Create(); runtime.ExecuteFile("MyController.rb")
Where MyController.rb contains:
view plaincopy to clipboardprint?
class MyController
def do_foo a, b
puts a, b
end
end
class MyController def do_foo a, b puts a, b end end
This will define the MyController class and the do_foo method. Here's some code that instantiates the controller and retrieves the action method:
view plaincopy to clipboardprint?
var engine = runtime.GetEngine("Ruby");
// TODO: should check that the values are identifiers
var code = String.Format("{0}.new.method :{1}", "MyController", "do_foo");
var action = engine.CreateScriptSourceFromString(code).Execute();
var engine = runtime.GetEngine("Ruby"); // TODO: should check that the values are identifiers var code = String.Format("{0}.new.method :{1}", "MyController", "do_foo"); var action = engine.CreateScriptSourceFromString(code).Execute();
The action variable now holds on do_foo method bound to the controller instance. You can invoke it by:
view plaincopy to clipboardprint?
var result = engine.Operations.Call(action, 1, 2);
var result = engine.Operations.Call(action, 1, 2);
The definitive reference is the DLR hosting specification.
Retrieved from "http://www.ironruby.net/Documentation/.NET/Hosting"
相关文章推荐
- IronRuby初探——在C#中调用Ruby代码
- C#前台js调用后台代码
- C# DllImport 系统调用使用详解 托管代码的介绍 EntryPoint的使用
- C#调用FFMPEG,并异步读取输出信息的代码
- .NET连接SAP系统专题:C#调用RFC代码(三)
- C#通过COM组件调用C++的代码
- C#代码与javaScript函数的相互调用
- .net中C#代码与javaScript函数的相互调用
- Unity中C#和Java的相互调用实例代码
- C#代码与javaScript函数的相互调用
- C#代码与javaScript函数的相互调用
- C#代码与javaScript函数的相互调用
- 【转】C#代码与javaScript函数的相互调用
- C#代码调用js函数,js函数中的document.getElementById("对象ID")得null值解决办法
- 图解C#中实现远程调用(Remoting)的示例代码(一)
- C#调用摄像头代码
- C#中调用PowerShell代码
- C#代码与javaScript函数的相互调用
- 转:C# .NET中调用VB编写的DLL代码事例(我做了些修改)
- C++ 通过DLL调用C#代码