如何使用 MongoDb 的自定义值类型序列化字典?

ket*_*ket 5 c# serialization dictionary mongodb

我有以下类,我想写入 MongoDb 的实例:

public class ChartDefinitionBase
{
    public ChartDefinitionBase(ObjectId id, IDictionary<ISite, IExample> examples)
    {
        ObjectId = id;
        Examples = examples;
    }

    [BsonIgnoreIfDefault]
    [BsonId(IdGenerator = typeof(ObjectIdGenerator))]
    public ObjectId ObjectId { get; private set; }

    [BsonDictionaryOptions(DictionaryRepresentation.ArrayOfDocuments)]
    public IDictionary<ISite, IExample> Examples{ get; private set; }
}
Run Code Online (Sandbox Code Playgroud)

当我尝试InsertOne()在这种类型的实例上调用 ' ' 时,我得到以下信息ArgumentException

GenericArguments[1], 'System.Collections.Generic.List{1}[System.Collections.Generic.KeyValuePair{2}[System.String,System.Object]]',在 'MongoDB.Bson.Serialization.Serializers.ImpliedImplementationInterfaceSerializer {2}[TInterface,TImplementation]' 违反了类型 'TImplementation' 的约束。

例如,如果我将字典值类型转换为字符串,异常就会消失并且序列化效果很好。但是,如果我使用 的具体实现,则会IExample得到相同的异常。
如何获取具有自定义值类型的字典以进行序列化?