// public methods /// <summary> /// Apply an attribute to these serialization options and modify the options accordingly. /// </summary> /// <param name="serializer">The serializer that these serialization options are for.</param> /// <param name="attribute">The serialization options attribute.</param> public override void ApplyAttribute(IBsonSerializer serializer, Attribute attribute) { EnsureNotFrozen(); var itemSerializer = serializer.GetItemSerializationInfo().Serializer; if (_itemSerializationOptions == null) { var itemDefaultSerializationOptions = itemSerializer.GetDefaultSerializationOptions(); // special case for legacy collections: allow BsonRepresentation on object if (itemDefaultSerializationOptions == null && (serializer.GetType() == typeof(EnumerableSerializer) || serializer.GetType() == typeof(QueueSerializer) || serializer.GetType() == typeof(StackSerializer)) && attribute.GetType() == typeof(BsonRepresentationAttribute)) { itemDefaultSerializationOptions = new RepresentationSerializationOptions(BsonType.Null); // will be modified later by ApplyAttribute } if (itemDefaultSerializationOptions == null) { var message = string.Format( "A serialization options attribute of type {0} cannot be used when the serializer is of type {1} and the item serializer is of type {2}.", BsonUtils.GetFriendlyTypeName(attribute.GetType()), BsonUtils.GetFriendlyTypeName(serializer.GetType()), BsonUtils.GetFriendlyTypeName(itemSerializer.GetType())); throw new NotSupportedException(message); } _itemSerializationOptions = itemDefaultSerializationOptions.Clone(); } _itemSerializationOptions.ApplyAttribute(itemSerializer, attribute); }
// public methods /// <summary> /// Apply an attribute to these serialization options and modify the options accordingly. /// </summary> /// <param name="serializer">The serializer that these serialization options are for.</param> /// <param name="attribute">The serialization options attribute.</param> public override void ApplyAttribute(IBsonSerializer serializer, Attribute attribute) { EnsureNotFrozen(); var dictionaryOptionsAttribute = attribute as BsonDictionaryOptionsAttribute; if (dictionaryOptionsAttribute != null) { _representation = dictionaryOptionsAttribute.Representation; return; } // for backward compatibility reasons representations Array and Document apply to the Dictionary and not the items var representationAttribute = attribute as BsonRepresentationAttribute; if (representationAttribute != null) { switch (representationAttribute.Representation) { case BsonType.Array: _representation = DictionaryRepresentation.ArrayOfArrays; return; case BsonType.Document: _representation = DictionaryRepresentation.Document; return; } } var itemSerializer = serializer.GetItemSerializationInfo().Serializer; if (_itemSerializationOptions == null) { var itemDefaultSerializationOptions = itemSerializer.GetDefaultSerializationOptions(); // special case for legacy dictionaries: allow BsonRepresentation on object if (itemDefaultSerializationOptions == null && serializer.GetType() == typeof(DictionarySerializer) && attribute.GetType() == typeof(BsonRepresentationAttribute)) { itemDefaultSerializationOptions = new RepresentationSerializationOptions(BsonType.Null); // will be modified later by ApplyAttribute } if (itemDefaultSerializationOptions == null) { var message = string.Format( "A serialization options attribute of type {0} cannot be used when the serializer is of type {1} and the item serializer is of type {2}.", BsonUtils.GetFriendlyTypeName(attribute.GetType()), BsonUtils.GetFriendlyTypeName(serializer.GetType()), BsonUtils.GetFriendlyTypeName(itemSerializer.GetType())); throw new NotSupportedException(message); } _itemSerializationOptions = itemDefaultSerializationOptions.Clone(); } _itemSerializationOptions.ApplyAttribute(itemSerializer, attribute); }