Esempio n. 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="JsonArrayContract"/> class.
        /// </summary>
        /// <param name="underlyingType">The underlying type for the contract.</param>
        public JsonArrayContract(Type underlyingType)
            : base(underlyingType)
        {
            ContractType = JsonContractType.Array;

            if (ReflectionUtils.ImplementsGenericDefinition(underlyingType, typeof(ICollection <>), out _genericCollectionDefinitionType))
            {
                CollectionItemType = _genericCollectionDefinitionType.GetGenericArguments()[0];
            }
            else if (underlyingType.IsGenericType() && underlyingType.GetGenericTypeDefinition() == typeof(IEnumerable <>))
            {
                _genericCollectionDefinitionType = typeof(IEnumerable <>);
                CollectionItemType = underlyingType.GetGenericArguments()[0];
            }
            else
            {
                CollectionItemType = ReflectionUtils.GetCollectionItemType(UnderlyingType);
            }

            if (CollectionItemType != null)
            {
                _isCollectionItemTypeNullableType = ReflectionUtils.IsNullableType(CollectionItemType);
            }

            if (IsTypeGenericCollectionInterface(UnderlyingType))
            {
                CreatedType = ReflectionUtils.MakeGenericType(typeof(List <>), CollectionItemType);
            }

            IsMultidimensionalArray = (UnderlyingType.IsArray && UnderlyingType.GetArrayRank() > 1);
        }
Esempio n. 2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="JsonArrayContract"/> class.
        /// </summary>
        /// <param name="underlyingType">The underlying type for the contract.</param>
        public JsonArrayContract(Type underlyingType)
            : base(underlyingType)
        {
            ContractType = JsonContractType.Array;

            if (ReflectionUtils.ImplementsGenericDefinition(underlyingType, typeof(ICollection <>), out _genericCollectionDefinitionType))
            {
                CollectionItemType = _genericCollectionDefinitionType.GetGenericArguments()[0];
            }
            else if (underlyingType.IsGenericType() && underlyingType.GetGenericTypeDefinition() == typeof(IEnumerable <>))
            {
                _genericCollectionDefinitionType = typeof(IEnumerable <>);
                CollectionItemType = underlyingType.GetGenericArguments()[0];
            }
#if !(NET40 || NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE || PORTABLE)
            else if (underlyingType.IsGenericType() && underlyingType.GetGenericTypeDefinition() == typeof(IReadOnlyCollection <>))
            {
                CollectionItemType = underlyingType.GetGenericArguments()[0];
            }
#endif
            else
            {
                CollectionItemType = ReflectionUtils.GetCollectionItemType(UnderlyingType);
            }

            if (CollectionItemType != null)
            {
                _isCollectionItemTypeNullableType = ReflectionUtils.IsNullableType(CollectionItemType);
            }

            if (IsTypeGenericCollectionInterface(UnderlyingType))
            {
                CreatedType = ReflectionUtils.MakeGenericType(typeof(List <>), CollectionItemType);
            }
#if !(NET40 || NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE || PORTABLE)
            else if (IsTypeGenericReadOnlyCollectionInterface(UnderlyingType))
            {
                CreatedType = ReflectionUtils.MakeGenericType(typeof(ReadOnlyCollection <>), CollectionItemType);
            }
#endif
#if !(NET20 || NET35)
            else if (IsTypeGenericSetInterface(UnderlyingType))
            {
                CreatedType = ReflectionUtils.MakeGenericType(typeof(HashSet <>), CollectionItemType);
            }
#endif

            IsMultidimensionalArray = (UnderlyingType.IsArray && UnderlyingType.GetArrayRank() > 1);
        }
