20 public static object DefaultFor(Type T) => Activator.CreateInstance(T);
22 public static T Parse<T>(
string raw) => (T)Parse(raw, typeof(T));
23 public static object Parse(
string raw, Type T,
bool verbose =
true)
25 if (raw ==
null)
return null;
26 if (T == typeof(
string))
return raw;
30 MethodInfo parse = T.GetMethod(
32 BindingFlags.Public | BindingFlags.Static,
33 new Type[] { typeof(string) }
38 return parse.Invoke(
null,
new object[] { raw });
44 CUI.Warning($
"CUIParser failed to parse [{raw}] into primitive type [{T}]");
56 return Enum.Parse(T, raw);
62 CUI.Warning($
"CUIParser failed to parse [{raw}] into Enum [{T}]");
72 MethodInfo parse =
null;
73 if (CUIExtensions.Parse.ContainsKey(T))
75 parse = CUIExtensions.Parse[T];
81 BindingFlags.Public | BindingFlags.Static,
82 new Type[] { typeof(string) }
88 return parse.Invoke(
null,
new object[] { raw });
94 CUI.Warning($
"CUIParser failed to parse [{raw}] into [{T}]");
102 return DefaultFor(T);
105 public static string Serialize(
object o,
bool verbose =
true)
107 if (o.GetType() == typeof(
string))
return (
string)o;
109 MethodInfo customToString = CUIExtensions.CustomToString.GetValueOrDefault(o.GetType());
110 string result =
null;
114 result = customToString ==
null ? o.ToString() : (string)customToString.Invoke(
null,
new object[] { o });
118 if (verbose)
CUI.Warning($
"CUIParser failed to serialize object of [{o.GetType()}] type");