private TsPropertySymbol GetPropertySymbol(PropertyInfo propertyInfo, DtoTypeSymbolMap dtoSymbolMap) { TsPropertySymbol result = TsPropertySymbol.LoadFrom(propertyInfo, this.symbolLookup, this.options); TsTypeSymbol unwrappedTypeSymbol = result.Type.UnwrapArray(out int arrayRank); if (dtoPropertyTypeSymbolMap.TryGetValue(unwrappedTypeSymbol, out TsTypeSymbol replacementType)) { // We need a type replacement. if (result.Type.IsArray) { replacementType = TsTypeSymbol.CreateArraySymbol(replacementType, arrayRank); } result = new TsPropertySymbol(result.Name, replacementType, result.PropertyMetadata); } else if ((unwrappedTypeSymbol.IsClass || unwrappedTypeSymbol.IsInterface) && this.symbolLookup.ContainsType(unwrappedTypeSymbol.TypeMetadata.Type)) { // We need a DTO Interface for this type. TsTypeSymbol dtoInterfaceType = this.GetOrCreateDtoInterface(unwrappedTypeSymbol, dtoSymbolMap); if (result.Type.IsArray) { dtoInterfaceType = TsTypeSymbol.CreateArraySymbol(dtoInterfaceType, arrayRank); } result = new TsPropertySymbol(result.Name, dtoInterfaceType, result.PropertyMetadata); } return(result); }
private TsTypeSymbol GetOrCreateDtoInterface(TsTypeSymbol typeSymbol, DtoTypeSymbolMap dtoSymbolMap) { TsTypeSymbol unwrappedTypeSymbol = typeSymbol.UnwrapArray(out int rank); if (!dtoSymbolMap.TryGetValue(unwrappedTypeSymbol, out TsTypeSymbol dtoInterfaceSymbol)) { dtoInterfaceSymbol = this.MakeAndRegisterDtoInterface(unwrappedTypeSymbol, dtoSymbolMap); } if (rank > 0) { return(TsTypeSymbol.CreateArraySymbol(dtoInterfaceSymbol, rank)); } return(dtoInterfaceSymbol); }
/// <summary> /// Tries to resolve the symbol. /// </summary> /// <param name="type">The type.</param> /// <param name="symbol">The symbol.</param> /// <returns><c>true</c> if the symbol was resolved, <c>false</c> otherwise.</returns> public bool TryResolveSymbol(Type type, out TsTypeSymbol symbol) { symbol = null; if (type.IsArray) { if (type.GetArrayRank() != 1) { return(false); } if (!this.TryResolveSymbol(type.GetElementType(), out TsTypeSymbol elementSymbol)) { return(false); } symbol = TsTypeSymbol.CreateArraySymbol(elementSymbol); return(true); } if (type.IsConstructedGenericType) { Type typeDefinition = type.GetGenericTypeDefinition(); if (typeDefinition == typeof(IEnumerable <>) || typeDefinition == typeof(ICollection <>) || typeDefinition == typeof(IList <>) || typeDefinition == typeof(IReadOnlyCollection <>) || typeDefinition == typeof(IReadOnlyList <>) || typeDefinition == typeof(List <>)) { Type genericType = type.GetGenericArguments().Single(); if (!this.TryResolveSymbol(genericType, out TsTypeSymbol elementSymbol)) { return(false); } symbol = TsTypeSymbol.CreateArraySymbol(elementSymbol); return(true); } } return(this.lookup.TryGetValue(type.UnwrapNullable(), out symbol)); }