Esempio n. 3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="JsonArrayContract"/> class.
        /// </summary>
        /// <param name="underlyingType">The underlying type for the contract.</param>
        public JsonArrayContract(Type underlyingType)
            : base(underlyingType)
        {
            if (ReflectionUtils.ImplementsGenericDefinition(underlyingType, typeof(ICollection <>), out _genericCollectionDefinitionType))
            {
                CollectionItemType = _genericCollectionDefinitionType.GetGenericArguments()[0];
            }
            else if (underlyingType.IsGenericType && underlyingType.GetGenericTypeDefinition() == typeof(IEnumerable <>))
            {
                _genericCollectionDefinitionType = typeof(IEnumerable <>);
                CollectionItemType = underlyingType.GetGenericArguments()[0];
            }
            else
            {
                CollectionItemType = ReflectionUtils.GetCollectionItemType(UnderlyingType);
            }

            if (CollectionItemType != null)
            {
                _isCollectionItemTypeNullableType = ReflectionUtils.IsNullableType(CollectionItemType);
            }

            if (IsTypeGenericCollectionInterface(UnderlyingType))
            {
#if (UNITY_IPHONE || UNITY_IOS)
                //if (underlyingType is ISerializable)
                if (typeof(ISerializable).IsAssignableFrom(underlyingType))
                {
                    CreatedType = ReflectionUtils.MakeGenericType(typeof(HashSet <>), CollectionItemType);
                    return;
                }
#endif
                CreatedType = ReflectionUtils.MakeGenericType(typeof(List <>), CollectionItemType);
            }
            else if (typeof(HashSet <>).IsAssignableFrom(UnderlyingType))
            {
                CreatedType = ReflectionUtils.MakeGenericType(typeof(HashSet <>), CollectionItemType);
            }

            IsMultidimensionalArray = (UnderlyingType.IsArray && UnderlyingType.GetArrayRank() > 1);
        }
