-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTypeUtils.cs
More file actions
91 lines (77 loc) · 2.74 KB
/
TypeUtils.cs
File metadata and controls
91 lines (77 loc) · 2.74 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
using System;
using System.Collections.Generic;
using System.Linq;
public static class TypeUtils
{
private class TypeInfo
{
public TypeInfo(string type, string defaultInitializer, string valueAccessor, string requiredUsing = "", bool requiresQuotes = false, string toStringConverter = "", string converterUsing = "", string assertPostfix = "")
{
Type = type;
DefaultInitializer = defaultInitializer;
ValueAccessor = valueAccessor;
RequiredUsing = requiredUsing;
RequiresQuotes = requiresQuotes;
ToStringConverter = toStringConverter;
ConverterUsing = converterUsing;
AssertPostfix = assertPostfix;
}
public string Type { get; }
public string DefaultInitializer { get; }
public string ValueAccessor { get; }
public string RequiredUsing { get; }
public bool RequiresQuotes { get; }
public string ToStringConverter { get; }
public string ConverterUsing { get; }
public string AssertPostfix { get; }
}
private static List<TypeInfo> types = new List<TypeInfo>
{
new TypeInfo("int", "", ".Value"),
new TypeInfo("bool", "", ".Value"),
new TypeInfo("string", " = \"\";", "", null, true),
new TypeInfo("float", "", ".Value", "", false, ".ToString(CultureInfo.InvariantCulture)", "System.Globalization"),
new TypeInfo("double", "", ".Value", "", false, ".ToString(CultureInfo.InvariantCulture)", "System.Globalization"),
new TypeInfo("DateTime", "", ".Value", "System", true, ".ToString(\"o\")", "", ".Within(0.01).Seconds")
};
public static bool IsNullableRequiredForType(string type)
{
return types.Any(t => t.Type == type);
}
public static string GetInitializerForType(string type)
{
return Get(type).DefaultInitializer;
}
public static string GetValueAccessor(string type)
{
return Get(type).ValueAccessor;
}
public static string GetTypeRequiredUsing(string type)
{
return Get(type).RequiredUsing;
}
public static bool RequiresQuotes(string type)
{
return Get(type).RequiresQuotes;
}
public static string GetToStringConverter(string type)
{
return Get(type).ToStringConverter;
}
public static string GetConverterRequiredUsing(string type)
{
return Get(type).ConverterUsing;
}
public static string GetAssertPostfix(string type)
{
return Get(type).AssertPostfix;
}
public static string[] GetSupportedTypes()
{
return types.Select(t => t.Type).ToArray();
}
private static TypeInfo Get(string type)
{
return types.Single(t => t.Type == type);
}
}