private static void GenerateClass(StringBuilder stringBuilder, Type type, TypeScriptAttribute attribute, bool camelCase) { stringBuilder.AppendLine(); stringBuilder.AppendFormat("\texport class {0}", type.Name); var interfaces = type.GetInterfaces().Where(x => x.GetCustomAttribute <TypeScriptAttribute>() != null); if (interfaces.Any()) { stringBuilder.AppendFormat(" implements {0}", string.Join(",", interfaces.Select(x => x.Name))); } stringBuilder.Append(" {"); foreach (var propertyInfo in GetAllProperties(type, true)) { stringBuilder.AppendLine(); stringBuilder.AppendFormat("\t\tpublic {0}: {1};", camelCase ? ToCamelCase(propertyInfo.Name) : propertyInfo.Name, TypeScriptConvert.ToScriptType(propertyInfo.PropertyType)); } if (attribute.GenerateTypeProperty) { stringBuilder.AppendLine(); stringBuilder.AppendFormat("\t\tpublic type: string = \"{0}\";", type.Name); } stringBuilder.AppendLine(); stringBuilder.Append("\t}"); }
private static void GenerateTypescript(StringBuilder stringBuilder, Type type, TypeScriptAttribute attribute, bool camelCase) { if (type.IsEnum) { GenerateEnum(stringBuilder, type); } else { if (type.IsInterface) { GenerateInterface(stringBuilder, type, attribute, camelCase); } if (type.IsClass) { GenerateClass(stringBuilder, type, attribute, camelCase); } } }
private static void GenerateInterface(StringBuilder stringBuilder, Type type, TypeScriptAttribute attribute, bool camelCase) { stringBuilder.AppendLine(); stringBuilder.AppendFormat("\texport interface {0}", type.Name); var extendingInterfaces = new List <string>(); if (type.BaseType != null) { var baseAttribute = type.BaseType.GetCustomAttributes <TypeScriptAttribute>().OfType <TypeScriptAttribute>().FirstOrDefault(); if (baseAttribute != null) { extendingInterfaces.Add(string.Format("{0}.{1}", type.BaseType.Namespace, type.BaseType.Name)); } } if (extendingInterfaces.Any()) { stringBuilder.AppendFormat(" extends {0}", string.Join(", ", extendingInterfaces)); } stringBuilder.Append(" {"); foreach (var propertyInfo in GetAllProperties(type, false)) { stringBuilder.AppendLine(); stringBuilder.AppendFormat("\t\t{0}: {1};", camelCase ? ToCamelCase(propertyInfo.Name) : propertyInfo.Name, TypeScriptConvert.ToScriptType(propertyInfo.PropertyType)); } stringBuilder.AppendLine(); stringBuilder.Append("\t}"); }
private static void GenerateClass(StringBuilder stringBuilder, Type type, TypeScriptAttribute attribute, bool camelCase) { stringBuilder.AppendLine(); stringBuilder.AppendFormat("\texport class {0}", type.Name); var interfaces = type.GetInterfaces().Where(x => x.GetCustomAttribute<TypeScriptAttribute>() != null); if(interfaces.Any()) { stringBuilder.AppendFormat(" implements {0}", string.Join(",", interfaces.Select(x => x.Name))); } stringBuilder.Append(" {"); foreach (var propertyInfo in GetAllProperties(type, true)) { stringBuilder.AppendLine(); stringBuilder.AppendFormat("\t\tpublic {0}: {1};", camelCase ? ToCamelCase(propertyInfo.Name) : propertyInfo.Name, TypeScriptConvert.ToScriptType(propertyInfo.PropertyType)); } if (attribute.GenerateTypeProperty) { stringBuilder.AppendLine(); stringBuilder.AppendFormat("\t\tpublic type: string = \"{0}\";", type.Name); } stringBuilder.AppendLine(); stringBuilder.Append("\t}"); }
private static void GenerateInterface(StringBuilder stringBuilder, Type type, TypeScriptAttribute attribute, bool camelCase) { stringBuilder.AppendLine(); stringBuilder.AppendFormat("\texport interface {0}", type.Name); var extendingInterfaces = new List<string>(); if (type.BaseType != null) { var baseAttribute = type.BaseType.GetCustomAttributes<TypeScriptAttribute>().OfType<TypeScriptAttribute>().FirstOrDefault(); if (baseAttribute != null) { extendingInterfaces.Add(string.Format("{0}.{1}", type.BaseType.Namespace, type.BaseType.Name)); } } if (extendingInterfaces.Any()) { stringBuilder.AppendFormat(" extends {0}", string.Join(", ", extendingInterfaces)); } stringBuilder.Append(" {"); foreach (var propertyInfo in GetAllProperties(type, false)) { stringBuilder.AppendLine(); stringBuilder.AppendFormat("\t\t{0}: {1};", camelCase ? ToCamelCase(propertyInfo.Name) : propertyInfo.Name, TypeScriptConvert.ToScriptType(propertyInfo.PropertyType)); } stringBuilder.AppendLine(); stringBuilder.Append("\t}"); }