Esempio n. 4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="JsonArrayContract"/> class.
        /// </summary>
        /// <param name="underlyingType">The underlying type for the contract.</param>
        public JsonArrayContract(Type underlyingType)
            : base(underlyingType)
        {
            ContractType = JsonContractType.Array;
            IsArray      = CreatedType.IsArray;

            bool canDeserialize;

            Type tempCollectionType;

            if (IsArray)
            {
                CollectionItemType               = ReflectionUtils.GetCollectionItemType(UnderlyingType);
                IsReadOnlyOrFixedSize            = true;
                _genericCollectionDefinitionType = typeof(List <>).MakeGenericType(CollectionItemType);

                canDeserialize          = true;
                IsMultidimensionalArray = (IsArray && UnderlyingType.GetArrayRank() > 1);
            }
            else if (typeof(IList).IsAssignableFrom(underlyingType))
            {
                if (ReflectionUtils.ImplementsGenericDefinition(underlyingType, typeof(ICollection <>), out _genericCollectionDefinitionType))
                {
                    CollectionItemType = _genericCollectionDefinitionType.GetGenericArguments()[0];
                }
                else
                {
                    CollectionItemType = ReflectionUtils.GetCollectionItemType(underlyingType);
                }

                if (underlyingType == typeof(IList))
                {
                    CreatedType = typeof(List <object>);
                }

                if (CollectionItemType != null)
                {
                    _parameterizedConstructor = CollectionUtils.ResolveEnumerableCollectionConstructor(underlyingType, CollectionItemType);
                }

                IsReadOnlyOrFixedSize = ReflectionUtils.InheritsGenericDefinition(underlyingType, typeof(ReadOnlyCollection <>));
                canDeserialize        = true;
            }
            else if (ReflectionUtils.ImplementsGenericDefinition(underlyingType, typeof(ICollection <>), out _genericCollectionDefinitionType))
            {
                CollectionItemType = _genericCollectionDefinitionType.GetGenericArguments()[0];

                if (ReflectionUtils.IsGenericDefinition(underlyingType, typeof(ICollection <>)) ||
                    ReflectionUtils.IsGenericDefinition(underlyingType, typeof(IList <>)))
                {
                    CreatedType = typeof(List <>).MakeGenericType(CollectionItemType);
                }

#if !(NET20 || NET35)
                if (ReflectionUtils.IsGenericDefinition(underlyingType, typeof(ISet <>)))
                {
                    CreatedType = typeof(HashSet <>).MakeGenericType(CollectionItemType);
                }
#endif

                _parameterizedConstructor = CollectionUtils.ResolveEnumerableCollectionConstructor(underlyingType, CollectionItemType);
                canDeserialize            = true;
                ShouldCreateWrapper       = true;
            }
#if !(NET40 || NET35 || NET20 || PORTABLE40)
            else if (ReflectionUtils.ImplementsGenericDefinition(underlyingType, typeof(IReadOnlyCollection <>), out tempCollectionType))
            {
                CollectionItemType = tempCollectionType.GetGenericArguments()[0];

                if (ReflectionUtils.IsGenericDefinition(underlyingType, typeof(IReadOnlyCollection <>)) ||
                    ReflectionUtils.IsGenericDefinition(underlyingType, typeof(IReadOnlyList <>)))
                {
                    CreatedType = typeof(ReadOnlyCollection <>).MakeGenericType(CollectionItemType);
                }

                _genericCollectionDefinitionType = typeof(List <>).MakeGenericType(CollectionItemType);
                _parameterizedConstructor        = CollectionUtils.ResolveEnumerableCollectionConstructor(CreatedType, CollectionItemType);
                IsReadOnlyOrFixedSize            = true;
                canDeserialize = HasParameterizedCreatorInternal;
            }
#endif
            else if (ReflectionUtils.ImplementsGenericDefinition(underlyingType, typeof(IEnumerable <>), out tempCollectionType))
            {
                CollectionItemType = tempCollectionType.GetGenericArguments()[0];

                if (ReflectionUtils.IsGenericDefinition(UnderlyingType, typeof(IEnumerable <>)))
                {
                    CreatedType = typeof(List <>).MakeGenericType(CollectionItemType);
                }

                _parameterizedConstructor = CollectionUtils.ResolveEnumerableCollectionConstructor(underlyingType, CollectionItemType);

#if !(NET35 || NET20)
                if (!HasParameterizedCreatorInternal && underlyingType.Name == FSharpUtils.FSharpListTypeName)
                {
                    FSharpUtils.EnsureInitialized(underlyingType.Assembly());
                    _parameterizedCreator = FSharpUtils.CreateSeq(CollectionItemType);
                }
#endif

                if (underlyingType.IsGenericType() && underlyingType.GetGenericTypeDefinition() == typeof(IEnumerable <>))
                {
                    _genericCollectionDefinitionType = tempCollectionType;

                    IsReadOnlyOrFixedSize = false;
                    ShouldCreateWrapper   = false;
                    canDeserialize        = true;
                }
                else
                {
                    _genericCollectionDefinitionType = typeof(List <>).MakeGenericType(CollectionItemType);

                    IsReadOnlyOrFixedSize = true;
                    ShouldCreateWrapper   = true;
                    canDeserialize        = HasParameterizedCreatorInternal;
                }
            }
            else
            {
                // types that implement IEnumerable and nothing else
                canDeserialize      = false;
                ShouldCreateWrapper = true;
            }

            CanDeserialize = canDeserialize;

#if (NET20 || NET35)
            if (CollectionItemType != null && ReflectionUtils.IsNullableType(CollectionItemType))
            {
                // bug in .NET 2.0 & 3.5 that List<Nullable<T>> throws an error when adding null via IList.Add(object)
                // wrapper will handle calling Add(T) instead
                if (ReflectionUtils.InheritsGenericDefinition(CreatedType, typeof(List <>), out tempCollectionType) ||
                    (IsArray && !IsMultidimensionalArray))
                {
                    ShouldCreateWrapper = true;
                }
            }
#endif

#if !(NET20 || NET35 || NET40)
            Type immutableCreatedType;
            ObjectConstructor <object> immutableParameterizedCreator;
            if (ImmutableCollectionsUtils.TryBuildImmutableForArrayContract(underlyingType, CollectionItemType, out immutableCreatedType, out immutableParameterizedCreator))
            {
                CreatedType           = immutableCreatedType;
                _parameterizedCreator = immutableParameterizedCreator;
                IsReadOnlyOrFixedSize = true;
                CanDeserialize        = true;
            }
#endif
        }
