【问题标题】:using C# DLL in IronRuby在 IronRuby 中使用 C# DLL
【发布时间】:2020-07-25 08:06:56
【问题描述】:

我在 IronRuby 中使用 C# DLL ServiceStack.redis。表单设计师是 SharpDevelop。代码是:

    require 'redis/ServiceStack.Redis' # redis is subfolder in project
    ...
    def Button1Click(sender, e)
          object =  ServiceStack::Redis.PooledRedisClientManager.new
    end

错误信息是:

System.MissingMethodException: undefined method `PooledRedisClientManager' for ServiceStack::Redis:Module

但在 dotPeek 中,我可以看到 C# 代码是:

namespace ServiceStack.Redis
{
  public class PooledRedisClientManager
 {
   ...
    public PooledRedisClientManager()
      : this("localhost")
    {
    }
 }
}

如何导入 C# DLL 并在 IronRuby 中使用?

【问题讨论】:

    标签: c# .net sharpdevelop ironruby


    【解决方案1】:

    好的,问题解决了。

    关键字是:要求路径、名称更改。

    我试图使 C# DLL 文件看起来像:

      namespace testdll
      {
          public class MyClass
          {
              public MyClass()
              {
                  Console.Writeln("run test dll");
              }
          }
      }
    

    DLL 名称为:testdll.dll 在 IronRuby 项目中,我写的代码:

        require "testdll"
        obj = testdll::MyClass.new
    

    失败了。因为模块名的Ruby首字母必须大写。

    所以我将 testdll 命名空间修改为:

    namespace TestDll
    

    那么 IR 中的代码是:

    obj = TestDll::MyClass.new
    

    已经过去了。

    那么我想也许 ServiceStack.Redis 是一个“错误的”模块名称?所以我尝试了:

      namespace Test.Dll # in C#
      ...
      obj = Test.Dll::MyClass.new
    

    失败。 当时我认为我需要将 ServiceStack.Redis 包装成类似的东西:

       namespace Wrap
       {
           public class Redis : ServiceStack.Redis.SomeClass ...
       }
    

    但这太复杂了,我在 IR 中查看原始代码, 我找到了代码:

      require "System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
      require "System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
    

    我们可以使用: 系统::绘图::点... 所以我在dotPeek中查看System.Drawing DLL,发现代码很熟悉,就像ServiceStack.xxx一样

      namespace System.Drawing
    

    所以如果我们可以使用命名空间 System.Drawing 的代码 System::Drawing,为什么我的代码会失败? 我尝试从以下位置更改要求代码:

       require "redis/ServiceStack.redis" to 
       require "ServiceStack.redis"
    

    然后:

       object = ServiceStack::Redis::PooledRedisClientManager.new
    

    .... 过去的 ! 重要的是:

    1. 不需要与路径相关的 C# DLL。
    2. 在 C# DLL 中的 IR 命名空间 A.B 中将模块名称重命名为 A::B

    【讨论】:

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