Ejemplo n.º 1
0
        /// <summary>
        /// The default value of the type.
        /// (Dictionary types returns {}, enumerable types returns [], boolean types returns false,
        /// numeric types returns 0, void returns void(0), all other types return null)
        /// </summary>
        public static string Default(this Type type)
        {
            // Dictionary = { [key: type]: type; }
            if (type.Name.StartsWith("{"))
            {
                return("{}");
            }

            if (type.IsEnumerable)
            {
                return("[]");
            }

            if (type.Name == "boolean" && type.IsNullable == false)
            {
                return("false");
            }
            if (type.Name == "number" && type.IsNullable == false)
            {
                return("0");
            }
            if (type.Name == "void")
            {
                return("void(0)");
            }
            if (type.IsGuid && type.IsNullable == false)
            {
                return("\"00000000-0000-0000-0000-000000000000\"");
            }

            return("null");
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Returns the first TypeArgument of a generic type or the type itself if it's not generic.
 /// </summary>
 public static Type Unwrap(this Type type)
 {
     return(type.IsGeneric ? type.TypeArguments.First() : type);
 }
Ejemplo n.º 3
0
 /// <summary>
 /// Returns the name of the type without []
 /// </summary>
 public static string ClassName(this Type type)
 {
     return(type.Name.TrimEnd('[', ']'));
 }