【问题标题】:Calling C# from IronRuby从 IronRuby 调用 C#
【发布时间】:2009-08-29 12:46:05
【问题描述】:

我在库中创建了一个小型 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?

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

非常感谢!

【问题讨论】:

    标签: c# ironruby


    【解决方案1】:

    首先要注意的是,我不确定 IronRuby 将如何处理以小写字母开头的命名空间。如果我没记错的话,您的命名空间将被忽略,但我不确定。 在 Ruby 语言中,模块(相当于 C# 命名空间)必须以大写字母开头。

    将命名空间更改为以大写字母开头 - HelloWorldLib 后,您可以使用 require 或 load_assembly 来加载程序集。

    require 只会加载一次程序集(即使需要多次调用 dll),并且 load_assembly 会在每次调用时重新加载程序集。

    此代码将运行您的 sn-p:

    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"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-10
      • 1970-01-01
      • 1970-01-01
      • 2017-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多