Esempio n. 5
0
        /// <summary>
        /// Initializes a new instance of the <see cref="JsonArrayContract"/> class.
        /// </summary>
        /// <param name="underlyingType">The underlying type for the contract.</param>
        public JsonArrayContract(Type underlyingType)
            : base(underlyingType)
        {
            ContractType = JsonContractType.Array;
            IsArray      = CreatedType.IsArray;

            bool canDeserialize;

            Type tempCollectionType;

            if (IsArray)
            {
                CollectionItemType               = ReflectionUtils.GetCollectionItemType(UnderlyingType);
                IsReadOnlyOrFixedSize            = true;
                _genericCollectionDefinitionType = typeof(List <>).MakeGenericType(CollectionItemType);

                canDeserialize          = true;
                IsMultidimensionalArray = (IsArray && UnderlyingType.GetArrayRank() > 1);
            }
            else if (typeof(IList).IsAssignableFrom(underlyingType))
            {
                if (ReflectionUtils.ImplementsGenericDefinition(underlyingType, typeof(ICollection <>), out _genericCollectionDefinitionType))
                {
                    CollectionItemType = _genericCollectionDefinitionType.GetGenericArguments()[0];
                }
                else
                {
                    CollectionItemType = ReflectionUtils.GetCollectionItemType(underlyingType);
                }

                if (underlyingType == typeof(IList))
                {
                    CreatedType = typeof(List <object>);
                }

                if (CollectionItemType != null)
                {
                    ParametrizedConstructor = CollectionUtils.ResolveEnumableCollectionConstructor(underlyingType, CollectionItemType);
                }

                IsReadOnlyOrFixedSize = ReflectionUtils.InheritsGenericDefinition(underlyingType, typeof(ReadOnlyCollection <>));
                canDeserialize        = true;
            }
            else if (ReflectionUtils.ImplementsGenericDefinition(underlyingType, typeof(ICollection <>), out _genericCollectionDefinitionType))
            {
                CollectionItemType = _genericCollectionDefinitionType.GetGenericArguments()[0];

                if (ReflectionUtils.IsGenericDefinition(underlyingType, typeof(ICollection <>)) ||
                    ReflectionUtils.IsGenericDefinition(underlyingType, typeof(IList <>)))
                {
                    CreatedType = typeof(List <>).MakeGenericType(CollectionItemType);
                }

                if (ReflectionUtils.IsGenericDefinition(underlyingType, typeof(ISet <>)))
                {
                    CreatedType = typeof(HashSet <>).MakeGenericType(CollectionItemType);
                }

                ParametrizedConstructor = CollectionUtils.ResolveEnumableCollectionConstructor(underlyingType, CollectionItemType);
                canDeserialize          = true;
                ShouldCreateWrapper     = true;
            }
//#if !(NET40 || NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE || PORTABLE40)
            else if (ReflectionUtils.ImplementsGenericDefinition(underlyingType, typeof(IReadOnlyCollection <>), out tempCollectionType))
            {
                CollectionItemType = underlyingType.GetGenericArguments()[0];

                if (ReflectionUtils.IsGenericDefinition(underlyingType, typeof(IReadOnlyCollection <>)) ||
                    ReflectionUtils.IsGenericDefinition(underlyingType, typeof(IReadOnlyList <>)))
                {
                    CreatedType = typeof(ReadOnlyCollection <>).MakeGenericType(CollectionItemType);
                }

                _genericCollectionDefinitionType = typeof(List <>).MakeGenericType(CollectionItemType);
                ParametrizedConstructor          = CollectionUtils.ResolveEnumableCollectionConstructor(CreatedType, CollectionItemType);
                IsReadOnlyOrFixedSize            = true;
                canDeserialize = (ParametrizedConstructor != null);
            }
//#endif
            else if (ReflectionUtils.ImplementsGenericDefinition(underlyingType, typeof(IEnumerable <>), out tempCollectionType))
            {
                CollectionItemType = tempCollectionType.GetGenericArguments()[0];

                if (ReflectionUtils.IsGenericDefinition(UnderlyingType, typeof(IEnumerable <>)))
                {
                    CreatedType = typeof(List <>).MakeGenericType(CollectionItemType);
                }

                ParametrizedConstructor = CollectionUtils.ResolveEnumableCollectionConstructor(underlyingType, CollectionItemType);

                if (underlyingType.IsGenericType() && underlyingType.GetGenericTypeDefinition() == typeof(IEnumerable <>))
                {
                    _genericCollectionDefinitionType = tempCollectionType;

                    IsReadOnlyOrFixedSize = false;
                    ShouldCreateWrapper   = false;
                    canDeserialize        = true;
                }
                else
                {
                    _genericCollectionDefinitionType = typeof(List <>).MakeGenericType(CollectionItemType);

                    IsReadOnlyOrFixedSize = true;
                    ShouldCreateWrapper   = true;
                    canDeserialize        = (ParametrizedConstructor != null);
                }
            }
            else
            {
                // types that implement IEnumerable and nothing else
                canDeserialize      = false;
                ShouldCreateWrapper = true;
            }

            CanDeserialize = canDeserialize;

            if (CollectionItemType != null)
            {
                _isCollectionItemTypeNullableType = ReflectionUtils.IsNullableType(CollectionItemType);
            }

//#if !(NET20 || NET35 || NET40 || PORTABLE40)
            Type       immutableCreatedType;
            MethodBase immutableParameterizedCreator;

            if (ImmutableCollectionsUtils.TryBuildImmutableForArrayContract(underlyingType, CollectionItemType, out immutableCreatedType, out immutableParameterizedCreator))
            {
                CreatedType             = immutableCreatedType;
                ParametrizedConstructor = immutableParameterizedCreator;
                IsReadOnlyOrFixedSize   = true;
                CanDeserialize          = true;
            }
//#endif
        }
