private void SerializeList(JsonWriter writer, IWrappedCollection values, JsonArrayContract contract, JsonProperty member, JsonContract collectionValueContract)
        {
            contract.InvokeOnSerializing(values.UnderlyingCollection, base.Serializer.Context);
            this.SerializeStack.Add(values.UnderlyingCollection);
            bool?isReference = contract.IsReference;
            bool flag        = (!isReference.HasValue ? this.HasFlag(base.Serializer.PreserveReferencesHandling, PreserveReferencesHandling.Arrays) : isReference.Value);
            bool flag1       = this.ShouldWriteType(TypeNameHandling.Arrays, contract, member, collectionValueContract);

            if (flag || flag1)
            {
                writer.WriteStartObject();
                if (flag)
                {
                    writer.WritePropertyName("$id");
                    writer.WriteValue(base.Serializer.ReferenceResolver.GetReference(this, values.UnderlyingCollection));
                }
                if (flag1)
                {
                    this.WriteTypeProperty(writer, values.UnderlyingCollection.GetType());
                }
                writer.WritePropertyName("$values");
            }
            JsonContract jsonContract = base.Serializer.ContractResolver.ResolveContract(contract.CollectionItemType ?? typeof(object));

            writer.WriteStartArray();
            int         top        = writer.Top;
            int         num        = 0;
            IEnumerator enumerator = values.GetEnumerator();

            try
            {
                while (enumerator.MoveNext())
                {
                    object current = enumerator.Current;
                    try
                    {
                        try
                        {
                            JsonContract contractSafe = this.GetContractSafe(current);
                            if (this.ShouldWriteReference(current, null, contractSafe))
                            {
                                this.WriteReference(writer, current);
                            }
                            else if (this.CheckForCircularReference(current, null, contract))
                            {
                                this.SerializeValue(writer, current, contractSafe, null, jsonContract);
                            }
                        }
                        catch (Exception exception)
                        {
                            if (!base.IsErrorHandled(values.UnderlyingCollection, contract, num, exception))
                            {
                                throw;
                            }
                            else
                            {
                                this.HandleError(writer, top);
                            }
                        }
                    }
                    finally
                    {
                        num++;
                    }
                }
            }
            finally
            {
                IDisposable disposable  = enumerator as IDisposable;
                IDisposable disposable1 = disposable;
                if (disposable != null)
                {
                    disposable1.Dispose();
                }
            }
            writer.WriteEndArray();
            if (flag || flag1)
            {
                writer.WriteEndObject();
            }
            this.SerializeStack.RemoveAt(this.SerializeStack.Count - 1);
            contract.InvokeOnSerialized(values.UnderlyingCollection, base.Serializer.Context);
        }
        private object CreateObjectFromNonDefaultConstructor(JsonReader reader, JsonObjectContract contract, ConstructorInfo constructorInfo, string id)
        {
            ValidationUtils.ArgumentNotNull(constructorInfo, "constructorInfo");
            Type underlyingType = contract.UnderlyingType;
            IDictionary <JsonProperty, object>  dictionary  = ResolvePropertyAndConstructorValues(contract, reader, underlyingType);
            IDictionary <ParameterInfo, object> dictionary2 = ((IEnumerable <ParameterInfo>)constructorInfo.GetParameters()).ToDictionary((Func <ParameterInfo, ParameterInfo>)((ParameterInfo p) => p), (Func <ParameterInfo, object>)((ParameterInfo p) => null));
            IDictionary <JsonProperty, object>  dictionary3 = new Dictionary <JsonProperty, object>();

            foreach (KeyValuePair <JsonProperty, object> item in dictionary)
            {
                ParameterInfo key = dictionary2.ForgivingCaseSensitiveFind((KeyValuePair <ParameterInfo, object> kv) => kv.Key.Name, item.Key.UnderlyingName).Key;
                if (key != null)
                {
                    dictionary2[key] = item.Value;
                }
                else
                {
                    dictionary3.Add(item);
                }
            }
            object obj = constructorInfo.Invoke(dictionary2.Values.ToArray());

            if (id != null)
            {
                base.Serializer.ReferenceResolver.AddReference(this, id, obj);
            }
            contract.InvokeOnDeserializing(obj, base.Serializer.Context);
            foreach (KeyValuePair <JsonProperty, object> item2 in dictionary3)
            {
                JsonProperty key2  = item2.Key;
                object       value = item2.Value;
                if (ShouldSetPropertyValue(item2.Key, item2.Value))
                {
                    key2.ValueProvider.SetValue(obj, value);
                }
                else if (!key2.Writable && value != null)
                {
                    JsonContract jsonContract = base.Serializer.ContractResolver.ResolveContract(key2.PropertyType);
                    if (jsonContract is JsonArrayContract)
                    {
                        JsonArrayContract jsonArrayContract = jsonContract as JsonArrayContract;
                        object            value2            = key2.ValueProvider.GetValue(obj);
                        if (value2 != null)
                        {
                            IWrappedCollection wrappedCollection  = jsonArrayContract.CreateWrapper(value2);
                            IWrappedCollection wrappedCollection2 = jsonArrayContract.CreateWrapper(value);
                            IEnumerator        enumerator3        = wrappedCollection2.GetEnumerator();
                            try
                            {
                                while (enumerator3.MoveNext())
                                {
                                    object current3 = enumerator3.Current;
                                    wrappedCollection.Add(current3);
                                }
                            }
                            finally
                            {
                                IDisposable disposable;
                                if ((disposable = (enumerator3 as IDisposable)) != null)
                                {
                                    disposable.Dispose();
                                }
                            }
                        }
                    }
                    else if (jsonContract is JsonDictionaryContract)
                    {
                        JsonDictionaryContract jsonDictionaryContract = jsonContract as JsonDictionaryContract;
                        object value3 = key2.ValueProvider.GetValue(obj);
                        if (value3 != null)
                        {
                            IWrappedDictionary    wrappedDictionary  = jsonDictionaryContract.CreateWrapper(value3);
                            IWrappedDictionary    wrappedDictionary2 = jsonDictionaryContract.CreateWrapper(value);
                            IDictionaryEnumerator enumerator4        = wrappedDictionary2.GetEnumerator();
                            try
                            {
                                while (enumerator4.MoveNext())
                                {
                                    DictionaryEntry dictionaryEntry = (DictionaryEntry)enumerator4.Current;
                                    wrappedDictionary.Add(dictionaryEntry.Key, dictionaryEntry.Value);
                                }
                            }
                            finally
                            {
                                IDisposable disposable2;
                                if ((disposable2 = (enumerator4 as IDisposable)) != null)
                                {
                                    disposable2.Dispose();
                                }
                            }
                        }
                    }
                }
            }
            contract.InvokeOnDeserialized(obj, base.Serializer.Context);
            return(obj);
        }