private void VerifySerializeTypes(Type nominalType, Type actualType)
{
// problem is that the actual type of a nullable type that is not null will appear as the non-nullable type
// we want to allow both Enum and Nullable<Enum> here
bool isNullableOfActual = (nominalType.IsGenericType &&
nominalType.GetGenericTypeDefinition() == typeof(Nullable<>) &&
nominalType.GetGenericArguments().Single() == actualType);
if (nominalType != typeof(object) && nominalType != actualType && !isNullableOfActual)
{
var message = string.Format("EnumSerializer.Serialize cannot be used with nominal type {0}.", nominalType.FullName);
throw new BsonSerializationException(message);
}
if (!actualType.IsEnum)
{
var message = string.Format("EnumSerializer.Serialize cannot be used with actual type {0}.", actualType.FullName);
throw new BsonSerializationException(message);
}
}
}