public static bool TryParse(string value, out BsonObjectID result) { if (String.IsNullOrEmpty(value) || value.Length != 24) { result = BsonObjectID.Empty; return(false); } byte[] bytes = new byte[12]; for (int i = 0; i < 24; i += 2) { byte digit; if (!Byte.TryParse( value.Substring(i, 2), NumberStyles.AllowHexSpecifier, NumberFormatInfo.InvariantInfo, out digit)) { result = BsonObjectID.Empty; return(false); } bytes[i] = digit; } result = new BsonObjectID(bytes); return(true); }
public static BsonObjectID Parse(string value) { BsonObjectID result; if (BsonObjectID.TryParse(value, out result)) { return(result); } throw new InvalidCastException("String must be exactly 24 hex digits"); }