/// <summary> Add a base type to SUCC serialization. It is recommended that you call this method in a static constructor. </summary> public static void AddBaseType <T>(SerializeMethod <T> serializeMethod, ParseMethod <T> parseMethod) { AddBaseType ( typeof(T), (object obj) => serializeMethod((T)obj), (string text) => parseMethod.Invoke(text) ); }
static T ParseToT <T>(string Src) { Type TT = typeof(T); // If no value found, return default if (string.IsNullOrEmpty(Src)) { return(default(T)); } // If it's a string, return it if (TT == typeof(string)) { return((T)(object)Src); } // If it contains a public string constructor if (TT.GetConstructor(new Type[] { typeof(string) }) != null) { return((T)Activator.CreateInstance(TT, Src)); } // If it has a Type.Parse static method MethodInfo ParseMethod; if ((ParseMethod = TT.GetMethod("Parse", new Type[] { typeof(string), typeof(IFormatProvider) })) != null) { return((T)ParseMethod.Invoke(null, new object[] { Src, CultureInfo.InvariantCulture })); } else if ((ParseMethod = TT.GetMethod("Parse", new Type[] { typeof(string) })) != null) { return((T)ParseMethod.Invoke(null, new object[] { Src })); } throw new NotImplementedException("Could not parse type " + TT); }