private void ProcessBaseType(IndentedStringBuilder sb, CustomType type) { if (type.BaseType != null) { var baseType = type.BaseType.ClrType; if (baseType.IsGenericType) { baseType = baseType.GetGenericTypeDefinition(); } if (!_processedTypes.Contains(baseType)) { var moduleMember = type.BaseType as IModuleMember; if (moduleMember != null) { if (moduleMember.Module == type.Module) { Render(sb, (dynamic)type.BaseType); } else { RenderModule(moduleMember.Module); } } } } }
private void Render(CustomType type) { _sb.AppendLine("interface {0}{1} {{", _typeNameGenerator.GetTypeName(type), RenderBaseType(type)); _sb.IncreaseIndentation(); foreach (var p in type.Properties) { Render(p); } if (_options.CodeGenerationOptions.AdditionalMembers != null) { foreach (var member in _options.CodeGenerationOptions.AdditionalMembers(type.ClrType)) { _sb.AppendLine(member); } } _sb.DecreaseIndentation(); _sb.AppendLine("}}"); _sb.AppendLine(""); }
public string GetTypeName(CustomType tst) { var type = tst.ClrType; string typeName; if (type.IsGenericTypeDefinition) { string nameOverride; _interfaceNamingOverride.TryGetValue(type, out nameOverride); typeName = string.Format("{0}<{1}>", nameOverride ?? _genericTypeReplacer.Replace(type.Name, ""), string.Join(", ", tst.GenericParameters.Select(p => GetGenericParameterName(p)))); } else if (type.IsGenericType) { var genericParams = tst.GenericArguments; string nameOverride; _interfaceNamingOverride.TryGetValue(type.GetGenericTypeDefinition(), out nameOverride); typeName = string.Format("{0}<{1}>", nameOverride ?? _genericTypeReplacer.Replace(type.Name, ""), string.Join(", ", genericParams.Select(p => _moduleNameGenerator.GetModuleName((dynamic)p) + GetTypeName((dynamic)p)))); } else { typeName = type.Name; } if (tst.DeclaringType != null) { typeName = GetTypeName((dynamic)tst.DeclaringType) + typeName; } return typeName; }
private static string NormalizeName(CustomType t) { string name; if (t.ClrType.IsGenericTypeDefinition) { name = TypeHelper.GetNameOfGenericType(t.ClrType); } else { name = t.ClrType.Name; } return t.Module + "." + name; }
private void Render(IndentedStringBuilder sb, CustomType type) { if (_processedTypes.Contains(type.ClrType)) { return; } _processedTypes.Add(type.ClrType.IsGenericType ? type.ClrType.GetGenericTypeDefinition() : type.ClrType); ProcessBaseType(sb, type); var interfaceType = type as InterfaceType; sb.AppendLine("export {2} {0}{1} {3}{{", _typeNameGenerator.GetTypeName(type), RenderBaseType(type), interfaceType == null ? "class" : "interface", RenderInterfaces(type)); sb.IncreaseIndentation(); foreach (var p in type.Properties) { Render(sb, p); } if (_options.CodeGenerationOptions.AdditionalMembers != null) { foreach (var member in _options.CodeGenerationOptions.AdditionalMembers(type.ClrType)) { sb.AppendLine(member); } } sb.DecreaseIndentation(); sb.AppendLine("}}"); sb.AppendLine(""); }
private string RenderInterfaces(CustomType type) { if (type.ImplementedInterfaces.Count == 0) { return ""; } return string.Format("implements {0} ", string.Join(", ", type.ImplementedInterfaces.Select(i => string.Format("{0}{1}", _moduleNameGenerator.GetModuleName((dynamic)i), _typeNameGenerator.GetTypeName((dynamic)i))))); }
private string RenderBaseType(CustomType type) { if (type.BaseType == null) { return ""; } var baseType = string.Format(" extends {0}{1}", _moduleNameGenerator.GetModuleName((dynamic)type.BaseType), _typeNameGenerator.GetTypeName((dynamic)type.BaseType)); return baseType; }
private TypeScriptType ProcessTypeScriptType(Type t, CustomType tst) { TypeScriptType processedType; if (!_processedTypes.TryGetValue(t, out processedType)) { if (!(t.IsGenericType && !t.IsGenericTypeDefinition)) { _processedTypes.Add(t, tst); } else if (t.IsGenericType) { ProcessTypeScriptType(t.GetGenericTypeDefinition(), (dynamic)GetTypeScriptType(t.GetGenericTypeDefinition())); } processedType = tst; bool skippedBaseType; var baseType = GetBaseType(t, out skippedBaseType); if (baseType != null) { if (_options.BaseTypeFilter(baseType)) { var processedBaseType = ProcessTypeScriptType(baseType, (dynamic)GetTypeScriptType(baseType)); tst.BaseType = processedBaseType; } tst.IncludeInheritedProperties = skippedBaseType; } ProcessProperties(tst); tst.Module = _options.ModuleNameGenerator(t); ProcessGenericArguments(t, tst); ProcessGenericConstraints(t, tst); ProcessNestedType(t, tst); ProcessInterfaces(t, tst); } return processedType; }
private void ProcessProperties(CustomType tst) { BindingFlags flags; if (tst.IncludeInheritedProperties) { flags = BindingFlags.Instance | BindingFlags.Public; } else { flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly; } var properties = tst.ClrType.GetProperties(flags).Cast<MemberInfo>().Union(tst.ClrType.GetFields(flags)); foreach (var property in properties) { if (_options.PropertyFilter != null) { var processProperty = _options.PropertyFilter(property); if (!processProperty) { continue; } } var type = property.MemberType == MemberTypes.Property ? ((PropertyInfo)property).PropertyType : ((FieldInfo)property).FieldType; var propertyTst = ProcessTypeScriptType(type, (dynamic)GetTypeScriptType(type)); tst.Properties.Add(new TypeScriptProperty { Property = property, Type = propertyTst }); } }
private void ProcessNestedType(Type t, CustomType tst) { if (t.IsNestedPublic && t.DeclaringType != null) { var declaringTypeTst = ProcessTypeScriptType(t.DeclaringType, (dynamic)GetTypeScriptType(t.DeclaringType)); tst.DeclaringType = declaringTypeTst; } }
private void ProcessInterfaces(Type t, CustomType tst) { var implementedInterfaces = t.BaseType != null ? t.GetInterfaces().Except(t.BaseType.GetInterfaces()) : t.GetInterfaces(); foreach (var implementedInterface in implementedInterfaces) { TypeScriptType processedInterfaceType = ProcessTypeScriptType(implementedInterface, (dynamic)GetTypeScriptType(implementedInterface)); if (processedInterfaceType is InterfaceType) { tst.ImplementedInterfaces.Add(processedInterfaceType); } } }
private void ProcessGenericConstraints(Type t, CustomType tst) { if (t.IsGenericTypeDefinition) { tst.GenericParameters = new List<GenericParameter>(); foreach (var param in t.GetGenericArguments()) { var genericParam = new GenericParameter() { GenericConstraints = new List<TypeScriptType>(), ClrGenericArgument = param }; tst.GenericParameters.Add(genericParam); var constraints = param.GetGenericParameterConstraints(); if (constraints.Any()) { foreach (var constraint in constraints) { var constraintArg = ProcessTypeScriptType(constraint, (dynamic)GetTypeScriptType(constraint)); genericParam.GenericConstraints.Add((TypeScriptType)constraintArg); } } } } }
private void ProcessGenericArguments(Type t, CustomType tst) { if (t.IsGenericType && !t.IsGenericTypeDefinition) { var baseTypeGenericArguments = t.GetGenericArguments(); tst.GenericArguments = new List<TypeScriptType>(); foreach (var arg in baseTypeGenericArguments) { var baseGenericArgTst = ProcessTypeScriptType(arg, (dynamic)GetTypeScriptType(arg)); tst.GenericArguments.Add((TypeScriptType)baseGenericArgTst); } } }
private TypeScriptType GetTypeScriptType(Type type) { TypeScriptType tst; if (TypeHelper.Is(type, typeof(string))) { tst = new StringType(); } else if (TypeHelper.Is(type, typeof(bool))) { tst = new BooleanType(); } else if (TypeHelper.Is(type, typeof(int), typeof(decimal), typeof(double), typeof(long), typeof(float), typeof(short), typeof(byte), typeof(uint), typeof(ushort), typeof(ulong), typeof(sbyte) )) { tst = new NumberType(); } else if (TypeHelper.Is(type, typeof(DateTime))) { tst = new DateTimeType(); } else if (TypeHelper.Is(type, typeof(TimeSpan))) { tst = new TimeSpanType(); } else if (type.IsGenericParameter) { tst = new GenericTypeParameter(); } else if (TypeHelper.IsDictionary(type)) { tst = new DictionaryType(); } else if (TypeHelper.IsEnumerable(type)) { tst = new ArrayType(); } else if (TypeHelper.IsEnum(type)) { tst = new EnumType(); } else { var processType = _options.TypeFilter(type); if (processType) { if (type.IsInterface) { tst = new InterfaceType(type); } else { tst = new CustomType(type); } } else { tst = new AnyType(); } } if (TypeHelper.IsNullableValueType(type)) { ((ValueType)tst).IsNullable = true; type = Nullable.GetUnderlyingType(type); } tst.ClrType = type; return tst; }
private string RenderBaseType(CustomType type) { var baseTypes = new List<TypeScriptType>(); if (type.BaseType != null) { baseTypes.Add(type.BaseType); } baseTypes.AddRange(type.ImplementedInterfaces); if (baseTypes.Count == 0) { return ""; } var baseType = string.Format(" extends {0}", string.Join(", ", baseTypes.Select(b => string.Format("{0}{1}", _moduleNameGenerator.GetModuleName((dynamic)b), _typeNameGenerator.GetTypeName((dynamic)b))))); return baseType; }
public string GetModuleName(CustomType type) { return type.Module + "."; }