24 {
25 if (raw == null) return null;
26 if (T == typeof(string)) return raw;
27
28 if (T.IsPrimitive)
29 {
30 MethodInfo parse = T.GetMethod(
31 "Parse",
32 BindingFlags.Public | BindingFlags.Static,
33 new Type[] { typeof(string) }
34 );
35
36 try
37 {
38 return parse.Invoke(null, new object[] { raw });
39 }
40 catch (Exception e)
41 {
42 if (verbose)
43 {
44 CUI.Warning($"CUIParser failed to parse [{raw}] into primitive type [{T}]");
45 }
46 else throw e;
47
48 return DefaultFor(T);
49 }
50 }
51
52 if (T.IsEnum)
53 {
54 try
55 {
56 return Enum.Parse(T, raw);
57 }
58 catch (Exception e)
59 {
60 if (verbose)
61 {
62 CUI.Warning($"CUIParser failed to parse [{raw}] into Enum [{T}]");
63 }
64 else throw e;
65
66 return DefaultFor(T);
67 }
68 }
69
70 if (!T.IsPrimitive)
71 {
72 MethodInfo parse = null;
73 if (CUIExtensions.Parse.ContainsKey(T))
74 {
75 parse = CUIExtensions.Parse[T];
76 }
77 else
78 {
79 parse = T.GetMethod(
80 "Parse",
81 BindingFlags.Public | BindingFlags.Static,
82 new Type[] { typeof(string) }
83 );
84 }
85
86 try
87 {
88 return parse.Invoke(null, new object[] { raw });
89 }
90 catch (Exception e)
91 {
92 if (verbose)
93 {
94 CUI.Warning($"CUIParser failed to parse [{raw}] into [{T}]");
95 }
96 else throw e;
97
98 return DefaultFor(T);
99 }
100 }
101
102 return DefaultFor(T);
103 }