/// <summary> /// Returns the text passed across unless <paramref name="resolver"/> is supplied, in which case /// it is passed through the resolver. Null is returned if the resolver rejects the string. /// </summary> /// <param name="text"></param> /// <param name="resolver"></param> /// <returns></returns> public static string ParseString(string text, TypeParserResolver resolver) { var parser = resolver?.StringParser; return(parser == null ? text : parser.TryParse(text, out var result) ? result : null); }
/// <summary> /// Extracts a GUID from the text or null if no GUID could be extracted. /// </summary> /// <param name="text"></param> /// <param name="resolver"></param> /// <returns></returns> public static Guid?ParseGuid(string text, TypeParserResolver resolver) { var parser = resolver?.GuidParser; return(parser == null ? ParseGuid(text) : parser.TryParse(text, out var result) ? result : (Guid?)null); }
/// <summary> /// Extracts a bool from the text or null if no bool could be extracted. /// </summary> /// <param name="text"></param> /// <param name="resolver"></param> /// <returns></returns> public static bool?ParseBool(string text, TypeParserResolver resolver) { var parser = resolver?.BoolParser; return(parser == null ? ParseBool(text) : parser.TryParse(text, out var result) ? result : (bool?)null); }
/// <summary> /// Extracts a byte array from text with an optional type parser resolver. /// </summary> /// <param name="text"></param> /// <param name="resolver"></param> /// <returns></returns> public static byte[] ParseByteArray(string text, TypeParserResolver resolver) { var parser = resolver?.ByteArrayParser; return(parser == null ? ParseByteArray(text) : parser.TryParse(text, out var result) ? result : (byte[])null); }
/// <summary> /// Extracts a decimal floating point number from the text or null if no decimal could be extracted. /// </summary> /// <param name="text"></param> /// <param name="resolver"></param> /// <returns></returns> public static decimal?ParseDecimal(string text, TypeParserResolver resolver) { var parser = resolver?.DecimalParser; return(parser == null ? ParseDecimal(text) : parser.TryParse(text, out var result) ? result : (decimal?)null); }
/// <summary> /// Extracts a date time offset from the text or null if no date time offset could be extracted. /// </summary> /// <param name="text"></param> /// <param name="resolver"></param> /// <returns></returns> public static DateTimeOffset?ParseDateTimeOffset(string text, TypeParserResolver resolver) { var parser = resolver?.DateTimeOffsetParser; return(parser == null ? ParseDateTimeOffset(text) : parser.TryParse(text, out var result) ? result : (DateTimeOffset?)null); }
/// <summary> /// Extracts a floating point number from the text or null if no float could be extracted. /// </summary> /// <param name="text"></param> /// <param name="resolver"></param> /// <returns></returns> public static float?ParseFloat(string text, TypeParserResolver resolver) { var parser = resolver?.FloatParser; return(parser == null ? ParseFloat(text) : parser.TryParse(text, out var result) ? result : (float?)null); }
/// <summary> /// Extracts a double-precision floating point number from the text or null if no double could be extracted. /// </summary> /// <param name="text"></param> /// <param name="resolver"></param> /// <returns></returns> public static double?ParseDouble(string text, TypeParserResolver resolver) { var parser = resolver?.DoubleParser; return(parser == null ? ParseDouble(text) : parser.TryParse(text, out var result) ? result : (double?)null); }
/// <summary> /// Extracts an unsigned long from the text or null if no ulong could be extracted. /// </summary> /// <param name="value"></param> /// <param name="resolver"></param> /// <returns></returns> public static UInt64?ParseUInt64(string text, TypeParserResolver resolver) { var parser = resolver?.UInt64Parser; return(parser == null ? ParseUInt64(text) : parser.TryParse(text, out var result) ? result : (UInt64?)null); }
/// <summary> /// Extracts an integer from the text or null if no int could be extracted. /// </summary> /// <param name="value"></param> /// <param name="resolver"></param> /// <returns></returns> public static Int32?ParseInt32(string text, TypeParserResolver resolver) { var parser = resolver?.Int32Parser; return(parser == null ? ParseInt32(text) : parser.TryParse(text, out var result) ? result : (Int32?)null); }
/// <summary> /// Extracts a single character from the text or null if no char could be extracted. /// </summary> /// <param name="text"></param> /// <param name="resolver"></param> /// <returns></returns> public static char?ParseChar(string text, TypeParserResolver resolver) { var parser = resolver?.CharParser; return(parser == null ? ParseChar(text) : parser.TryParse(text, out var result) ? result : (char?)null); }
/// <summary> /// Adds the resolver passed across to the cache. Must be called within a lock. /// </summary> /// <param name="resolver"></param> private static void AddToCache(TypeParserResolver resolver) { lock (_SyncLock) { var copy = new TypeParserResolver[_Resolvers.Length + 1]; Array.Copy(_Resolvers, 0, copy, 0, _Resolvers.Length); copy[copy.Length - 1] = resolver; _Resolvers = copy; } }
/// <summary> /// Returns an existing <see cref="TypeParserResolver"/> that has the exact same parsers /// as those passed in or creates and returns a new resolver if no such parser exists. /// </summary> /// <param name="parsers"></param> /// <returns></returns> public static TypeParserResolver Find(params ITypeParser[] parsers) { parsers = parsers ?? new ITypeParser[0]; var result = FindInCache(parsers); if (result == null) { lock (_SyncLock) { result = FindInCache(parsers); if (result == null) { result = new TypeParserResolver(parsers); AddToCache(result); } } } return(result); }
/// <summary> /// Parses the <paramref name="text"/> into an object of type <paramref name="type"/>. If the /// text cannot be parsed then null is returned. /// </summary> /// <param name="type"></param> /// <param name="text"></param> /// <param name="typeParserResolver"></param> /// <returns></returns> /// <remarks> /// This should be faster than TypeDescriptor.GetConverter().ConvertFrom() for the stock types. /// </remarks> public static object ParseType(Type type, string text, TypeParserResolver typeParserResolver) { object result = null; if (type == typeof(string)) { result = typeParserResolver == null ? text : ParseString(text, typeParserResolver); } else if (text != null) { if (type == typeof(bool) || type == typeof(bool?)) { result = typeParserResolver == null ? ParseBool(text) : ParseBool(text, typeParserResolver); } else if (type == typeof(byte) || type == typeof(byte?)) { result = typeParserResolver == null ? ParseByte(text) : ParseByte(text, typeParserResolver); } else if (type == typeof(char) || type == typeof(char?)) { result = typeParserResolver == null ? ParseChar(text) : ParseChar(text, typeParserResolver); } else if (type == typeof(Int16) || type == typeof(Int16?)) { result = typeParserResolver == null ? ParseInt16(text) : ParseInt16(text, typeParserResolver); } else if (type == typeof(UInt16) || type == typeof(UInt16?)) { result = typeParserResolver == null ? ParseUInt16(text) : ParseUInt16(text, typeParserResolver); } else if (type == typeof(Int32) || type == typeof(Int32?)) { result = typeParserResolver == null ? ParseInt32(text) : ParseInt32(text, typeParserResolver); } else if (type == typeof(UInt32) || type == typeof(UInt32?)) { result = typeParserResolver == null ? ParseUInt32(text) : ParseUInt32(text, typeParserResolver); } else if (type == typeof(Int64) || type == typeof(Int64?)) { result = typeParserResolver == null ? ParseInt64(text) : ParseInt64(text, typeParserResolver); } else if (type == typeof(UInt64) || type == typeof(UInt64?)) { result = typeParserResolver == null ? ParseUInt64(text) : ParseUInt64(text, typeParserResolver); } else if (type == typeof(float) || type == typeof(float?)) { result = typeParserResolver == null ? ParseFloat(text) : ParseFloat(text, typeParserResolver); } else if (type == typeof(double) || type == typeof(double?)) { result = typeParserResolver == null ? ParseDouble(text) : ParseDouble(text, typeParserResolver); } else if (type == typeof(decimal) || type == typeof(decimal?)) { result = typeParserResolver == null ? ParseDecimal(text) : ParseDecimal(text, typeParserResolver); } else if (type == typeof(DateTime) || type == typeof(DateTime?)) { result = typeParserResolver == null ? ParseDateTime(text) : ParseDateTime(text, typeParserResolver); } else if (type == typeof(DateTimeOffset) || type == typeof(DateTimeOffset?)) { result = typeParserResolver == null ? ParseDateTimeOffset(text) : ParseDateTimeOffset(text, typeParserResolver); } else if (type == typeof(Guid) || type == typeof(Guid?)) { result = typeParserResolver == null ? ParseGuid(text) : ParseGuid(text, typeParserResolver); } else if (type == typeof(byte[])) { result = typeParserResolver == null ? ParseByteArray(text) : ParseByteArray(text, typeParserResolver); } } return(result); }