-
Notifications
You must be signed in to change notification settings - Fork 0
/
TypeExtensions.cs
48 lines (43 loc) · 1.56 KB
/
TypeExtensions.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
using System.Collections.Generic;
// ReSharper disable once CheckNamespace
namespace System
{
public static class TypeExtensions
{
private static readonly Dictionary<Type, TypeCode> TypeCodeMap =
new Dictionary<Type, TypeCode>
{
{typeof(bool), TypeCode.Boolean},
{typeof(byte), TypeCode.Byte},
{typeof(sbyte), TypeCode.SByte},
{typeof(char), TypeCode.Char},
{typeof(DateTime), TypeCode.DateTime},
{typeof(decimal), TypeCode.Decimal},
{typeof(double), TypeCode.Double},
{typeof(float), TypeCode.Single},
{typeof(short), TypeCode.Int16},
{typeof(int), TypeCode.Int32},
{typeof(long), TypeCode.Int64},
{typeof(ushort), TypeCode.UInt16},
{typeof(uint), TypeCode.UInt32},
{typeof(ulong), TypeCode.UInt64},
{typeof(string), TypeCode.String}
};
public static TypeCode GetTypeCode(this object obj)
{
if (obj == null)
return TypeCode.Empty;
return obj.GetType().ToTypeCode();
}
public static TypeCode ToTypeCode(this Type type)
{
if (type == null)
return TypeCode.Empty;
TypeCode tc;
if (!TypeCodeMap.TryGetValue(type, out tc)) {
tc = TypeCode.Object;
}
return tc;
}
}
}