public static bool TryParse(string representation, out Ratio?value) { if (representation is null) { throw new ArgumentNullException(nameof(representation)); } value = null; // Not too sure if quantities cannot contain colons themselves, but I have // no time to worry about that now. var components = representation.Split(':'); if (components.Length != 2) { return(false); } if (!Quantity.TryParse(components[0].Trim(), out var numerator)) { return(false); } if (!Quantity.TryParse(components[1].Trim(), out var denumerator)) { return(false); } value = new Ratio(numerator, denumerator); return(true); }
public static bool TryParse(string value, Type primitiveType, out object?parsed) { if (value == null) { throw new ArgumentNullException(nameof(value)); } if (!typeof(Any).IsAssignableFrom(primitiveType)) { throw new ArgumentException($"Must be a subclass of {nameof(Any)}.", nameof(primitiveType)); } bool success; (success, parsed) = parse(); return(success); (bool success, object?p) parse() { if (primitiveType == typeof(Boolean)) { return(Boolean.TryParse(value, out var p), p?.Value); } else if (primitiveType == typeof(Code)) { return(Code.TryParse(value, out var p), p); } else if (primitiveType == typeof(Concept)) { return(Concept.TryParse(value, out var p), p); } else if (primitiveType == typeof(Decimal)) { return(Decimal.TryParse(value, out var p), p?.Value); } else if (primitiveType == typeof(Integer)) { return(Integer.TryParse(value, out var p), p?.Value); } else if (primitiveType == typeof(Long)) { return(Long.TryParse(value, out var p), p?.Value); } else if (primitiveType == typeof(Date)) { return(Date.TryParse(value, out var p), p); } else if (primitiveType == typeof(DateTime)) { return(DateTime.TryParse(value, out var p), p); } else if (primitiveType == typeof(Time)) { return(Time.TryParse(value, out var p), p); } else if (primitiveType == typeof(Ratio)) { return(Ratio.TryParse(value, out var p), p); } else if (primitiveType == typeof(Quantity)) { return(Quantity.TryParse(value, out var p), p); } else if (primitiveType == typeof(String)) { return(String.TryParse(value, out var p), p?.Value); } else { return(false, null); } } }