一千萬個為什麽

搜索

如何在不必包含FSharp.Core的情況下將字典從F#返回到C#?

I'm trying to return a IDictionary (created with dict tuplist) from F# to C#, but it says that I must include a reference to FSharp.Core because of System.Collections.IStructuralEquatable.

I've tried returning a Dictionary<_,_>(dict tuplist), but that doesn't make any difference.

I even tried Dictionary<_,_>(dict tuplist, HashIdentity.Reference), but that says that int is a struct...

更新</強>

好的,我=愚蠢

我只是忽略了在我的問題中包含關鍵細節:這是我在F#記錄中返回我的字典而 是問題。因為我同時添加了記錄和詞典,並且我看到了IStructuralEquality,我只是假設它是字典就是問題。

衛生署!抱歉...

最佳答案

我認為這應該有效。如果您沒有在公共簽名中公開任何F#特定類型,那麽您不需要在C#項目中引用 FSharp.Core.dll 程序集(您仍需要分發它)但是,使用F#庫)。

我嘗試編寫一個簡單的例子,我可以在不引用 FSharp.Core 的情況下編譯C#項目。如果以下項目適合您,您可以嘗試: http://dl.dropbox.com/u/5676796/test.zip

它聲明了一個簡單的F#模塊:

module Module1
open System.Collections.Generic

let getDictionary() = 
  let d = new Dictionary<_, _>()
  d.[10] <- 1
  d :> IDictionary

從C#引用它的代碼如下所示:

var d = Module1.getDictionary();
Console.WriteLine("d 10 = " + d[10]);

Do you have any other public F# member that would expose some F# specific type (this may include things like F# records & discriminated unions, F# list, etc.)

轉載註明原文: 如何在不必包含FSharp.Core的情況下將字典從F#返回到C#?