Skip to content

Commit 95d91f2

Browse files
committed
Redesigned StringParserProvider
1 parent 1b0b0dc commit 95d91f2

1 file changed

Lines changed: 20 additions & 41 deletions

File tree

Lines changed: 20 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,31 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.ComponentModel.DataAnnotations;
42
using System.Globalization;
5-
using System.Linq;
6-
using System.Text;
7-
8-
namespace IGLib
9-
{
103

4+
namespace IGLib;
115

12-
internal static class StringParserProvider
6+
internal static class StringParserProvider
7+
{
8+
public static IStringParser<T> GetParser<T>(IFormatProvider formatProvider = null)
139
{
14-
public static IStringParser<T> GetParser<T>(IFormatProvider? formatProvider = null)
15-
{
16-
object parser = GetParser(typeof(T), formatProvider ?? CultureInfo.CurrentCulture);
17-
18-
return (IStringParser<T>)parser;
19-
}
20-
21-
private static object GetParser(Type targetType, IFormatProvider formatProvider)
22-
{
23-
if (targetType == typeof(string))
24-
return new IdentityStringParser();
25-
26-
if (targetType == typeof(bool))
27-
return new BooleanStringParser();
28-
29-
if (targetType == typeof(int))
30-
return new Int32StringParser(formatProvider);
31-
32-
//if (targetType == typeof(byte))
33-
// return new ByteStringParser(formatProvider);
10+
object parser = GetParser(typeof(T), formatProvider ?? CultureInfo.CurrentCulture);
3411

35-
//if (targetType == typeof(byte))
36-
// return new SByteStringParser(formatProvider);
37-
38-
39-
40-
41-
if (targetType == typeof(decimal))
42-
return new DecimalStringParser(formatProvider);
12+
if (parser is IStringParser<T> typedParser)
13+
return typedParser;
4314

15+
throw new InvalidOperationException(
16+
$"Internal parser provider error: parser for type '{typeof(T).FullName}' " +
17+
$"has incompatible runtime type '{parser.GetType().FullName}'.");
18+
}
4419

20+
private static object GetParser(Type targetType, IFormatProvider formatProvider)
21+
{
22+
if (targetType == typeof(string))
23+
return new IdentityStringParser();
4524

46-
throw new NotSupportedException(
47-
$"No built-in string parser is available for target type '{targetType.FullName}'.");
48-
}
25+
if (targetType == typeof(bool))
26+
return new BooleanStringParser();
4927

28+
throw new NotSupportedException(
29+
$"No built-in string parser is available for target type '{targetType.FullName}'.");
5030
}
51-
52-
}
31+
}

0 commit comments

Comments
 (0)