Esempio n. 6
0
        /// <summary>
        /// Initializes a new instance of the <see cref="JsonArrayContract"/> class.
        /// </summary>
        /// <param name="underlyingType">The underlying type for the contract.</param>
        public JsonArrayContract(Type underlyingType)
            : base(underlyingType)
        {
            ContractType = JsonContractType.Array;
            IsArray      = CreatedType.IsArray;

            bool canDeserialize;

            Type tempCollectionType;

            if (IsArray)
            {
                CollectionItemType               = ReflectionUtils.GetCollectionItemType(UnderlyingType);
                IsReadOnlyOrFixedSize            = true;
                _genericCollectionDefinitionType = typeof(List <>).MakeGenericType(CollectionItemType);

                canDeserialize          = true;
                IsMultidimensionalArray = (IsArray && UnderlyingType.GetArrayRank() > 1);
            }
            else if (typeof(IList).IsAssignableFrom(underlyingType))
            {
                if (ReflectionUtils.ImplementsGenericDefinition(underlyingType, typeof(ICollection <>), out _genericCollectionDefinitionType))
                {
                    CollectionItemType = _genericCollectionDefinitionType.GetGenericArguments()[0];
                }
                else
                {
                    CollectionItemType = ReflectionUtils.GetCollectionItemType(underlyingType);
                }

                if (underlyingType == typeof(IList))
                {
                    CreatedType = typeof(List <object>);
                }

                if (CollectionItemType != null)
                {
                    _parametrizedConstructor = CollectionUtils.ResolveEnumerableCollectionConstructor(underlyingType, CollectionItemType);
                }

                IsReadOnlyOrFixedSize = ReflectionUtils.InheritsGenericDefinition(underlyingType, typeof(ReadOnlyCollection <>));
                canDeserialize        = true;
            }
            else if (ReflectionUtils.ImplementsGenericDefinition(underlyingType, typeof(ICollection <>), out _genericCollectionDefinitionType))
            {
                CollectionItemType = _genericCollectionDefinitionType.GetGenericArguments()[0];

                if (ReflectionUtils.IsGenericDefinition(underlyingType, typeof(ICollection <>)) ||
                    ReflectionUtils.IsGenericDefinition(underlyingType, typeof(IList <>)))
                {
                    CreatedType = typeof(List <>).MakeGenericType(CollectionItemType);
                }

                if (ReflectionUtils.IsGenericDefinition(underlyingType, typeof(ISet <>)))
                {
                    CreatedType = typeof(HashSet <>).MakeGenericType(CollectionItemType);
                }

                _parametrizedConstructor = CollectionUtils.ResolveEnumerableCollectionConstructor(underlyingType, CollectionItemType);
                canDeserialize           = true;
                ShouldCreateWrapper      = true;
            }
            else if (ReflectionUtils.ImplementsGenericDefinition(underlyingType, typeof(IEnumerable <>), out tempCollectionType))
            {
                CollectionItemType = tempCollectionType.GetGenericArguments()[0];

                if (ReflectionUtils.IsGenericDefinition(UnderlyingType, typeof(IEnumerable <>)))
                {
                    CreatedType = typeof(List <>).MakeGenericType(CollectionItemType);
                }

                _parametrizedConstructor = CollectionUtils.ResolveEnumerableCollectionConstructor(underlyingType, CollectionItemType);

#if !(NET35 || NET20 || NETFX_CORE)
                if (!HasParametrizedCreator && underlyingType.Name == FSharpUtils.FSharpListTypeName)
                {
                    FSharpUtils.EnsureInitialized(underlyingType.Assembly());
                    _parametrizedCreator = FSharpUtils.CreateSeq(CollectionItemType);
                }
#endif

                if (underlyingType.IsGenericType() && underlyingType.GetGenericTypeDefinition() == typeof(IEnumerable <>))
                {
                    _genericCollectionDefinitionType = tempCollectionType;

                    IsReadOnlyOrFixedSize = false;
                    ShouldCreateWrapper   = false;
                    canDeserialize        = true;
                }
                else
                {
                    _genericCollectionDefinitionType = typeof(List <>).MakeGenericType(CollectionItemType);

                    IsReadOnlyOrFixedSize = true;
                    ShouldCreateWrapper   = true;
                    canDeserialize        = HasParametrizedCreator;
                }
            }
            else
            {
                // types that implement IEnumerable and nothing else
                canDeserialize      = false;
                ShouldCreateWrapper = true;
            }

            CanDeserialize = canDeserialize;
        }
