C#编程中,业务部分由IronRuby来实现是一个不错的选择。
1. IDE: SharpDevelop http://www.icsharpcode.net/opensource/sd/
2. IronRuby: http://ironruby.codeplex.com/releases
下载 IronRuby 1.1.1 Binaries
即可
3. 新建C#项目,然后引用 IronRuby Binaries下bin目录的DLL:
IronRuby.dll
IronRuby.Libraries.dll
IronRuby.Libraries.Yaml.dll
Microsoft.Dynamic.dll
Microsoft.Scripting.dll
Microsoft.Scripting.Metadata.dll
另外需要引用库 Microsoft.CSharp
4. 简单例子
在项目根目录下创建一个service
目录放Ruby文件,当然目录你可以自己取名,并且修改相应的地方。
service
/apple.rb
class Apple
def say
"Hello MBP!"
end
end
C#
using System;
using Microsoft.Scripting;
using Microsoft.Scripting.Hosting;
using IronRuby;
namespace SharpDLR
{
class Program
{
public static void Main(string[] args)
{
var engine = Ruby.CreateEngine();
engine.ExecuteFile("service/apple.rb");
dynamic ruby = engine.Runtime.Globals;
dynamic apple = ruby.Apple.@new();
Console.WriteLine("Ruby :"+apple.say());
Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
}
}
}
5. 由于编译项目时不会自动复制Ruby文件到bin/Debug目录,所以我们要写一个编译事件。
项目配置:在编译事件(Build Events)的Pre-build event 或者 Post-build event 中加入以下命令
mkdir "$(ProjectDir)$(OutDir)service
/"
copy "$(ProjectDir)service
/" "$(ProjectDir)$(OutDir)service
/" |
6. 编译运行吧
7. 本文只是起到一个入门的作用,可以写一个Ruby初始化文件,载入整个业务所需要的Ruby类。例:
bootstrap.rb
load 'service/product.rb'
load 'service/customer.rb'
load 'service/orderitem.rb'
load 'service/order.rb'
相应的C#调用为
var engine = Ruby.CreateEngine();
engine.ExecuteFile("service/bootstrap.rb");
dynamic ruby = engine.Runtime.Globals;
dynamic customer = ruby.Customer.@new("Davy Brion", "davy@gmail.com");
dynamic product1 = ruby.Product.@new("product1", 50);
dynamic product2 = ruby.Product.@new("product2", 60);
dynamic order = ruby.Order.@new(customer, null, DateTime.Now);
order.add_item(ruby.OrderItem.@new(product1, 5));
order.add_item(ruby.OrderItem.@new(product2, 5));
...
开始享受混合编程的乐趣吧!
分享到:
相关推荐
IronRuby 是 Ruby 编程语言在微软 .NET 平台上的一种实现。 IronRuby 与 Wilco Bauwer 的 IronRuby project 同名,2007年4月30日由微软于 MIX 上发表。并计划于2007年的 OSCON 公开推出。 2007年8月31日,John Lam...
NULL 博文链接:https://raylinn.iteye.com/blog/670006
IronRuby博客中文版_多用户博客空间源码下载
Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, Ironpython,对JSON、Web Service、WCF以及Sockets的支持等一系列新的特性。...
Silverlight 3.0支持框架语言Visual Basic, Visual C#, IronRuby, Ironpython,对JSON、Web Service、WCF以及Sockets的支持等一系列新的特性
[博客空间]IronRuby博客中文版 Alpha_ironruby-pre-alpha1(ASP.NET源码).rar
Sams.IronRuby.Unleashed.Feb.2010.pdf
Sams.IronRuby.Unleashed.Feb.2010.rar
Silverlight 2 Beta 1版本发布了无论从Runtime还是Tools都给我们带来了很多惊喜如支持框架语言Visual Basic, Visual C#, IronRuby, Ironpython对JSON、Web Service、WCF以及Sockets支持等系列新特性步步学...
ASP.NET源码——[博客空间]IronRuby博客中文版 Alpha.zip
Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, Ironpython,对JSON、Web Service、WCF以及Sockets的支持等一系列新的特性。...
[博客空间]IronRuby博客中文版 Alpha_ironruby-pre-alpha1.zip源码ASP.NET网站源码打包[博客空间]IronRuby博客中文版 Alpha_ironruby-pre-alpha1.zip源码ASP.NET网站源码打包[博客空间]IronRuby博客中文版 Alpha_...
1.支持(面向)该平台的编程语言(如C#、Visual Basic、C++/CLI、F#、IronPython、IronRuby…), 2.用于该平台下开发人员的技术框架体系(.NET Framework、.NET Core、Mono、UWP等), 1.定义了通用类型系统,庞大的CTS...
Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, Ironpython,对JSON、Web Service、WCF以及Sockets的支持等一系列新的特性。...
免责声明:资料部分来源于合法的互联网渠道收集和整理,部分自己学习积累成果,供大家学习参考与交流。收取的费用仅用于收集和整理资料耗费时间的酬劳。 本人尊重原创作者或出版方,资料版权归原作者或出版方所有,...
Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, Ironpython,对JSON、Web Service、WCF以及Sockets的支持等一系列新的特性。...
Ruby语言在.net平台的实现。alpha版源代码,最初阶段代码,可能比较粗糙,但是对于初学者确实比较容易入门。
另请访问官网 nehulsinghide.blogspot.in 使用包括 C#、VB.NET、F#、IronPython 和 IronRuby 等语言编写应用程序,以及目标丰富和范围:Windows Forms 或 WPF,以及 ASP.NET MVC 和 WCF。 它从 USB 驱动器开始,支持...