MongoDb 反序列化 F# 列表时出错

Des*_*ino 3 serialization f# mongodb

所以我有一个简单的对象

type DbObject() = 
    member val Id = ObjectId.GenerateNewId().ToString() with get, set
    member val Name = "" with get, set
type Item() =
    inherit DbObject()
    member val Description = "" with get, set
    member val Refs : list<string> = [] with get, set
Run Code Online (Sandbox Code Playgroud)

当我将它插入到我的 MongoDB 数据库中时,这工作得很好,但是每当我尝试接收它时,我都会收到以下错误。

System.FormatException: An error occurred while deserializing the Material property of class Item: Type 'Microsoft.FSharp.Collections.FSharpList`1[[System.String, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]' does not have a suitable constructor or Add method. ---> MongoDB.Bson.BsonSerializationException: Type 'Microsoft.FSharp.Collections.FSharpList`1[[System.String, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]' does not have a suitable constructor or Add method.
Run Code Online (Sandbox Code Playgroud)

我猜问题是 FSharpList 没有反序列化器,但是它为什么可以很好地序列化,它不应该双向工作吗?是使自定义序列化程序起作用的唯一方法吗?

Fyo*_*kin 5

F# 列表是不可变的单向链接列表,但根据错误消息判断,该库希望找到一个带有Add方法的可变列表。为了满足这一点,只需使用 a ResizeArray- 它是 F# 的同义词System.Collections.Generic.List<_>

    member val Refs : ResizeArray<string> ...
Run Code Online (Sandbox Code Playgroud)