Beispiel #1
0
        public JsonDictionaryContract(Type underlyingType) : base(underlyingType)
        {
            Type type;
            Type type2;

            base.ContractType = JsonContractType.Dictionary;
            if (ReflectionUtils.ImplementsGenericDefinition(underlyingType, typeof(IDictionary <,>), out this._genericCollectionDefinitionType))
            {
                type  = this._genericCollectionDefinitionType.GetGenericArguments()[0];
                type2 = this._genericCollectionDefinitionType.GetGenericArguments()[1];
                if (ReflectionUtils.IsGenericDefinition(base.UnderlyingType, typeof(IDictionary <,>)))
                {
                    Type[] typeArguments = new Type[] { type, type2 };
                    base.CreatedType = typeof(Dictionary <,>).MakeGenericType(typeArguments);
                }
                base.IsReadOnlyOrFixedSize = ReflectionUtils.InheritsGenericDefinition(underlyingType, typeof(ReadOnlyDictionary <,>));
            }
            else if (ReflectionUtils.ImplementsGenericDefinition(underlyingType, typeof(IReadOnlyDictionary <,>), out this._genericCollectionDefinitionType))
            {
                type  = this._genericCollectionDefinitionType.GetGenericArguments()[0];
                type2 = this._genericCollectionDefinitionType.GetGenericArguments()[1];
                if (ReflectionUtils.IsGenericDefinition(base.UnderlyingType, typeof(IReadOnlyDictionary <,>)))
                {
                    Type[] typeArguments = new Type[] { type, type2 };
                    base.CreatedType = typeof(ReadOnlyDictionary <,>).MakeGenericType(typeArguments);
                }
                base.IsReadOnlyOrFixedSize = true;
            }
            else
            {
                ReflectionUtils.GetDictionaryKeyValueTypes(base.UnderlyingType, out type, out type2);
                if (base.UnderlyingType == typeof(IDictionary))
                {
                    base.CreatedType = typeof(Dictionary <object, object>);
                }
            }
            if ((type != null) && (type2 != null))
            {
                Type[] typeArguments = new Type[] { type, type2 };
                Type[] typeArray4    = new Type[] { type, type2 };
                this._parameterizedConstructor = CollectionUtils.ResolveEnumerableCollectionConstructor(base.CreatedType, typeof(KeyValuePair <,>).MakeGenericType(typeArguments), typeof(IDictionary <,>).MakeGenericType(typeArray4));
                if (!this.HasParameterizedCreatorInternal && (underlyingType.Name == "FSharpMap`2"))
                {
                    FSharpUtils.EnsureInitialized(underlyingType.Assembly());
                    this._parameterizedCreator = FSharpUtils.CreateMap(type, type2);
                }
            }
            this.ShouldCreateWrapper = !typeof(IDictionary).IsAssignableFrom(base.CreatedType);
            this.DictionaryKeyType   = type;
            this.DictionaryValueType = type2;
            if (ImmutableCollectionsUtils.TryBuildImmutableForDictionaryContract(underlyingType, this.DictionaryKeyType, this.DictionaryValueType, out Type type3, out ObjectConstructor <object> constructor))
            {
                base.CreatedType           = type3;
                this._parameterizedCreator = constructor;
                base.IsReadOnlyOrFixedSize = true;
            }
        }
