forked from ThatRendle/Simple.Data
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTypeHelper.cs
More file actions
73 lines (68 loc) · 5.55 KB
/
TypeHelper.cs
File metadata and controls
73 lines (68 loc) · 5.55 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using System;
using System.Collections.Generic;
namespace Simple.Data.Ado
{
using System.Data;
public static class TypeHelper
{
private static readonly HashSet<Type> BaseTypes = new HashSet<Type>
{
typeof (bool),
typeof (char),
typeof (sbyte),
typeof (byte),
typeof (short),
typeof (ushort),
typeof (int),
typeof (uint),
typeof (long),
typeof (ulong),
typeof (float),
typeof (double),
typeof (decimal),
typeof (DateTime),
typeof (string),
typeof (byte[]),
typeof (Guid),
};
private static readonly Dictionary<DbType, Type> DbTypeToClrTypeMap = new Dictionary<DbType, Type>
{
{DbType.Int16, typeof (short)},
{DbType.Int32, typeof (int)},
{DbType.Double, typeof (double)},
{DbType.Guid, typeof (Guid)},
{DbType.SByte, typeof (sbyte)},
{DbType.Single, typeof (Single)},
{DbType.Int64, typeof (long)},
{DbType.Object, typeof(object)},
{DbType.Byte, typeof(byte)},
{DbType.Boolean, typeof(bool)},
{DbType.AnsiString, typeof(string)},
{DbType.Binary, typeof(byte[])},
{DbType.DateTime, typeof(DateTime)},
{DbType.Decimal, typeof(decimal)},
{DbType.Currency, typeof(decimal)},
{DbType.Date, typeof(DateTime)},
{DbType.StringFixedLength, typeof(string)},
{DbType.AnsiStringFixedLength, typeof(string)},
{DbType.Xml, typeof(string)},
{DbType.DateTimeOffset, typeof(DateTime)},
{DbType.DateTime2, typeof(DateTime)},
{DbType.VarNumeric, typeof(double)},
{DbType.UInt16, typeof(ushort)},
{DbType.String, typeof(string)},
{DbType.Time, typeof(TimeSpan)},
{DbType.UInt64, typeof(ulong)},
{DbType.UInt32, typeof(uint)}
};
public static bool IsKnownType(Type type)
{
return BaseTypes.Contains(type);
}
public static Type ToClrType(this DbType dbType)
{
if (!DbTypeToClrTypeMap.ContainsKey(dbType)) return typeof (object);
return DbTypeToClrTypeMap[dbType];
}
}
}