/// <summary> /// Formats a generic type /// </summary> /// <param name="tsGenericType">The generic type</param> /// <returns>The string representation of the generic type</returns> public virtual string Format(TsGenericType tsGenericType) { return(string.Format("{0}{1}", tsGenericType.Name.FullName, tsGenericType.TypeArguments.Count > 0 ? string.Format("<{0}>", string.Join(", ", tsGenericType.TypeArguments.Select(Format))) : string.Empty)); }
public virtual string Format(TsGenericType tsGenericType) { return string.Format("{0}{1}", tsGenericType.Name.FullName, tsGenericType.TypeArguments.Count > 0 ? string.Format("<{0}>", string.Join(", ", tsGenericType.TypeArguments.Select(Format))) : string.Empty); }
protected virtual TsType Resolve(Type type) { // see if we have already processed the type TsType tsType; var isNullable = false; var isArray = false; if (type .GetInterfaces() .Any(t => t.IsGenericType && t.GetGenericTypeDefinition() == typeof(IEnumerable<>))) { if (type != typeof(string)) { isArray = true; type = type.GetGenericArguments()[0]; } } if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>)) { isNullable = true; type = type.GetGenericArguments()[0]; } if (this.TypeLookup.TryGetValue(type, out tsType)) { tsType.IsNullable = isNullable; if (!isArray) { return tsType; } else { var array = new TsArray(tsType, 1); return array; } } // should this assembly be considered? if (this.AssemblyFilter != null && !this.AssemblyFilter(type.Assembly)) return TsPrimitive.Any; // should this assembly be considered? if (this.TypeFilter != null && !this.TypeFilter(type)) return TsPrimitive.Any; if (type.IsGenericParameter) return new TsGenericType(new TsName(type.Name)); else if (type.IsGenericType && !type.IsGenericTypeDefinition) { var tsGenericTypeDefinition = Resolve(type.GetGenericTypeDefinition()); var tsGenericType = new TsGenericType(tsGenericTypeDefinition.Name); foreach (var argument in type.GetGenericArguments()) { var tsArgType = this.Resolve(argument); tsGenericType.TypeArguments.Add(tsArgType); } tsGenericType.IsNullable = isNullable; return tsGenericType; } else if (type.IsArray && type.HasElementType) { var elementType = this.Resolve(type.GetElementType()); var array = new TsArray(elementType, type.GetArrayRank()); array.IsNullable = isNullable; return array; } else if (type.IsEnum) tsType = GenerateEnum(type); else if (type.IsAnsiClass) tsType = GenerateInterface(type); else if (type.IsInterface) tsType = GenerateInterface(type); else tsType = TsPrimitive.Any; tsType.IsNullable = isNullable; // add the lookup if (!this.TypeLookup.ContainsKey(type)) this.TypeLookup.Add(type, tsType); return tsType; }