To serialize the Guid to string I have no problem since I am using this code : https://stackoverflow.com/a/33258168/4148788
var pack = new ConventionPack { new GuidAsStringRepresentationConvention () };
ConventionRegistry.Register("GuidAsString", pack, t => t == typeof (MyClass));
public class GuidAsStringRepresentationConvention : ConventionBase, IMemberMapConvention
{
public void Apply(BsonMemberMap memberMap)
{
if (memberMap.MemberType == typeof(Guid))
{
var serializer = memberMap.GetSerializer();
var representationConfigurableSerializer = serializer as IRepresentationConfigurable;
if (representationConfigurableSerializer != null)
{
var reconfiguredSerializer = representationConfigurableSerializer.WithRepresentation(BsonType.String);
memberMap.SetSerializer(reconfiguredSerializer);
}
}
}
}
If I try to do the same for the Guid? it doesn't work
if (memberMap.MemberType == typeof(Guid?))
{
var serializer = memberMap.GetSerializer();
var representationConfigurableSerializer = serializer as IRepresentationConfigurable;
if (representationConfigurableSerializer != null)
{
var reconfiguredSerializer = representationConfigurableSerializer.WithRepresentation(BsonType.String);
memberMap.SetSerializer(reconfiguredSerializer);
}
}
This line is always null:
var representationConfigurableSerializer = serializer as IRepresentationConfigurable;
How do you do for nullable Guids?