コード例 #1
0
 /// <summary>
 /// Converts the specific type to it's string representation using a formatter registered for the type
 /// </summary>
 /// <param name="type">The type to format.</param>
 /// <returns>The string representation of the type.</returns>
 public string FormatType(TsType type)
 {
     if (_formatters.ContainsKey(type.GetType()))
     {
         return _formatters[type.GetType()](type, this);
     }
     else
     {
         return "any";
     }
 }
コード例 #2
0
        private static IEnumerable <TsType> Unwrap(TsType tsType)
        {
            switch (tsType)
            {
            case TsUnionType tsUnionType:
                return(tsUnionType.Types.SelectMany(Unwrap));

            case TsArrayType tsArrayType:
                return(Unwrap(tsArrayType.ElementType));

            case TsObjectType tsObjectType:
                return(tsObjectType.Members
                       .Select(UnwrapTsTypeMember)
                       .SelectMany(Unwrap));

            case TsTypeReference tsTypeReference:
                return(tsTypeReference.TypeArguments
                       .SelectMany(Unwrap)
                       .Append(tsTypeReference));

            case TsEnumLiteralType tsEnumLiteralType:
                return(new[] { tsEnumLiteralType.EnumType });

            case TsNull _:
            case TsPredefinedType _:
            case TsStringLiteralType _:
            case TsBoolLiteralType _:
            case TsIntLiteralType _:
                return(new[]
                {
                    tsType
                });

            default:
                throw new ArgumentOutOfRangeException(nameof(tsType),
                                                      tsType.GetType().Name,
                                                      null);
            }
        }
 /// <summary>
 /// Converts the specific type to it's string representation using a formatter registered for the type
 /// </summary>
 /// <param name="formatters"></param>
 /// <param name="type">The type to format.</param>
 /// <returns>The string representation of the type.</returns>
 public static string FormatType(this Dictionary <Type, TsTypeFormatter> formatters, TsType type)
 {
     return(formatters.ContainsKey(type.GetType()) ? formatters[type.GetType()](type) : "any");
 }