嵌入 IronRuby 到 CLR 主程序的例子
时间:2011-05-29
来源:oldrev
在手机上看
手机扫描阅读
下面的代码实现了 IronRuby 脚本继承 C# 主程序提供的类,并被 C# 调用的例子,对于使用 Iron 语言来做 .NET 程序插件或脚本是一个很好的参考。
using System;
using System.Reflection;
using Microsoft.Scripting;
using Microsoft.Scripting.Hosting;
using IronRuby;
namespace IronRubyTest1
{
public class Human
{
protected string GetInternalCondition()
{
return "all good";
}
}
class Program
{
static void Main(string[] args)
{
const string RubyCode = @"
class Doctor < IronRubyTest1::Human
def check_condition()
puts 'Condition: ' + get_internal_condition()
end
end";
ScriptRuntime runtime = Ruby.CreateRuntime();
ScriptEngine engine = Ruby.GetEngine(runtime);
ScriptScope scope = engine.CreateScope();
//让 DLR 载入包含 Human 类的 Assembly
runtime.LoadAssembly(Assembly.GetExecutingAssembly());
engine.Execute(RubyCode, scope); //执行 Ruby 代码
dynamic globals = engine.Runtime.Globals;
dynamic house = globals.Doctor.@new(); //创建 Doctor 的实例
house.check_condition();
Console.WriteLine("Press any key to close...");
Console.ReadKey();
}
}
}
using System.Reflection;
using Microsoft.Scripting;
using Microsoft.Scripting.Hosting;
using IronRuby;
namespace IronRubyTest1
{
public class Human
{
protected string GetInternalCondition()
{
return "all good";
}
}
class Program
{
static void Main(string[] args)
{
const string RubyCode = @"
class Doctor < IronRubyTest1::Human
def check_condition()
puts 'Condition: ' + get_internal_condition()
end
end";
ScriptRuntime runtime = Ruby.CreateRuntime();
ScriptEngine engine = Ruby.GetEngine(runtime);
ScriptScope scope = engine.CreateScope();
//让 DLR 载入包含 Human 类的 Assembly
runtime.LoadAssembly(Assembly.GetExecutingAssembly());
engine.Execute(RubyCode, scope); //执行 Ruby 代码
dynamic globals = engine.Runtime.Globals;
dynamic house = globals.Doctor.@new(); //创建 Doctor 的实例
house.check_condition();
Console.WriteLine("Press any key to close...");
Console.ReadKey();
}
}
}
代码下载:http://pastebin.com/0tHWuAmH
参考资料
http://www.voidspace.org.uk/ironpython/hosting_api.shtml#id2
《IronRuby In Action》
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28