如何在不包含 FSharp.Core 的情况下将字典从 F# 返回到 C#?

发布于 2024-08-29 12:21:39 字数 596 浏览 9 评论 0原文

我试图从 F# 返回一个 IDictionary (使用 dict tuplist 创建)到 C#,但它说我必须包含对 的引用>FSharp.Core 因为 System.Collections.IStructuralEquatable

我尝试返回一个 Dictionary<_,_>(dict tuplist),但这没有任何区别。

我什至尝试过 Dictionary<_,_>(dict tuplist, HashIdentity.Reference),但这表明 int 是一个结构...

UPDATE

好吧,ME = STUPID

我只是忽略了在我的问题中包含关键细节:即我在 F# 记录中返回我的字典,而就是问题所在。因为我同时添加了记录和字典,并且我看到了 IStructuralEquality,所以我只是假设问题出在字典上。

哎哟!对不起...

I'm trying to return a IDictionary<int,int> (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...

UPDATE

OK, ME = STUPID

I just omitted to include in my question the crucial detail: which is that I was returning my dictionary in a F# record and that is the problem. Because I added the record and the Dictionary at the same time, and I saw the IStructuralEquality, I just assumed it was the dictionary which was the problem.

Doh! Sorry...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风渺 2024-09-05 12:21:39

我认为这应该有效。如果您不在公共签名中公开任何 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<int, int>

从 C# 引用它的代码如下所示:

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

您是否有任何其他公共 F# 成员可以公开某些 F# 特定类型(这可能包括 F# 记录和可区分联合、F# 列表等内容) ETC。)

I think this should work. If you don't expose any F# specific type in the public signature, than you shouldn't need to reference the FSharp.Core.dll assembly in your C# project (you'll still need to distribute it with the F# library, though).

I tried writing a simple example and I can compile the C# project without referencing FSharp.Core. Could you try if the following project works for you:
http://dl.dropbox.com/u/5676796/test.zip ?

It declares a simple F# module:

module Module1
open System.Collections.Generic

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

And the code that references it from C# looks like this:

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.)

全部不再 2024-09-05 12:21:39

(我尚未验证这一点,但我怀疑...)

您正在调用 dict,这将创建需要 FSharp.Core 的 IDictionary 的 F# 特定实现。相反,如果您使用 Dictionary 作为具体实现,它将起作用。您甚至可以执行 new Dictionary<_,_>(dict tupleList) ,这样就可以了,因为这会将 F# 结构复制到 .NET 字典中。

(I have not verified this, but I suspect...)

You are calling dict, which creates an F#-specific implementation of IDictionary that requires FSharp.Core. If instead, you use Dictionary as the concrete implementation, it will work. You can probably even do new Dictionary<_,_>(dict tupleList) and it will be ok, since that will copy the F# structure into a .NET Dictionary.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文