Пример #1
0
        private ITypeBuildingContext GetTypeBuildingContext(string typeLocation, ITypeInfo typeInfo)
        {
            if (BuiltinTypeBuildingContext.Accept(typeInfo))
            {
                return(new BuiltinTypeBuildingContext(typeInfo));
            }

            if (ArrayTypeBuildingContext.Accept(typeInfo))
            {
                return(new ArrayTypeBuildingContext(typeInfo, Options));
            }

            if (DictionaryTypeBuildingContext.Accept(typeInfo))
            {
                return(new DictionaryTypeBuildingContext(typeInfo, Options));
            }

            if (typeInfo.IsEnum)
            {
                var targetUnit = typeUnitFactory.GetOrCreateTypeUnit(typeLocation);
                return(Options.EnumGenerationMode == EnumGenerationMode.FixedStringsAndDictionary
                           ? (ITypeBuildingContext) new FixedStringsAndDictionaryTypeBuildingContext(targetUnit, typeInfo)
                           : new TypeScriptEnumTypeBuildingContext(targetUnit, typeInfo));
            }

            if (typeInfo.IsGenericType && !typeInfo.IsGenericTypeDefinition && typeInfo.GetGenericTypeDefinition().Equals(TypeInfo.From(typeof(Nullable <>))))
            {
                var underlyingType = typeInfo.GetGenericArguments().Single();
                if (Options.EnableExplicitNullability)
                {
                    return(new NullableTypeBuildingContext(underlyingType, Options.UseGlobalNullable));
                }
                return(GetTypeBuildingContext(typeLocation, underlyingType));
            }

            if (typeInfo.IsGenericType && !typeInfo.IsGenericTypeDefinition)
            {
                return(new GenericTypeTypeBuildingContext(typeInfo, Options));
            }

            if (typeInfo.IsGenericParameter)
            {
                return(new GenericParameterTypeBuildingContext(typeInfo));
            }

            if (typeInfo.IsGenericTypeDefinition)
            {
                return(new CustomTypeTypeBuildingContext(typeUnitFactory.GetOrCreateTypeUnit(typeLocation), typeInfo));
            }

            return(new CustomTypeTypeBuildingContext(typeUnitFactory.GetOrCreateTypeUnit(typeLocation), typeInfo));
        }
        private ITypeBuildingContext GetTypeBuildingContext(string typeLocation, ITypeInfo typeInfo)
        {
            if (BuiltinTypeBuildingContext.Accept(typeInfo))
            {
                return(new BuiltinTypeBuildingContext(typeInfo));
            }

            if (ArrayTypeBuildingContext.Accept(typeInfo))
            {
                return(new ArrayTypeBuildingContext(typeInfo));
            }

            if (DictionaryTypeBuildingContext.Accept(typeInfo))
            {
                return(new DictionaryTypeBuildingContext(typeInfo));
            }

            if (TaskTypeBuildingContext.Accept(typeInfo))
            {
                return(new TaskTypeBuildingContext(typeInfo));
            }

            if (NullableTypeBuildingContext.Accept(typeInfo))
            {
                return(new NullableTypeBuildingContext(typeInfo));
            }

            if (typeInfo.IsEnum)
            {
                return(new TypeScriptEnumTypeBuildingContext(typeUnitFactory.GetOrCreateTypeUnit(typeLocation), typeInfo));
            }

            if (typeInfo.IsGenericType && !typeInfo.IsGenericTypeDefinition)
            {
                return(new GenericTypeTypeBuildingContext(typeInfo));
            }

            if (typeInfo.IsGenericParameter)
            {
                return(new GenericParameterTypeBuildingContext(typeInfo));
            }

            if (typeInfo.IsGenericTypeDefinition)
            {
                return(new CustomTypeTypeBuildingContext(typeUnitFactory.GetOrCreateTypeUnit(typeLocation), typeInfo));
            }

            return(new CustomTypeTypeBuildingContext(typeUnitFactory.GetOrCreateTypeUnit(typeLocation), typeInfo));
        }
        private TypeScriptType GetTypeScriptType([NotNull] TypeScriptUnit targetUnit, [NotNull] Type type, [CanBeNull] ICustomAttributeProvider customAttributeProvider)
        {
            customAttributeProvider = ArrayTypeBuildingContext.Accept(type) ? customAttributeProvider : null;
            var typeDeclarationKey = new TypeDeclarationKey(type, customAttributeProvider);

            if (typeDeclarations.ContainsKey(typeDeclarationKey))
            {
                return(typeDeclarations[typeDeclarationKey].ReferenceFrom(targetUnit, this));
            }
            if (type.IsGenericTypeDefinition && type.GetGenericTypeDefinition() == typeof(Nullable <>))
            {
                return(new TypeScriptNullableType(GetTypeScriptType(targetUnit, type.GetGenericArguments()[0], null)));
            }
            var context = ResolveType(type, customAttributeProvider);

            return(context.ReferenceFrom(targetUnit, this));
        }