Beispiel #1
0
        private static TypeDetails Create(Type type)
        {
            var isGenericList = !type.IsPrimitive && type.ImplementsOpenGenericInterface(typeof(IList <>));
            var isList        = !type.IsPrimitive && (isGenericList || typeof(IList).IsAssignableFrom(type));

            var isNullable = type.IsNullable();

            Type elementType;

            if (type.IsArray)
            {
                elementType = type.GetElementType();
            }
            else if (isGenericList)
            {
                var iList = type.GetOpenGenericInterface(typeof(IList <>));

                elementType = iList.GetGenericArguments()[0];
            }
            else
            {
                elementType = null;
            }

            var nonNullType = isNullable ? type.GetGenericArguments()[0] : type;

            var isStringLike = false;

            IJsonConverter converter;

            var jsonConverterAttribute = type.GetCustomAttribute <JsonConverterAttribute>();

            if (jsonConverterAttribute != null)
            {
                converter = jsonConverterAttribute.Converter;
            }
            else if (nonNullType.IsEnum)
            {
                converter = new EnumConverter(nonNullType);
            }
            else if (JsonConverterFactory.Instances.TryGetValue(nonNullType, out converter))
            {
            }
            else if (StringLikeHelper.IsStringLike(nonNullType))
            {
                isStringLike = true;

                converter = new StringLikeConverter(nonNullType);
            }

            return(new TypeDetails(nonNullType)
            {
                NonNullType = nonNullType,
                DefaultValue = type.IsValueType ? Activator.CreateInstance(type) : null,
                IsNullable = isNullable,
                IsList = isList,
                IsStringLike = isStringLike,
                ElementType = elementType,
                JsonConverter = converter
            });
        }
 internal StringLikeConverter(Type type)
 {
     this.type        = type ?? throw new ArgumentNullException(nameof(type));
     this.parseMethod = StringLikeHelper.GetParseMethod(type);
 }