如何在不包含 FSharp.Core 的情况下将字典从 F# 返回到 C#?
我试图从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为这应该有效。如果您不在公共签名中公开任何 F# 特定类型,那么您不需要在 C# 项目中引用
FSharp.Core.dll
程序集(您仍然需要分发它)不过,使用 F# 库)。我尝试编写一个简单的示例,并且可以在不引用
FSharp.Core
的情况下编译 C# 项目。您可以尝试以下项目是否适合您:http://dl.dropbox.com/u/5676796/test.zip ?
它声明了一个简单的 F# 模块:
从 C# 引用它的代码如下所示:
您是否有任何其他公共 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:
And the code that references it from C# looks like this:
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.)
(我尚未验证这一点,但我怀疑...)
您正在调用
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 ofIDictionary
that requires FSharp.Core. If instead, you useDictionary
as the concrete implementation, it will work. You can probably even donew Dictionary<_,_>(dict tupleList)
and it will be ok, since that will copy the F# structure into a .NET Dictionary.