Esempio n. 7
0
        /// <summary>
        /// Initializes a new instance of the <see cref="JsonArrayContract"/> class.
        /// </summary>
        /// <param name="underlyingType">The underlying type for the contract.</param>
        public JsonArrayContract(Type underlyingType)
            : base(underlyingType)
        {
            ContractType = JsonContractType.Array;

            bool canDeserialize;

            Type tempCollectionType;

            if (CreatedType.IsArray)
            {
                CollectionItemType               = ReflectionUtils.GetCollectionItemType(UnderlyingType);
                IsReadOnlyOrFixedSize            = true;
                _genericCollectionDefinitionType = typeof(List <>).MakeGenericType(CollectionItemType);

                canDeserialize          = true;
                IsMultidimensionalArray = (UnderlyingType.IsArray && UnderlyingType.GetArrayRank() > 1);
            }
            else if (typeof(IList).IsAssignableFrom(underlyingType))
            {
                if (ReflectionUtils.ImplementsGenericDefinition(underlyingType, typeof(ICollection <>), out _genericCollectionDefinitionType))
                {
                    CollectionItemType = _genericCollectionDefinitionType.GetGenericArguments()[0];
                }
                else
                {
                    CollectionItemType = ReflectionUtils.GetCollectionItemType(underlyingType);
                }

                if (underlyingType == typeof(IList))
                {
                    CreatedType = typeof(List <object>);
                }

                if (CollectionItemType != null)
                {
                    ParametrizedConstructor = CollectionUtils.ResolveEnumableCollectionConstructor(underlyingType, CollectionItemType);
                }

                IsReadOnlyOrFixedSize = ReflectionUtils.InheritsGenericDefinition(underlyingType, typeof(ReadOnlyCollection <>));
                canDeserialize        = true;
            }
            else if (ReflectionUtils.ImplementsGenericDefinition(underlyingType, typeof(ICollection <>), out _genericCollectionDefinitionType))
            {
                CollectionItemType = _genericCollectionDefinitionType.GetGenericArguments()[0];

                if (ReflectionUtils.IsGenericDefinition(underlyingType, typeof(ICollection <>)) ||
                    ReflectionUtils.IsGenericDefinition(underlyingType, typeof(IList <>)))
                {
                    CreatedType = typeof(List <>).MakeGenericType(CollectionItemType);
                }

#if !(NET20 || NET35 || PORTABLE40)
                if (ReflectionUtils.IsGenericDefinition(underlyingType, typeof(ISet <>)))
                {
                    CreatedType = typeof(HashSet <>).MakeGenericType(CollectionItemType);
                }
#endif

                ParametrizedConstructor = CollectionUtils.ResolveEnumableCollectionConstructor(underlyingType, CollectionItemType);
                canDeserialize          = true;
                ShouldCreateWrapper     = true;
            }
#if !(NET40 || NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE || PORTABLE40)
            else if (ReflectionUtils.ImplementsGenericDefinition(underlyingType, typeof(IReadOnlyCollection <>), out tempCollectionType))
            {
                CollectionItemType = underlyingType.GetGenericArguments()[0];

                if (ReflectionUtils.IsGenericDefinition(underlyingType, typeof(IReadOnlyCollection <>)) ||
                    ReflectionUtils.IsGenericDefinition(underlyingType, typeof(IReadOnlyList <>)))
                {
                    CreatedType = typeof(ReadOnlyCollection <>).MakeGenericType(CollectionItemType);
                }

                _genericCollectionDefinitionType = typeof(List <>).MakeGenericType(CollectionItemType);
                ParametrizedConstructor          = CollectionUtils.ResolveEnumableCollectionConstructor(CreatedType, CollectionItemType);
                IsReadOnlyOrFixedSize            = true;
                canDeserialize = (ParametrizedConstructor != null);
            }
#endif
            else if (ReflectionUtils.ImplementsGenericDefinition(underlyingType, typeof(IEnumerable <>), out tempCollectionType))
            {
                CollectionItemType = tempCollectionType.GetGenericArguments()[0];

                if (ReflectionUtils.IsGenericDefinition(UnderlyingType, typeof(IEnumerable <>)))
                {
                    CreatedType = typeof(List <>).MakeGenericType(CollectionItemType);
                }

                ParametrizedConstructor = CollectionUtils.ResolveEnumableCollectionConstructor(underlyingType, CollectionItemType);

                if (underlyingType.IsGenericType() && underlyingType.GetGenericTypeDefinition() == typeof(IEnumerable <>))
                {
                    _genericCollectionDefinitionType = tempCollectionType;

                    IsReadOnlyOrFixedSize = false;
                    ShouldCreateWrapper   = false;
                    canDeserialize        = true;
                }
                else
                {
                    _genericCollectionDefinitionType = typeof(List <>).MakeGenericType(CollectionItemType);

                    IsReadOnlyOrFixedSize = true;
                    ShouldCreateWrapper   = true;
                    canDeserialize        = (ParametrizedConstructor != null);
                }
            }
            else
            {
                // types that implement IEnumerable and nothing else
                canDeserialize      = false;
                ShouldCreateWrapper = true;
            }

            CanDeserialize = canDeserialize;

            if (CollectionItemType != null)
            {
                _isCollectionItemTypeNullableType = ReflectionUtils.IsNullableType(CollectionItemType);
            }

#if (NET20 || NET35)
            // bug in .NET 2.0 & 3.5 that List<Nullable<T>> throws an error when adding null via IList.Add(object)
            // wrapper will handle calling Add(T) instead
            if (_isCollectionItemTypeNullableType &&
                (ReflectionUtils.InheritsGenericDefinition(CreatedType, typeof(List <>), out tempCollectionType) ||
                 (CreatedType.IsArray && !IsMultidimensionalArray)))
            {
                ShouldCreateWrapper = true;
            }
#endif
        }
