private string GetPropertyType(Type type, TypeScriptClass tsClass, bool generic) { var underlyingType = Nullable.GetUnderlyingType(type); var currentType = underlyingType == null ? type : underlyingType; if (currentType.GetTypeInfo().IsGenericType && currentType.Name.Contains("List")) { try { Type genericType = currentType.GenericTypeArguments.FirstOrDefault(); return string.Format("Array<{0}>", GetPropertyType(genericType, tsClass, generic)); } catch (Exception ex) { return ex.Message; } } if (generic) return type.Name; if (currentType.GetTypeInfo().IsEnum) return currentType.Name; if (currentType.Name.Contains("Entity") || currentType.Name == "ExecuteMessage") { tsClass.Imports.Add(new TypeScriptImport { Class = $"I{currentType.Name}", File = GetFileName(currentType.Namespace) }); return $"I{currentType.Name}"; } switch (currentType.Name) { case "Int32": case "Int64": case "Decimal": case "Single": return "number"; case "String": case "Guid": return "string"; case "DateTime": return "Date"; case "Boolean": return "boolean"; default: return "Não identificado: " + currentType.Name; } }
private void LoadProperties() { var nullablePropertys = new string[] { "Keys", "Keys", "PageNumber", "PageSize" }; List<Type> types = Config.Assemblys .SelectMany(c => c.GetTypes()) .Where(c => !c.GetTypeInfo().IsAbstract && !c.GetTypeInfo().IsInterface && !c.GetTypeInfo().IsEnum) .ToList(); types.AddRange(Config.TypesExtra.Where(c => !c.GetTypeInfo().IsAbstract && !c.GetTypeInfo().IsInterface && !c.GetTypeInfo().IsEnum)); foreach (var type in types.Distinct()) { TypeScriptClass tsClass = new TypeScriptClass { Class = type.Name, FileName = GetFileName(type.Namespace), }; foreach (var property in type.GetProperties()) { if (property.GetCustomAttributes<IgnoreDataMemberAttribute>().Count() > 0) continue; var isGeneric = property.PropertyType.IsGenericParameter && !property.Name.Contains("List"); var enumProperty = TSEnums.FirstOrDefault(c => c.Type == (Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType)); var typeName = enumProperty != null ? enumProperty.Name : GetPropertyType(property.PropertyType, tsClass, isGeneric); if (enumProperty != null) { tsClass.Imports.Add(new TypeScriptImport { Class = enumProperty.Name, File = "enum.ts" }); } tsClass.Properties.Add(new TypeScriptProperty { Name = property.Name, Nullable = nullablePropertys.Contains(property.Name) || type.Name.EndsWith("Filter") || Nullable.GetUnderlyingType(property.PropertyType) != null, Generic = isGeneric, Type = typeName }); } TSClass.Add(tsClass); } }