4

我在库中创建了一个小型 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 and Settings\myUser\My Documents\Visual Studio 2008\Projects\Project1\helloWorldLib\bin\Debug\helloWorldLib.dll

如何从 IronRuby 脚本调用 SayHelloWorld?

我知道这看起来很简单,但经过大量研究,我似乎找不到一致的代码示例。

非常感谢!

4

1 回答 1

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"
于 2009-08-29T13:23:04.383 回答