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



所以我有一个简单的对象

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

当我将其插入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.

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

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

    member val Refs : ResizeArray<string> ...

最新更新