从IronRuby调用C#

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?

我知道这似乎非常简单,但是经过大量的研究,我似乎找不到一致的代码示例。

非常感谢!

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"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接