F# return ICollection

后端 未结 1 1124
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-14 08:06

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.

One piece of code

相关标签:
1条回答
  • 2021-01-14 08:26

    F# doesn't do implicit casting like C#. So even though System.Collections.Generic.List<'T> implements the ICollection interface, you can't directly set some ICollection-typed property to an instance of System.Collections.Generic.List<'T>.

    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)
    

    or

    this.field.Collection1 = (ResizeArray<Recoveries>() :> ICollection)
    
    0 讨论(0)
提交回复
热议问题