/// <summary> /// Parses the specified object into the specified type. /// </summary> /// <param name="type">The type to parse into.</param> /// <param name="o">The object to parse.</param> /// <returns>An object of the specified type, if successful; otherwise, null.</returns> public static object Parse(Type type, object o) { var text = o?.ToString(); return(string.IsNullOrWhiteSpace(text) ? default(object) : type.Name switch { "UInt16" when ushort.TryParse(text, NumberStyles.Any, CultureInfo.InvariantCulture, out var v2) => v2, "UInt32" when uint.TryParse(text, NumberStyles.Any, CultureInfo.InvariantCulture, out var v2) => v2, "UInt64" when ulong.TryParse(text, NumberStyles.Any, CultureInfo.InvariantCulture, out var v2) => v2, "Int16" when short.TryParse(text, NumberStyles.Any, CultureInfo.InvariantCulture, out var v2) => v2, "Int32" when int.TryParse(text, NumberStyles.Any, CultureInfo.InvariantCulture, out var v2) => v2, "Int64" when long.TryParse(text, NumberStyles.Any, CultureInfo.InvariantCulture, out var v2) => v2, "Byte" when byte.TryParse(text, NumberStyles.Any, CultureInfo.InvariantCulture, out var v2) => v2, "Decimal" when decimal.TryParse(text, NumberStyles.Any, CultureInfo.InvariantCulture, out var v2) => v2, "DateTime" when DateTime.TryParse(text, CultureInfo.InvariantCulture, DateTimeStyles.AllowInnerWhite | DateTimeStyles.AllowLeadingWhite | DateTimeStyles.AllowTrailingWhite | DateTimeStyles.AllowWhiteSpaces, out var v2) => v2, "DateTimeOffset" when DateTimeOffset.TryParse(text, CultureInfo.InvariantCulture, DateTimeStyles.AllowInnerWhite | DateTimeStyles.AllowLeadingWhite | DateTimeStyles.AllowTrailingWhite | DateTimeStyles.AllowWhiteSpaces, out var v2) => v2, "TimeSpan" when TimeSpan.TryParse(text, CultureInfo.InvariantCulture, out var v2) => v2, "Boolean" => BooleanValues.Contains(text), _ => null });
/// <summary> /// Parses the specified object into the specified type. /// </summary> /// <param name="type">The type to parse into.</param> /// <param name="o">The object to parse.</param> /// <returns>An object of the specified type, if successfull; otherwise, null.</returns> public static object Parse(Type type, object o) { if (o == null) { return(null); } var text = o.ToString(); if (string.IsNullOrEmpty(text)) { return(null); } object result = null; switch (type.Name) { case "UInt16": { UInt16 v2; if (UInt16.TryParse(text, NumberStyles.Any, CultureInfo.InvariantCulture, out v2)) { result = v2; } break; } case "UInt32": { UInt32 v2; if (UInt32.TryParse(text, NumberStyles.Any, CultureInfo.InvariantCulture, out v2)) { result = v2; } break; } case "UInt64": { UInt64 v2; if (UInt64.TryParse(text, NumberStyles.Any, CultureInfo.InvariantCulture, out v2)) { result = v2; } break; } case "Int16": { Int16 v2; if (Int16.TryParse(text, NumberStyles.Any, CultureInfo.InvariantCulture, out v2)) { result = v2; } break; } case "Int32": { Int32 v2; if (Int32.TryParse(text, NumberStyles.Any, CultureInfo.InvariantCulture, out v2)) { result = v2; } break; } case "Int64": { Int64 v2; if (Int64.TryParse(text, NumberStyles.Any, CultureInfo.InvariantCulture, out v2)) { result = v2; } break; } case "Byte": { Byte v2; if (Byte.TryParse(text, NumberStyles.Any, CultureInfo.InvariantCulture, out v2)) { result = v2; } break; } case "Boolean": { result = BooleanValues.Contains(text); break; } case "Decimal": { decimal v2; if (decimal.TryParse(text, NumberStyles.Any, CultureInfo.InvariantCulture, out v2)) { result = v2; } break; } case "DateTime": { DateTime v2; if (DateTime.TryParse(text, CultureInfo.InvariantCulture, DateTimeStyles.AllowInnerWhite | DateTimeStyles.AllowLeadingWhite | DateTimeStyles.AllowTrailingWhite | DateTimeStyles.AllowWhiteSpaces, out v2)) { result = v2; } break; } case "DateTimeOffset": { DateTimeOffset v2; if (DateTimeOffset.TryParse(text, CultureInfo.InvariantCulture, DateTimeStyles.AllowInnerWhite | DateTimeStyles.AllowLeadingWhite | DateTimeStyles.AllowTrailingWhite | DateTimeStyles.AllowWhiteSpaces, out v2)) { result = v2; } break; } case "TimeSpan": { TimeSpan v2; if (TimeSpan.TryParse(text, CultureInfo.InvariantCulture, out v2)) { result = v2; } break; } } return(result); }