Beispiel #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="JsonDictionaryContract"/> class.
        /// </summary>
        /// <param name="underlyingType">The underlying type for the contract.</param>
        public JsonDictionaryContract(Type underlyingType)
            : base(underlyingType)
        {
            ContractType = JsonContractType.Dictionary;

            Type keyType;
            Type valueType;

            if (ReflectionUtils.ImplementsGenericDefinition(underlyingType, typeof(IDictionary <,>), out _genericCollectionDefinitionType))
            {
                keyType   = _genericCollectionDefinitionType.GetGenericArguments()[0];
                valueType = _genericCollectionDefinitionType.GetGenericArguments()[1];

                if (ReflectionUtils.IsGenericDefinition(UnderlyingType, typeof(IDictionary <,>)))
                {
                    CreatedType = typeof(Dictionary <,>).MakeGenericType(keyType, valueType);
                }

#if !(NET40 || NET35 || NET20 || PORTABLE40)
                IsReadOnlyOrFixedSize = ReflectionUtils.InheritsGenericDefinition(underlyingType, typeof(ReadOnlyDictionary <,>));
#endif
            }
#if !(NET40 || NET35 || NET20 || PORTABLE40)
            else if (ReflectionUtils.ImplementsGenericDefinition(underlyingType, typeof(IReadOnlyDictionary <,>), out _genericCollectionDefinitionType))
            {
                keyType   = _genericCollectionDefinitionType.GetGenericArguments()[0];
                valueType = _genericCollectionDefinitionType.GetGenericArguments()[1];

                if (ReflectionUtils.IsGenericDefinition(UnderlyingType, typeof(IReadOnlyDictionary <,>)))
                {
                    CreatedType = typeof(ReadOnlyDictionary <,>).MakeGenericType(keyType, valueType);
                }

                IsReadOnlyOrFixedSize = true;
            }
#endif
            else
            {
                ReflectionUtils.GetDictionaryKeyValueTypes(UnderlyingType, out keyType, out valueType);

                if (UnderlyingType == typeof(IDictionary))
                {
                    CreatedType = typeof(Dictionary <object, object>);
                }
            }

            if (keyType != null && valueType != null)
            {
                _parametrizedConstructor = CollectionUtils.ResolveEnumerableCollectionConstructor(CreatedType, typeof(KeyValuePair <,>).MakeGenericType(keyType, valueType));

#if !(NET35 || NET20)
                if (!HasParametrizedCreator && underlyingType.Name == FSharpUtils.FSharpMapTypeName)
                {
                    FSharpUtils.EnsureInitialized(underlyingType.Assembly());
                    _parametrizedCreator = FSharpUtils.CreateMap(keyType, valueType);
                }
#endif
            }

            ShouldCreateWrapper = !typeof(IDictionary).IsAssignableFrom(CreatedType);

            DictionaryKeyType   = keyType;
            DictionaryValueType = valueType;

#if (NET20 || NET35)
            if (DictionaryValueType != null && ReflectionUtils.IsNullableType(DictionaryValueType))
            {
                Type tempDictioanryType;

                // bug in .NET 2.0 & 3.5 that Dictionary<TKey, Nullable<TValue>> throws an error when adding null via IDictionary[key] = object
                // wrapper will handle calling Add(T) instead
                if (ReflectionUtils.InheritsGenericDefinition(CreatedType, typeof(Dictionary <,>), out tempDictioanryType))
                {
                    ShouldCreateWrapper = true;
                }
            }
#endif

#if !(NET20 || NET35 || NET40)
            Type immutableCreatedType;
            ObjectConstructor <object> immutableParameterizedCreator;
            if (ImmutableCollectionsUtils.TryBuildImmutableForDictionaryContract(underlyingType, DictionaryKeyType, DictionaryValueType, out immutableCreatedType, out immutableParameterizedCreator))
            {
                CreatedType           = immutableCreatedType;
                _parametrizedCreator  = immutableParameterizedCreator;
                IsReadOnlyOrFixedSize = true;
            }
#endif
        }
        public JsonDictionaryContract(Type underlyingType)
        {
            Class6.yDnXvgqzyB5jw();
            base(underlyingType);
            Type genericArguments;
            Type type;
            Type type1;
            ObjectConstructor <object> objectConstructor;

            this.ContractType = JsonContractType.Dictionary;
            if (ReflectionUtils.ImplementsGenericDefinition(underlyingType, typeof(IDictionary <,>), out this._genericCollectionDefinitionType))
            {
                genericArguments = this._genericCollectionDefinitionType.GetGenericArguments()[0];
                type             = this._genericCollectionDefinitionType.GetGenericArguments()[1];
                if (ReflectionUtils.IsGenericDefinition(base.UnderlyingType, typeof(IDictionary <,>)))
                {
                    base.CreatedType = typeof(Dictionary <,>).MakeGenericType(new Type[] { genericArguments, type });
                }
                else if (underlyingType.IsGenericType() && underlyingType.GetGenericTypeDefinition().FullName == "System.Collections.Concurrent.ConcurrentDictionary`2")
                {
                    this.ShouldCreateWrapper = true;
                }
                this.IsReadOnlyOrFixedSize = ReflectionUtils.InheritsGenericDefinition(underlyingType, typeof(ReadOnlyDictionary <,>));
            }
            else if (!ReflectionUtils.ImplementsGenericDefinition(underlyingType, typeof(IReadOnlyDictionary <,>), out this._genericCollectionDefinitionType))
            {
                ReflectionUtils.GetDictionaryKeyValueTypes(base.UnderlyingType, out genericArguments, out type);
                if (base.UnderlyingType == typeof(IDictionary))
                {
                    base.CreatedType = typeof(Dictionary <object, object>);
                }
            }
            else
            {
                genericArguments = this._genericCollectionDefinitionType.GetGenericArguments()[0];
                type             = this._genericCollectionDefinitionType.GetGenericArguments()[1];
                if (ReflectionUtils.IsGenericDefinition(base.UnderlyingType, typeof(IReadOnlyDictionary <,>)))
                {
                    base.CreatedType = typeof(ReadOnlyDictionary <,>).MakeGenericType(new Type[] { genericArguments, type });
                }
                this.IsReadOnlyOrFixedSize = true;
            }
            if (genericArguments != null && type != null)
            {
                this._parameterizedConstructor = CollectionUtils.ResolveEnumerableCollectionConstructor(base.CreatedType, typeof(KeyValuePair <,>).MakeGenericType(new Type[] { genericArguments, type }), typeof(IDictionary <,>).MakeGenericType(new Type[] { genericArguments, type }));
                if (!this.HasParameterizedCreatorInternal && underlyingType.Name == "FSharpMap`2")
                {
                    FSharpUtils.EnsureInitialized(underlyingType.Assembly());
                    this._parameterizedCreator = FSharpUtils.CreateMap(genericArguments, type);
                }
            }
            if (!typeof(IDictionary).IsAssignableFrom(base.CreatedType))
            {
                this.ShouldCreateWrapper = true;
            }
            this.DictionaryKeyType   = genericArguments;
            this.DictionaryValueType = type;
            if (ImmutableCollectionsUtils.TryBuildImmutableForDictionaryContract(underlyingType, this.DictionaryKeyType, this.DictionaryValueType, out type1, out objectConstructor))
            {
                base.CreatedType           = type1;
                this._parameterizedCreator = objectConstructor;
                this.IsReadOnlyOrFixedSize = true;
            }
        }
