public override object Deserialize(
BsonReader bsonReader,
Type nominalType,
Type actualType,
IBsonSerializationOptions options)
{
VerifyTypes(nominalType, actualType, typeof(IPAddress));
BsonType bsonType = bsonReader.GetCurrentBsonType();
string message;
switch (bsonType)
{
case BsonType.Null:
bsonReader.ReadNull();
return null;
case BsonType.String:
var stringValue = bsonReader.ReadString();
IPAddress address;
if (IPAddress.TryParse(stringValue, out address))
{
return address;
}
message = string.Format("Invalid IPAddress value '{0}'.", stringValue);
throw new Exception(message);
default:
message = string.Format("Cannot deserialize IPAddress from BsonType {0}.", bsonType);
throw new Exception(message);
}
}