我在库中创建了一个小的C#类.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace helloWorldLib
{
public class Greeter
{
public string SayHelloWorld(string name)
{
return "Hello world " + name;
}
}
}
Run Code Online (Sandbox Code Playgroud)
图书馆位于
C:\ Documents and Settings\myUser\My Documents\Visual Studio 2008\Projects\Project1\helloWorldLib\bin\Debug\helloWorldLib.dll
你会如何从IronRuby脚本中调用SayHelloWorld?
我知道这看起来很简单,但经过大量研究后我似乎无法找到一致的代码示例.
非常感谢!
首先要注意的是,我不确定IronRuby将如何处理以小写字母开头的命名空间.如果我没记错的话,你的命名空间将被忽略,但我不确定.在Ruby语言中,模块(相当于C#名称空间)必须以大写字母开头.
将命名空间更改为以大写字母 - HelloWorldLib开头后,可以使用require或load_assembly来加载程序集.
require只会加载一次程序集(即使多次需要dll),load_assembly会在每次调用时重新加载程序集.
此代码将运行您的代码段:
require 'C:\Documents and Settings\myUser\My Documents\Visual Studio 2008\Projects\Project1\helloWorldLib\bin\Debug\helloWorldLib.dll'
greeter = HelloWorldLib::Greeter.new
greeter.say_hello_world "Michael"
Run Code Online (Sandbox Code Playgroud)