Beispiel #4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="JsonDictionaryContract"/> class.
        /// </summary>
        /// <param name="underlyingType">The underlying type for the contract.</param>
        public JsonDictionaryContract(Type underlyingType)
            : base(underlyingType)
        {
            ContractType = JsonContractType.Dictionary;

            Type keyType;
            Type valueType;

            if (ReflectionUtils.ImplementsGenericDefinition(underlyingType, typeof(IDictionary <,>), out _genericCollectionDefinitionType))
            {
                keyType   = _genericCollectionDefinitionType.GetGenericArguments()[0];
                valueType = _genericCollectionDefinitionType.GetGenericArguments()[1];

                if (ReflectionUtils.IsGenericDefinition(UnderlyingType, typeof(IDictionary <,>)))
                {
                    CreatedType = typeof(Dictionary <,>).MakeGenericType(keyType, valueType);
                }

//#if !(NET40 || NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE || PORTABLE40)
                IsReadOnlyOrFixedSize = ReflectionUtils.InheritsGenericDefinition(underlyingType, typeof(ReadOnlyDictionary <,>));
//#endif
            }
//#if !(NET40 || NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE || PORTABLE40)
            else if (ReflectionUtils.ImplementsGenericDefinition(underlyingType, typeof(IReadOnlyDictionary <,>), out _genericCollectionDefinitionType))
            {
                keyType   = _genericCollectionDefinitionType.GetGenericArguments()[0];
                valueType = _genericCollectionDefinitionType.GetGenericArguments()[1];

                if (ReflectionUtils.IsGenericDefinition(UnderlyingType, typeof(IReadOnlyDictionary <,>)))
                {
                    CreatedType = typeof(ReadOnlyDictionary <,>).MakeGenericType(keyType, valueType);
                }

                IsReadOnlyOrFixedSize = true;
            }
//#endif
            else
            {
                ReflectionUtils.GetDictionaryKeyValueTypes(UnderlyingType, out keyType, out valueType);

                if (UnderlyingType == typeof(IDictionary))
                {
                    CreatedType = typeof(Dictionary <object, object>);
                }
            }

            if (keyType != null && valueType != null)
            {
                ParametrizedConstructor = CollectionUtils.ResolveEnumableCollectionConstructor(CreatedType, typeof(KeyValuePair <,>).MakeGenericType(keyType, valueType));
            }

            ShouldCreateWrapper = !typeof(IDictionary).IsAssignableFrom(CreatedType);

            DictionaryKeyType   = keyType;
            DictionaryValueType = valueType;

            if (DictionaryValueType != null)
            {
                _isDictionaryValueTypeNullableType = ReflectionUtils.IsNullableType(DictionaryValueType);
            }

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

            if (ImmutableCollectionsUtils.TryBuildImmutableForDictionaryContract(underlyingType, DictionaryKeyType, DictionaryValueType, out immutableCreatedType, out immutableParameterizedCreator))
            {
                CreatedType             = immutableCreatedType;
                ParametrizedConstructor = immutableParameterizedCreator;
                IsReadOnlyOrFixedSize   = true;
            }
//#endif
        }
