private static void GenerateInterface(StringBuilder stringBuilder, Type type, TypescriptAttribute attribute, bool camelCase) { stringBuilder.AppendLine(); stringBuilder.AppendFormat("\texport interface I{0}", type.Name); var extendingInterfaces = new List<string>(); if (type.BaseType != null) { var baseAttribute = type.BaseType.GetCustomAttributes<TypescriptAttribute>().OfType<TypescriptAttribute>().FirstOrDefault(); if (baseAttribute != null) { if (baseAttribute.GenerateInterface) { extendingInterfaces.Add(string.Format("{0}.I{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)); } if (attribute.GenerateInterfaceMethods) { foreach (var methodInfo in type.GetMethods()) { stringBuilder.AppendLine(); var parameters = methodInfo.GetParameters(); var parametersAsTs = parameters.Select(x => string.Format("{0}:{1}", x.Name, TypescriptConvert.ToScriptType(x.ParameterType))); stringBuilder.AppendFormat("\t\t{0}({1}):{2};" , camelCase ? ToCamelCase(methodInfo.Name) : methodInfo.Name , string.Join(", ", parametersAsTs) , TypescriptConvert.ToScriptType(methodInfo.ReturnType)); } } 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); if (attribute.GenerateInterface) { stringBuilder.AppendFormat(" implements I{0}", type.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 (attribute.GenerateInterface) { GenerateInterface(stringBuilder, type, attribute, camelCase); } if (attribute.GenerateClass) { GenerateClass(stringBuilder, type, attribute, camelCase); } } }