本文介绍了F#返回ICollection的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C#创建的库.我一直在努力将一些代码移植到F#,但必须使用C#库中的许多基础类型.

I'm working with a library created in C#. I've been working on porting some code to F# but must use quite a few underlying types from the C# lib.

一段代码需要计算一个值列表,并将其分配给该类中的公共字段/属性.该字段是一个包含两个ICollection的C#类.

One piece of code needs to calculate a list of values and assign it to a public field/property in the class. The field is a C# class that contains two ICollection.

我的F#代码工作正常,需要返回一个F#Seq/List.

My F# code works fine and needs to return an F# Seq/List.

我尝试了以下每个都会产生错误的代码段.

I tried the following code snippets which each produce errors.

  • F#成员的返回类型是具有恢复列表类型的称为recoveryList的类型
  • 类中的公共字段,它是一个类本身包含两个ICollection对象

  • Return type of F# member is a type called recoveryList with type Recoveries list
  • Public field in class that is a class itself containing two ICollection objects


The fix is easy though -- all you need to do is add an explicit upcast to ICollection to your ResizeArray<'T> or System.Collections.Generic.List<'T> before assigning it:

// Make sure to add an 'open' declaration for System.Collections.Generic
this.field.Collection1 = (recoveries :> ICollection)

this.field.Collection1 = (ResizeArray<Recoveries>() :> ICollection)

这篇关于F#返回ICollection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 17:35