Beispiel #5
0
        public JsonDictionaryContract(Type underlyingType)
            : base(underlyingType)
        {
            ContractType = JsonContractType.Dictionary;

            Type?keyType;
            Type?valueType;

            if (ReflectionUtils.ImplementsGenericDefinition(NonNullableUnderlyingType, typeof(IDictionary <,>), out _genericCollectionDefinitionType))
            {
                keyType   = _genericCollectionDefinitionType.GetGenericArguments()[0];
                valueType = _genericCollectionDefinitionType.GetGenericArguments()[1];

                if (ReflectionUtils.IsGenericDefinition(NonNullableUnderlyingType, typeof(IDictionary <,>)))
                {
                    CreatedType = typeof(Dictionary <,>).MakeGenericType(keyType, valueType);
                }
                else if (NonNullableUnderlyingType.IsGenericType())
                {
                    // ConcurrentDictionary<,> + IDictionary setter + null value = error
                    // wrap to use generic setter
                    // https://github.com/JamesNK/Newtonsoft.Json/issues/1582
                    var typeDefinition = NonNullableUnderlyingType.GetGenericTypeDefinition();
                    if (typeDefinition.FullName == JsonTypeReflector.ConcurrentDictionaryTypeName)
                    {
                        ShouldCreateWrapper = true;
                    }
                }

#if HAVE_READ_ONLY_COLLECTIONS
                IsReadOnlyOrFixedSize = ReflectionUtils.InheritsGenericDefinition(NonNullableUnderlyingType, typeof(ReadOnlyDictionary <,>));
#endif
            }
#if HAVE_READ_ONLY_COLLECTIONS
            else if (ReflectionUtils.ImplementsGenericDefinition(NonNullableUnderlyingType, typeof(IReadOnlyDictionary <,>), out _genericCollectionDefinitionType))
            {
                keyType   = _genericCollectionDefinitionType.GetGenericArguments()[0];
                valueType = _genericCollectionDefinitionType.GetGenericArguments()[1];

                if (ReflectionUtils.IsGenericDefinition(NonNullableUnderlyingType, typeof(IReadOnlyDictionary <,>)))
                {
                    CreatedType = typeof(ReadOnlyDictionary <,>).MakeGenericType(keyType, valueType);
                }

                IsReadOnlyOrFixedSize = true;
            }
#endif
            else
            {
                ReflectionUtils.GetDictionaryKeyValueTypes(NonNullableUnderlyingType, out keyType, out valueType);

                if (NonNullableUnderlyingType == typeof(IDictionary))
                {
                    CreatedType = typeof(Dictionary <object, object>);
                }
            }

            if (keyType != null && valueType != null)
            {
                _parameterizedConstructor = CollectionUtils.ResolveEnumerableCollectionConstructor(
                    CreatedType,
                    typeof(KeyValuePair <,>).MakeGenericType(keyType, valueType),
                    typeof(IDictionary <,>).MakeGenericType(keyType, valueType));

#if HAVE_FSHARP_TYPES
                if (!HasParameterizedCreatorInternal && NonNullableUnderlyingType.Name == FSharpUtils.FSharpMapTypeName)
                {
                    FSharpUtils.EnsureInitialized(NonNullableUnderlyingType.Assembly());
                    _parameterizedCreator = FSharpUtils.Instance.CreateMap(keyType, valueType);
                }
#endif
            }

            if (!typeof(IDictionary).IsAssignableFrom(CreatedType))
            {
                ShouldCreateWrapper = true;
            }

            DictionaryKeyType   = keyType;
            DictionaryValueType = valueType;

#if (NET20 || NET35)
            if (DictionaryValueType != null && ReflectionUtils.IsNullableType(DictionaryValueType))
            {
                // bug in .NET 2.0 & 3.5 that Dictionary<TKey, Nullable<TValue>> throws an error when adding null via IDictionary[key] = object
                // wrapper will handle calling Add(T) instead
                if (ReflectionUtils.InheritsGenericDefinition(CreatedType, typeof(Dictionary <,>), out _))
                {
                    ShouldCreateWrapper = true;
                }
            }
#endif

            if (DictionaryKeyType != null &&
                DictionaryValueType != null &&
                ImmutableCollectionsUtils.TryBuildImmutableForDictionaryContract(
                    NonNullableUnderlyingType,
                    DictionaryKeyType,
                    DictionaryValueType,
                    out var immutableCreatedType,
                    out var immutableParameterizedCreator))
            {
                CreatedType           = immutableCreatedType;
                _parameterizedCreator = immutableParameterizedCreator;
                IsReadOnlyOrFixedSize = true;
            }
        }