private static String TypeGenImpl(Type type, TsOptions options, Stack <Type> typeStack) { if (options == null) { options = DEFAULT_OPTIONS; } if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Task <>)) { var redirect = type.GetProperty("Result").PropertyType; return(GuardedTypeGen(redirect, options, typeStack)); } if (type == typeof(Task)) { return("void"); } if (type == typeof(String) || type == typeof(char) || type == typeof(DateTime) || type == typeof(DateTimeOffset) || type == typeof(TimeSpan)) { return("string"); } if (type == typeof(bool)) { return("boolean"); } if (TypeHelpers.IsNumericType(type)) { return("number"); } if (type == typeof(void)) { return("void"); } if (type.IsArray) { return(GuardedTypeGen(type.GetElementType(), options, typeStack) + "[]"); } if (type.IsEnum) { if (options.EnumNumberValues) { return(String.Join(" | ", type.GetEnumValues() as IEnumerable <int>)); } else { return(String.Join(" | ", type.GetEnumNames().Select(name => "'" + name + "'"))); } } var iDictionary = type.GetInterfaces().FirstOrDefault(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IDictionary <,>)); if (iDictionary != null) { var args = iDictionary.GetGenericArguments(); if (args.Length == 2) { return("{ [key:string]: " + GuardedTypeGen(args[1], options, typeStack) + " }"); } return("{ [key:string]: string }"); } if (typeof(IEnumerable).IsAssignableFrom(type)) { var generics = type.GetGenericArguments(); if (generics.Length == 1) { return(GuardedTypeGen(generics[0], options, typeStack) + "[]"); } return("any[]"); } var nullableUndelyingType = Nullable.GetUnderlyingType(type); if (nullableUndelyingType != null) { // is nullable<T> return(GuardedTypeGen(nullableUndelyingType, options, typeStack) + " | null"); } var bindingFlags = BindingFlags.Instance; if (options.Public) { bindingFlags |= BindingFlags.Public; } if (options.NonPublic) { bindingFlags |= BindingFlags.NonPublic; } if (options.Readable) { if (options.Fields) { bindingFlags |= BindingFlags.GetField; } if (options.Properties) { bindingFlags |= BindingFlags.GetProperty; } } if (options.Writeable) { if (options.Fields) { bindingFlags |= BindingFlags.SetField; } if (options.Properties) { bindingFlags |= BindingFlags.SetProperty; } } var members = type.GetMembers(bindingFlags); if (members.Length > 0) { var strb = new StringBuilder(); var separator = "{ "; foreach (var member in members) { if (member.MemberType == MemberTypes.Property && options.Properties) { var property = type.GetProperty(member.Name); if (!(options.Readable && property.CanRead && (options.NonPublic || property.GetGetMethod(nonPublic: true).IsPublic) || options.Writeable && property.CanWrite && (options.NonPublic || property.GetSetMethod(nonPublic: true).IsPublic))) { continue; } var name = options.LowercaseFirstLetter ? property.Name.LowerFirstLetter() : property.Name; var typeString = GuardedTypeGen(property.PropertyType, options, typeStack); AppendMember(strb, name, typeString, separator); separator = ", "; } else if (member.MemberType == MemberTypes.Field && options.Fields) { var field = type.GetField(member.Name, bindingFlags); if (field.Name.Contains("<") || field.Name.Contains(">")) { continue; // skip auto generated backing fields } if (options.Readable || !field.IsInitOnly && options.Writeable) { var name = options.LowercaseFirstLetter ? field.Name.LowerFirstLetter() : field.Name; var typeString = GuardedTypeGen(field.FieldType, options, typeStack); AppendMember(strb, name, typeString, separator); separator = ", "; } } } if (strb.Length == 0) { return("any"); // had no valid members } strb.Append(" }"); return(strb.ToString()); } return("any"); }