2009-08-29 46 views
4

我在庫中創建了一個小的C#類。從IronRuby調用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; 
     } 
    } 
} 

該庫位於

C:\ Documents和Settings \ MYUSER \我的文檔\ Visual Studio 2008的\項目\ PROJECT1 \ helloWorldLib \ BIN \調試\ helloWorldLib.dll

如何你會從IronRuby腳本調用SayHelloWorld嗎?

我知道這看起來很簡單,但我似乎無法在大量研究後找到一致的代碼示例。

非常感謝!

回答

7

首先要注意的是,我不確定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"