Esempio n. 8
0
        /// <summary>
        /// Initializes a new instance of the <see cref="JsonArrayContract"/> class.
        /// </summary>
        /// <param name="underlyingType">The underlying type for the contract.</param>
        public JsonArrayContract(Type underlyingType)
            : base(underlyingType)
        {
            ContractType = JsonContractType.Array;
            IsArray      = CreatedType.IsArray;

            bool canDeserialize;

            Type tempCollectionType;

            if (IsArray)
            {
                CollectionItemType = ReflectionUtils.GetCollectionItemType(UnderlyingType);

                Type MakeType = CollectionItemType;
                if (MakeType is ILRuntimeType)
                {
                    MakeType = (MakeType as ILRuntimeType).UnderlyingSystemType;
                }
                else if (MakeType is ILRuntimeWrapperType)
                {
                    MakeType = (MakeType as ILRuntimeWrapperType).RealType;
                }

                IsReadOnlyOrFixedSize            = true;
                _genericCollectionDefinitionType = typeof(List <>).MakeGenericType(MakeType);

                canDeserialize = true;

                if (UnderlyingType is ILRuntimeType iLRuntimeType)
                {
                    IsMultidimensionalArray = false;
                }
                else
                {
                    IsMultidimensionalArray = (IsArray && UnderlyingType.GetArrayRank() > 1);
                }
            }
            else if (typeof(IList).IsAssignableFrom(underlyingType))
            {
                if (underlyingType is ILRuntimeWrapperType wrapperType)
                {
                    CollectionItemType = wrapperType.CLRType.GenericArguments[0].Value.ReflectionType;
                }
                else if (ReflectionUtils.ImplementsGenericDefinition(underlyingType, typeof(ICollection <>), out _genericCollectionDefinitionType))
                {
                    CollectionItemType = _genericCollectionDefinitionType.GetGenericArguments()[0];
                }
                else
                {
                    CollectionItemType = ReflectionUtils.GetCollectionItemType(underlyingType);
                }

                if (underlyingType == typeof(IList))
                {
                    CreatedType = typeof(List <object>);
                }

                if (CollectionItemType != null)
                {
                    _parameterizedConstructor = CollectionUtils.ResolveEnumerableCollectionConstructor(underlyingType, CollectionItemType);
                }

                IsReadOnlyOrFixedSize = ReflectionUtils.InheritsGenericDefinition(underlyingType, typeof(ReadOnlyCollection <>));
                canDeserialize        = true;
            }
            else if (ReflectionUtils.ImplementsGenericDefinition(underlyingType, typeof(ICollection <>), out _genericCollectionDefinitionType))
            {
                CollectionItemType = _genericCollectionDefinitionType.GetGenericArguments()[0];

                if (ReflectionUtils.IsGenericDefinition(underlyingType, typeof(ICollection <>)) ||
                    ReflectionUtils.IsGenericDefinition(underlyingType, typeof(IList <>)))
                {
                    CreatedType = typeof(List <>).MakeGenericType(CollectionItemType);
                }

                _parameterizedConstructor = CollectionUtils.ResolveEnumerableCollectionConstructor(underlyingType, CollectionItemType);
                canDeserialize            = true;
                ShouldCreateWrapper       = true;
            }
            else if (ReflectionUtils.ImplementsGenericDefinition(underlyingType, typeof(IEnumerable <>), out tempCollectionType))
            {
                CollectionItemType = tempCollectionType.GetGenericArguments()[0];

                if (ReflectionUtils.IsGenericDefinition(UnderlyingType, typeof(IEnumerable <>)))
                {
                    CreatedType = typeof(List <>).MakeGenericType(CollectionItemType);
                }

                _parameterizedConstructor = CollectionUtils.ResolveEnumerableCollectionConstructor(underlyingType, CollectionItemType);


                if (underlyingType.IsGenericType() && underlyingType.GetGenericTypeDefinition() == typeof(IEnumerable <>))
                {
                    _genericCollectionDefinitionType = tempCollectionType;

                    IsReadOnlyOrFixedSize = false;
                    ShouldCreateWrapper   = false;
                    canDeserialize        = true;
                }
                else
                {
                    _genericCollectionDefinitionType = typeof(List <>).MakeGenericType(CollectionItemType);

                    IsReadOnlyOrFixedSize = true;
                    ShouldCreateWrapper   = true;
                    canDeserialize        = HasParameterizedCreatorInternal;
                }
            }
            else
            {
                // types that implement IEnumerable and nothing else
                canDeserialize      = false;
                ShouldCreateWrapper = true;
            }

            CanDeserialize = canDeserialize;

            if (CollectionItemType != null && ReflectionUtils.IsNullableType(CollectionItemType))
            {
                // bug in .NET 2.0 & 3.5 that List<Nullable<T>> throws an error when adding null via IList.Add(object)
                // wrapper will handle calling Add(T) instead
                if (ReflectionUtils.InheritsGenericDefinition(CreatedType, typeof(List <>), out tempCollectionType) ||
                    (IsArray && !IsMultidimensionalArray))
                {
                    ShouldCreateWrapper = true;
                }
            }
        }