-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringConverter.cs
55 lines (52 loc) · 1.64 KB
/
StringConverter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EasyExcelFramework
{
internal class StringConverter
{
public object DetectType(string stringValue)
{
if (stringValue == null)
return null;
var expectedTypes = new List<Type> { typeof(DateTime)};
foreach (var type in expectedTypes)
{
TypeConverter converter = TypeDescriptor.GetConverter(type);
if (converter.CanConvertFrom(typeof(string)))
{
try
{
// You'll have to think about localization here
object newValue = converter.ConvertFromInvariantString(stringValue);
if (newValue != null)
{
return newValue;
}
}
catch
{
// Can't convert given string to this type
continue;
}
}
}
if (stringValue[0].Equals("\"") & stringValue[stringValue.Length-1].Equals("\""))
{
return stringValue;
}
if (int.TryParse(stringValue, out int result))
{
return result;
}
if (float.TryParse(stringValue, out float floatresult))
{
return floatresult;
}
return stringValue;
}
}
}