簡體   English   中英

在 IronRuby 中使用 C# DLL

[英]using C# DLL in IronRuby

我在 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 中使用?

OK 問題解決了。

關鍵字是:需要路徑,名稱更改。

我試圖使 C# DLL 文件看起來像:

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

並且 IronRuby 項目中的 DLL 名稱是:testdll.dll,我寫的代碼:

    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"

我們可以使用: System::Drawing::Point... 所以我在 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 命名空間 AB 中,將模塊名稱重命名為 A::B

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM