Ejemplo n.º 1
0
        public static JsiiClassAttribute GetClassAttribute(Type type)
        {
            Type current = type;

            while (current != null)
            {
                // JsiiClassAttribute can't be inheritable, because we need to distinguish between JSII
                // types and native extensions of JSII types. So we have to search the inheritance tree
                // manually.
                JsiiClassAttribute classAttribute = current.GetCustomAttribute <JsiiClassAttribute>();
                if (classAttribute != null)
                {
                    return(classAttribute);
                }

                current = current.BaseType;
            }

            return(null);
        }
Ejemplo n.º 2
0
        TypeReference InferType(IReferenceMap referenceMap, Type type)
        {
            type = type ?? throw new ArgumentNullException(nameof(type));

            JsiiClassAttribute classAttribute = ReflectionUtils.GetClassAttribute(type);

            if (classAttribute != null)
            {
                return(new TypeReference(classAttribute.FullyQualifiedName));
            }

            JsiiEnumAttribute enumAttribute = type.GetCustomAttribute <JsiiEnumAttribute>();

            if (enumAttribute != null)
            {
                return(new TypeReference(enumAttribute.FullyQualifiedName));
            }

            if (type.IsAssignableFrom(typeof(string)))
            {
                return(new TypeReference(primitive: PrimitiveType.String));
            }

            if (type.IsAssignableFrom(typeof(bool)))
            {
                return(new TypeReference(primitive: PrimitiveType.Boolean));
            }

            if (IsNumeric(type))
            {
                return(new TypeReference(primitive: PrimitiveType.Number));
            }

            if (type.IsAssignableFrom(typeof(DateTime)))
            {
                return(new TypeReference(primitive: PrimitiveType.Date));
            }

            if (type.IsAssignableFrom(typeof(JObject)))
            {
                return(new TypeReference(primitive: PrimitiveType.Json));
            }

            if (type.IsArray)
            {
                return(new TypeReference
                       (
                           collection: new CollectionTypeReference
                           (
                               kind: CollectionKind.Array,
                               elementType: InferType(referenceMap, type.GetElementType())
                           )
                       ));
            }

            Type dictionaryInterface = type.GetInterfaces()
                                       .FirstOrDefault(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IDictionary <,>));

            if (dictionaryInterface != null)
            {
                if (!dictionaryInterface.GetGenericArguments()[0].IsAssignableFrom(typeof(string)))
                {
                    throw new ArgumentException("All dictionaries must have string keys", nameof(type));
                }

                Type elementType = dictionaryInterface.GetGenericArguments()[1];
                return(new TypeReference
                       (
                           collection: new CollectionTypeReference
                           (
                               kind: CollectionKind.Map,
                               elementType: InferType(referenceMap, elementType)
                           )
                       ));
            }

            throw new ArgumentException($"Could not infer JSII type for .NET type '{type.Name}'", nameof(type));
        }