Esempio n. 1
0
        /// <summary>
        /// Gets the parse function for a specific type.
        /// </summary>
        public static Func <string, T> GetParseFunction <T>()
        {
            var type = typeof(T);

            if (ParseFuncs.TryGetValue(type, out var func))
            {
                return((Func <string, T>)func);
            }

            if (type.IsEnum)
            {
                return(x => (T)Enum.Parse(type, x, true));
            }

            if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable <>))
            {
                var innerType = type.GetGenericArguments()[0];
                if (innerType.IsEnum)
                {
                    return(x => (T)Enum.Parse(innerType, x, true));
                }
            }

            throw new Exception($"No parser function found for type '{type.Name}'.");
        }
Esempio n. 2
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="fromString"></param>
 /// <param name="toString"></param>
 /// <typeparam name="T"></typeparam>
 public static void RegisterFunction <T>(Func <string, object> fromString, Func <object, string> toString)
 {
     ParseFuncs.Add(typeof(T), new Tuple <Func <string, object>, Func <object, string> >(fromString, toString));
 }