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 values
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;
}
}
// any other attributes are applied to the values
var valueType = typeof(object);
if (serializer.GetType().IsGenericType)
{
valueType = serializer.GetType().GetGenericArguments()[1]; // TValue
}
var valueSerializer = BsonSerializer.LookupSerializer(valueType);
var valueSerializationOptions = _keyValuePairSerializationOptions.ValueSerializationOptions;
if (valueSerializationOptions == null)
{
var valueDefaultSerializationOptions = valueSerializer.GetDefaultSerializationOptions();
// special case for legacy dictionaries: allow BsonRepresentation on object
if (valueDefaultSerializationOptions == null &&
serializer.GetType() == typeof(DictionarySerializer) &&
attribute.GetType() == typeof(BsonRepresentationAttribute))
{
valueDefaultSerializationOptions = new RepresentationSerializationOptions(BsonType.Null); // will be modified later by ApplyAttribute
}
if (valueDefaultSerializationOptions == null)
{
var message = string.Format(
"A serialization options attribute of type {0} cannot be used when the serializer is of type {1} and the value serializer is of type {2}.",
BsonUtils.GetFriendlyTypeName(attribute.GetType()),
BsonUtils.GetFriendlyTypeName(serializer.GetType()),
BsonUtils.GetFriendlyTypeName(valueSerializer.GetType()));
throw new NotSupportedException(message);
}
valueSerializationOptions = valueDefaultSerializationOptions.Clone();
}
valueSerializationOptions.ApplyAttribute(valueSerializer, attribute);
_keyValuePairSerializationOptions = new KeyValuePairSerializationOptions(
_keyValuePairSerializationOptions.Representation,
_keyValuePairSerializationOptions.KeySerializationOptions,
valueSerializationOptions);
}