Beispiel #1
0
        BuiltInTypeDef AddBuiltInType(Type type, string code, string fullyQualifiedTsTypeName)
        {
            var name    = GetQualifiedClassName(type);
            var typeDef = new BuiltInTypeDef(name, type, code, fullyQualifiedTsTypeName);

            _types.Add(type, typeDef);

            return(typeDef);
        }
Beispiel #2
0
        TypeDef CreateSpecialTypeDefOrNull(Type type)
        {
            Console.WriteLine("Generating type {0}", type);

            TypeDef typeDef = null;

            if (typeof(IDictionary).IsAssignableFrom(type))
            {
                var dictionaryType = GetClosedGenericInterfaceFromImplementation(type, typeof(IDictionary <,>));
                if (dictionaryType == null)
                {
                    throw new NotSupportedException("Only dictionaries that implement IDictionary<TKey, TValue> is supported");
                }

                var keyType   = dictionaryType.GetGenericArguments()[0];
                var valueType = dictionaryType.GetGenericArguments()[1];
                var keyDef    = GetOrCreateTypeDef(keyType);
                var valueDef  = GetOrCreateTypeDef(valueType);

                if (keyDef.FullyQualifiedTsTypeName != "string")
                {
                    throw new NotSupportedException(
                              string.Format("Only dictionaries with string key is supported. Key: {0}, TypeScript key: {1}",
                                            keyType,
                                            keyDef.FullyQualifiedTsTypeName));
                }

                _generateDictionaryDefinition = false;

                typeDef = new BuiltInTypeDef(new QualifiedClassName("", "Dictionary"), type, "", string.Format("Dictionary<{0}>", valueDef.FullyQualifiedTsTypeName))
                {
                    NoEmit = true
                };
                _types.Add(type, typeDef);
            }
            else if (typeof(IEnumerable).IsAssignableFrom(type))
            {
                if (type.IsArray && type.GetArrayRank() == 1)
                {
                    var elementType = type.GetElementType();

                    var typeDefOfContainedType = GetOrCreateTypeDef(elementType);

                    typeDef = AddBuiltInType(type, "", string.Format("{0}[]", typeDefOfContainedType.FullyQualifiedTsTypeName));
                }
                else if (!type.IsGenericType)
                {
                    typeDef = AddBuiltInType(type, "", "any[]");
                }
                else if (type.IsGenericType && type.GetGenericArguments().Length == 1)
                {
                    var elementType = type.GetGenericArguments()[0];

                    if (!elementType.IsGenericParameter)
                    {
                        var typeDefOfContainedType = GetOrCreateTypeDef(elementType);
                        typeDef = AddBuiltInType(type, "", string.Format("{0}[]", typeDefOfContainedType.FullyQualifiedTsTypeName));
                    }
                    else
                    {
                        typeDef = AddBuiltInType(type, "", string.Format("{0}[]", elementType.Name));
                    }
                }
            }
            else if (type.IsEnum)
            {
                var enumTypeDef = new EnumDef(GetQualifiedClassName(type), CirqusType.Other, type);
                _types[type] = enumTypeDef;
                return(enumTypeDef);
            }

            return(typeDef);
        }