我有以下课程,我想写给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; }
}
当我尝试拨打&#39; InsertOne()
&#39;在这种类型的实例上,我得到以下ArgumentException
:
GenericArguments [1],&#39; System.Collections.Generic.List {1} [System.Collections.Generic.KeyValuePair {2} [System.String,System.Object]]&#39;, on&#39; MongoDB.Bson.Serialization.Serializers.ImpliedImplementationInterfaceSerializer {2} [TInterface,TImplementation]&#39;违反了类型&#39; TImplementation&#39;。
的约束
例如,如果我将字典值类型转换为字符串,则异常消失,序列化效果很好。但是,如果我使用IExample
的具体实现,我会得到相同的例外
如何获取具有自定义值类型的字典以进行序列化?