Пример #1
0
        protected override bool TryConvertEnum(object value, bool isOptional, string fullyQualifiedName,
                                               out object result)
        {
            if (value == null)
            {
                result = null;
                return(isOptional);
            }

            Type valueType = value.GetType();
            JsiiEnumAttribute attribute = value.GetType().GetCustomAttribute <JsiiEnumAttribute>();

            if (attribute == null || attribute.FullyQualifiedName != fullyQualifiedName)
            {
                result = null;
                return(false);
            }

            string    valueName = Enum.GetName(valueType, value);
            FieldInfo fieldInfo = valueType.GetFields().FirstOrDefault(field => field.Name == valueName);

            if (fieldInfo == null)
            {
                result = null;
                return(false);
            }

            JsiiEnumMemberAttribute memberAttribute = fieldInfo.GetCustomAttribute <JsiiEnumMemberAttribute>();

            if (memberAttribute == null)
            {
                result = null;
                return(false);
            }

            result = JObject.FromObject(new EnumValue(fullyQualifiedName, memberAttribute.Name));
            return(true);
        }
Пример #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));
        }