private static string generateInner(IEnumerable <Type> types, TsReflectionFilters filters) { var cb = new TypescriptCodeBuilder(); var lastNamespace = ""; var scope = null as ScriptScope; types = types.OrderBy(t => t.ToTsName()).OrderBy(t => t.GetTsNamespace()); foreach (var t in types) { // namespace var ns = t.GetTsNamespace(); if (ns != lastNamespace) { scope?.Dispose(); scope = cb.EnterNameSpace(ns); lastNamespace = ns; } // class var baseTypes = t.BaseType?.ToTsName() != "any" && filters.TypeFilter(t.BaseType) ? new[] { t.BaseType.ToTsFullName() } : null; var interfaces = t.GetInterfaces() .Where(i => filters.TypeFilter(i)) .Select(i => i.ToTsFullName()) .ToArray(); if (t.IsEnum) { GenerateEnum(t, cb); } else { using (cb.EnterClass(t.ToTsName(), baseTypes, interfaces, Ambient.export)) { // property foreach (var prop in t.GetProperties().Where(p => filters.PropertyFilter(t, p))) { cb.AppendLine($"{prop.Name} : {prop.PropertyType.ToTsFullName()};"); } // field foreach (var field in t.GetFields().Where(f => filters.FieldFilter(t, f))) { cb.AppendLine($"{field.Name} : {field.FieldType.ToTsFullName()};"); } } } } scope?.Dispose(); return(cb.ToString()); }
protected static void GenerateEnum(Type type, TypescriptCodeBuilder cb) { using (cb.EnterEnum(type.Name, Ambient.export)) { var names = type.GetEnumNames(); var values = type.GetEnumValues(); var baseType = type.GetEnumUnderlyingType(); for (var i = 0; i < names.Length; i++) { cb.AppendLine($"{names[i]}={GetEnumValue(values.GetValue(i), baseType)},"); } } }