public string CreateClass(GeneratedTypeScript scriptInfo, bool includeModule) { var sb = new StringBuilder(); if (includeModule) { sb.AppendFormat("module " + scriptInfo.ModuleName + " {{\r\n"); } sb.AppendFormat(" export class {0} {{\r\n", scriptInfo.TypeName); sb.AppendFormat(" public static TYPE_NAME: string = '" + scriptInfo.TypeName + "';\r\n"); var properties = GetInterfaceMembers(scriptInfo.DotNetType); foreach (var mi in properties) { sb.AppendFormat(" public {0}: {1};\r\n", mi.Name, GetTypeName(mi, scriptInfo)); } var constructor = scriptInfo.DotNetType.GetConstructors().FirstOrDefault(); if (constructor != null) { var contructorParameterMappings = new Dictionary <string, string>(); foreach (var parameter in constructor.GetParameters()) { var property = properties.FirstOrDefault(x => x.Name.Equals(parameter.Name, StringComparison.OrdinalIgnoreCase)); if (property != null) { contructorParameterMappings.Add(parameter.Name, property.Name); } } if (contructorParameterMappings.Count > 0) { sb.Append(" public constructor("); foreach (var parameter in constructor.GetParameters()) { sb.Append(parameter.Name + ": " + GetTypeName(parameter.ParameterType, scriptInfo) + ", "); } sb.Remove(sb.Length - 2, 2); sb.AppendLine(") {"); foreach (var parameter in contructorParameterMappings) { sb.AppendFormat(" this.{0} = {1};\r\n", parameter.Value, parameter.Key); } sb.AppendLine(" }"); } } sb.AppendLine(" }"); if (includeModule) { sb.AppendLine("}"); } return(sb.ToString()); }
public string GetEnum(GeneratedTypeScript type, bool includeModule) { var sb = new StringBuilder(); var values = (int[])Enum.GetValues(type.DotNetType); if (includeModule) { sb.AppendFormat("module " + type.ModuleName + " {{\r\n"); } sb.AppendLine(" export enum " + type.TypeName + " {"); foreach (var val in values) { var name = Enum.GetName(type.DotNetType, val); sb.AppendFormat(" {0} = {1},\r\n", name, val); } sb.AppendLine(" }"); if (includeModule) { sb.AppendLine("}"); } return(sb.ToString()); }
private string GetTypeName(Type type, GeneratedTypeScript containingType) { if (type.IsPrimitive) { if (type == typeof(bool)) { return("boolean"); } if (type == typeof(char)) { return("string"); } return("number"); } if (type == typeof(decimal)) { return("number"); } if (type == typeof(string)) { return("string"); } if (type == typeof(Guid)) { return("string"); } if (type.IsEnum) { var name = GetNamespace(type.Namespace); if (name == containingType.ModuleName) { return(type.Name); } return(GetFullName(type)); } if (type.IsArray) { var at = type.GetElementType(); return(GetTypeName(at, containingType) + "[]"); } if (typeof(IEnumerable).IsAssignableFrom(type)) { var collectionType = type.GetGenericArguments()[0]; // all my enumerables are typed, so there is a generic argument return(GetTypeName(collectionType, containingType) + "[]"); } if (Nullable.GetUnderlyingType(type) != null) { return(GetTypeName(Nullable.GetUnderlyingType(type), containingType)); } GeneratedTypeScript propertyType; if (_generatedTypes.TryGetValue(type, out propertyType)) { var containingModuleName = GetNamespace(containingType.ModuleName); if (propertyType.ModuleName != containingModuleName) { return(propertyType.ModuleName + "." + propertyType.TypeName); } return(propertyType.TypeName); } return("any"); }
private string GetTypeName(MemberInfo mi, GeneratedTypeScript containingType) { var t = (mi is PropertyInfo) ? ((PropertyInfo)mi).PropertyType : ((FieldInfo)mi).FieldType; return(GetTypeName(t, containingType)); }