예제 #1
0
        private static TsType FromInternal(Type type)
        {
            if (type == typeof(void))
            {
                return(TsPredefinedType.Void());
            }
            if (type == typeof(string))
            {
                return(TsPredefinedType.String());
            }
            if (type.IsNumber())
            {
                return(TsPredefinedType.Number());
            }
            if (type == typeof(bool))
            {
                return(TsPredefinedType.Boolean());
            }
            if (type == typeof(Guid))
            {
                return(TsPredefinedType.String());
            }
            if (type == typeof(DateTime))
            {
                return(new TsTypeReference("Date"));
            }
            if (type == typeof(object))
            {
                return(TsPredefinedType.Any());
            }
            if (type == typeof(Type))
            {
                return(TsPredefinedType.String());
            }

            return(new TsTypeReference(Helpers.GetName(type))
            {
                CsType = new CsType(type)
            });
        }
예제 #2
0
        private static TsType From(Type type,
                                   bool forceNullable = false)
        {
            if (Helpers.IsNullable(type) || forceNullable)
            {
                return(new TsUnionType(From(Helpers.UnwrapNullable(type)),
                                       new TsNull()));
            }

            foreach (var customTypeConverter in customTypeConverters)
            {
                if (customTypeConverter.TryConvert(type, out var tsType))
                {
                    return(tsType);
                }
            }

            if (Helpers.IsEnumerable(type))
            {
                return(new TsArrayType {
                    ElementType = From(Helpers.UnwrapEnumerableType(type))
                });
            }

            if (typeof(Delegate).IsAssignableFrom(type))
            {
                return(TsPredefinedType.Any());
            }

            if (type.IsGenericType)
            {
                if (type.GetGenericTypeDefinition() == typeof(Dictionary <,>) ||
                    type.GetGenericTypeDefinition() == typeof(IDictionary <,>))
                {
                    var arguments      = type.GetGenericArguments();
                    var keyType        = arguments[0];
                    var valueType      = arguments[1];
                    var indexSignature = keyType.IsNumber()
                                             ? TsIndexSignature.ByNumber(From(valueType))
                                             : TsIndexSignature.ByString(From(valueType));
                    return(new TsObjectType {
                        Members = new[] { indexSignature }
                    });
                }

                var typeArguments = type.GetGenericArguments()
                                    .Select(x => From(x))
                                    .ToArray();
                return(new TsTypeReference(Helpers.GetName(type),
                                           typeArguments)
                {
                    CsType = new CsType(type)
                });
            }

            if (type == typeof(NameValueCollection))
            {
                return(new TsObjectType
                {
                    Members = new[] { TsIndexSignature.ByString(TsPredefinedType.String()) }
                });
            }

            return(FromInternal(type));
        }