From 8471c4ffe1cb800a4afd747b71dbb5ba31d5ca90 Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Sun, 18 Mar 2012 16:24:30 +1300 Subject: [PATCH] -Added IgnoreSerializableInterface to DefaultContractResolver -Changed serializing ISerializable types under partial trust to error to fix security issue -Fixed LinqBridge collision error in .NET 2.0 by moving types to a different namespace -Fixed JObject.Keys error when object has no items --- .../Bson/BsonReaderTests.cs | 2 - .../Bson/BsonWriterTests.cs | 6 +- .../Converters/BinaryConverterTests.cs | 2 - .../CustomCreationConverterTests.cs | 2 - .../Converters/DataSetConverterTests.cs | 3 - .../Converters/DataTableConverterTests.cs | 5 - .../Converters/IsoDateTimeConverterTests.cs | 4 - .../JavaScriptDateTimeConverterTests.cs | 5 +- .../Converters/ObjectIdConverterTests.cs | 7 +- .../Converters/RegexConverterTests.cs | 2 - .../Converters/StringEnumConverterTests.cs | 3 - .../Converters/VersionConverterTests.cs | 4 - Src/Newtonsoft.Json.Tests/ExceptionTests.cs | 3 - .../JsonArrayAttributeTests.cs | 4 - .../JsonValidatingReaderTests.cs | 4 +- .../Linq/ComponentModel/BindingTests.cs | 4 - .../JPropertyDescriptorTests.cs | 4 - Src/Newtonsoft.Json.Tests/Linq/JArrayTests.cs | 7 +- .../Linq/JConstructorTests.cs | 7 +- .../Linq/JObjectTests.cs | 23 +- Src/Newtonsoft.Json.Tests/Linq/JPathTests.cs | 12 +- .../Linq/JPropertyTests.cs | 2 - Src/Newtonsoft.Json.Tests/Linq/JRawTests.cs | 6 +- .../Linq/JTokenEqualityComparerTests.cs | 3 - Src/Newtonsoft.Json.Tests/Linq/JTokenTests.cs | 6 +- .../Linq/JTokenWriterTest.cs | 4 + Src/Newtonsoft.Json.Tests/Linq/JValueTests.cs | 6 +- .../Linq/LinqToJsonTest.cs | 12 +- .../Properties/AssemblyInfo.cs | 2 +- .../Schema/ExtensionsTests.cs | 2 - .../Schema/JsonSchemaBuilderTests.cs | 3 - .../Schema/JsonSchemaGeneratorTests.cs | 6 +- .../Schema/JsonSchemaModelBuilderTests.cs | 5 - .../Schema/JsonSchemaNodeTests.cs | 4 - .../Schema/JsonSchemaTests.cs | 3 - ...lCasePropertyNamesContractResolverTests.cs | 2 - .../JsonPropertyCollectionTests.cs | 4 - .../Serialization/JsonSerializerTest.cs | 87 +- .../Serialization/PopulateTests.cs | 3 - .../PreserveReferencesHandlingTests.cs | 1 - .../SerializationErrorHandlingTests.cs | 3 - .../SerializationEventAttributeTests.cs | 5 - .../Serialization/TypeNameHandlingTests.cs | 2 - Src/Newtonsoft.Json.Tests/TestFixtureBase.cs | 7 +- Src/Newtonsoft.Json.Tests/TestObjects/Car.cs | 2 - .../TestObjects/CircularReferenceClass.cs | 5 - .../CircularReferenceWithIdClass.cs | 5 - .../TestObjects/ContentBaseClass.cs | 7 +- .../TestObjects/ContentSubClass.cs | 7 +- .../TestObjects/EmployeeReference.cs | 5 - ...ricListAndDictionaryInterfaceProperties.cs | 5 +- .../TestObjects/HolderClass.cs | 5 +- .../TestObjects/JaggedArray.cs | 7 +- .../TestObjects/Movie.cs | 2 - .../TestObjects/PersonError.cs | 2 - .../TestObjects/PersonPropertyClass.cs | 7 +- .../TestObjects/PrivateMembersClass.cs | 5 - ...dConstructorRequiringConverterTestClass.cs | 3 - .../PublicParametizedConstructorTestClass.cs | 7 +- ...ructorWithNonPropertyParameterTestClass.cs | 7 +- ...izedConstructorWithPropertyNameConflict.cs | 3 - .../TestObjects/RequiredMembersClass.cs | 3 - .../SerializationEventTestObject.cs | 2 - .../TestObjects/Shortie.cs | 8 +- .../TestObjects/WagePerson.cs | 7 +- .../DynamicReflectionDelegateFactoryTests.cs | 6 +- .../Utilities/ReflectionUtilsTests.cs | 2 - Src/Newtonsoft.Json/Bson/BsonBinaryWriter.cs | 2 - Src/Newtonsoft.Json/Bson/BsonObjectId.cs | 3 - Src/Newtonsoft.Json/ConstructorHandling.cs | 5 - .../Converters/BsonObjectIdConverter.cs | 3 - .../Converters/DateTimeConverterBase.cs | 3 - .../Converters/KeyValuePairConverter.cs | 2 - .../Converters/RegexConverter.cs | 3 - .../Converters/StringEnumConverter.cs | 6 +- .../Converters/VersionConverter.cs | 3 - .../Converters/XmlNodeConverter.cs | 4 + Src/Newtonsoft.Json/DefaultValueHandling.cs | 3 - Src/Newtonsoft.Json/IJsonLineInfo.cs | 7 +- Src/Newtonsoft.Json/JsonArrayAttribute.cs | 3 - .../JsonConstructorAttribute.cs | 3 - Src/Newtonsoft.Json/JsonContainerAttribute.cs | 3 - Src/Newtonsoft.Json/JsonConverterAttribute.cs | 3 - Src/Newtonsoft.Json/JsonReader.cs | 7 +- Src/Newtonsoft.Json/JsonSerializerSettings.cs | 3 - Src/Newtonsoft.Json/JsonValidatingReader.cs | 7 +- Src/Newtonsoft.Json/JsonWriter.cs | 9 +- Src/Newtonsoft.Json/Linq/Extensions.cs | 8 +- Src/Newtonsoft.Json/Linq/IJEnumerable.cs | 5 +- Src/Newtonsoft.Json/Linq/JArray.cs | 3 - Src/Newtonsoft.Json/Linq/JConstructor.cs | 2 - Src/Newtonsoft.Json/Linq/JContainer.cs | 9 +- Src/Newtonsoft.Json/Linq/JEnumerable.cs | 8 +- Src/Newtonsoft.Json/Linq/JObject.cs | 61 +- Src/Newtonsoft.Json/Linq/JProperty.cs | 4 - .../Linq/JPropertyKeyedCollection.cs | 168 + Src/Newtonsoft.Json/Linq/JRaw.cs | 6 +- Src/Newtonsoft.Json/Linq/JToken.cs | 7 +- .../Linq/JTokenEqualityComparer.cs | 5 +- Src/Newtonsoft.Json/Linq/JTokenReader.cs | 4 - Src/Newtonsoft.Json/Linq/JTokenWriter.cs | 3 - Src/Newtonsoft.Json/Linq/JValue.cs | 3 - Src/Newtonsoft.Json/MemberSerialization.cs | 5 - .../Newtonsoft.Json.Net20.csproj | 1 + .../Newtonsoft.Json.Net35.csproj | 1 + .../Newtonsoft.Json.Silverlight.csproj | 1 + .../Newtonsoft.Json.WindowsPhone.csproj | 1 + Src/Newtonsoft.Json/Newtonsoft.Json.csproj | 1 + Src/Newtonsoft.Json/NullValueHandling.cs | 5 - Src/Newtonsoft.Json/ObjectCreationHandling.cs | 5 - .../Properties/AssemblyInfo.cs | 2 +- Src/Newtonsoft.Json/Schema/Extensions.cs | 3 - Src/Newtonsoft.Json/Schema/JsonSchema.cs | 3 - .../Schema/JsonSchemaBuilder.cs | 7 +- .../Schema/JsonSchemaConstants.cs | 5 +- .../Schema/JsonSchemaGenerator.cs | 7 +- Src/Newtonsoft.Json/Schema/JsonSchemaModel.cs | 2 - .../Schema/JsonSchemaModelBuilder.cs | 6 +- Src/Newtonsoft.Json/Schema/JsonSchemaNode.cs | 4 + .../Schema/JsonSchemaResolver.cs | 9 +- Src/Newtonsoft.Json/Schema/JsonSchemaType.cs | 3 - .../Schema/JsonSchemaWriter.cs | 8 +- .../Serialization/CachedAttributeGetter.cs | 3 - .../Serialization/DefaultContractResolver.cs | 18 +- .../Serialization/DefaultReferenceResolver.cs | 4 - .../Serialization/DynamicValueProvider.cs | 4 +- .../Serialization/ErrorContext.cs | 3 - .../Serialization/ErrorEventArgs.cs | 3 - .../Serialization/IContractResolver.cs | 3 - .../Serialization/JsonArrayContract.cs | 6 +- .../Serialization/JsonDictionaryContract.cs | 3 + .../Serialization/JsonFormatterConverter.cs | 3 - .../JsonISerializableContract.cs | 4 - .../Serialization/JsonLinqContract.cs | 3 - .../Serialization/JsonProperty.cs | 3 + .../JsonSerializerInternalReader.cs | 12 +- .../JsonSerializerInternalWriter.cs | 16 +- .../Serialization/JsonStringContract.cs | 3 - .../Serialization/JsonTypeReflector.cs | 40 +- .../Serialization/ReflectionValueProvider.cs | 3 - Src/Newtonsoft.Json/TypeNameHandling.cs | 3 - .../Utilities/BidirectionalDictionary.cs | 2 - .../Utilities/CollectionUtils.cs | 5 + .../Utilities/CollectionWrapper.cs | 7 +- Src/Newtonsoft.Json/Utilities/ConvertUtils.cs | 7 +- .../Utilities/DateTimeUtils.cs | 3 - .../Utilities/DictionaryWrapper.cs | 7 +- .../DynamicReflectionDelegateFactory.cs | 6 +- .../Utilities/DynamicWrapper.cs | 8 +- Src/Newtonsoft.Json/Utilities/EnumUtils.cs | 4 + Src/Newtonsoft.Json/Utilities/EnumValue.cs | 5 - Src/Newtonsoft.Json/Utilities/EnumValues.cs | 4 - .../Utilities/ILGeneratorExtensions.cs | 3 - .../LateBoundReflectionDelegateFactory.cs | 5 +- Src/Newtonsoft.Json/Utilities/LinqBridge.cs | 6010 ++++++++--------- Src/Newtonsoft.Json/Utilities/ListWrapper.cs | 5 - .../Utilities/ReflectionDelegateFactory.cs | 4 + .../Utilities/ReflectionUtils.cs | 8 +- .../Utilities/StringReference.cs | 7 +- Src/Newtonsoft.Json/Utilities/StringUtils.cs | 8 +- .../Utilities/ThreadSafeStore.cs | 4 + .../Utilities/TypeExtensions.cs | 7 +- 162 files changed, 3550 insertions(+), 3566 deletions(-) create mode 100644 Src/Newtonsoft.Json/Linq/JPropertyKeyedCollection.cs diff --git a/Src/Newtonsoft.Json.Tests/Bson/BsonReaderTests.cs b/Src/Newtonsoft.Json.Tests/Bson/BsonReaderTests.cs index d99cecaf..ad21afc1 100644 --- a/Src/Newtonsoft.Json.Tests/Bson/BsonReaderTests.cs +++ b/Src/Newtonsoft.Json.Tests/Bson/BsonReaderTests.cs @@ -24,9 +24,7 @@ #endregion using System; -using System.Collections.Generic; using System.Globalization; -using System.Linq; using System.Text; #if !NETFX_CORE using NUnit.Framework; diff --git a/Src/Newtonsoft.Json.Tests/Bson/BsonWriterTests.cs b/Src/Newtonsoft.Json.Tests/Bson/BsonWriterTests.cs index d5c1d99a..3018a121 100644 --- a/Src/Newtonsoft.Json.Tests/Bson/BsonWriterTests.cs +++ b/Src/Newtonsoft.Json.Tests/Bson/BsonWriterTests.cs @@ -25,7 +25,6 @@ using System; using System.Collections.Generic; -using System.Linq; using System.Text; #if !NETFX_CORE using NUnit.Framework; @@ -39,6 +38,11 @@ using System.IO; using Newtonsoft.Json.Utilities; using Newtonsoft.Json.Tests.TestObjects; using System.Globalization; +#if NET20 +using Newtonsoft.Json.Utilities.LinqBridge; +#else +using System.Linq; +#endif namespace Newtonsoft.Json.Tests.Bson { diff --git a/Src/Newtonsoft.Json.Tests/Converters/BinaryConverterTests.cs b/Src/Newtonsoft.Json.Tests/Converters/BinaryConverterTests.cs index 86d4dce5..ae1d9ed2 100644 --- a/Src/Newtonsoft.Json.Tests/Converters/BinaryConverterTests.cs +++ b/Src/Newtonsoft.Json.Tests/Converters/BinaryConverterTests.cs @@ -6,7 +6,6 @@ using System.Data.Linq; #if !(SILVERLIGHT || NETFX_CORE) using System.Data.SqlTypes; #endif -using System.Linq; using System.Text; using Newtonsoft.Json.Converters; #if !NETFX_CORE @@ -16,7 +15,6 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; using TestFixture = Microsoft.VisualStudio.TestTools.UnitTesting.TestClassAttribute; using Test = Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute; #endif -using Newtonsoft.Json.Tests.TestObjects; namespace Newtonsoft.Json.Tests.Converters { diff --git a/Src/Newtonsoft.Json.Tests/Converters/CustomCreationConverterTests.cs b/Src/Newtonsoft.Json.Tests/Converters/CustomCreationConverterTests.cs index eae51bbb..a38e516a 100644 --- a/Src/Newtonsoft.Json.Tests/Converters/CustomCreationConverterTests.cs +++ b/Src/Newtonsoft.Json.Tests/Converters/CustomCreationConverterTests.cs @@ -1,7 +1,5 @@ using System; using System.Collections.Generic; -using System.Linq; -using System.Text; using Newtonsoft.Json.Converters; #if !NETFX_CORE using NUnit.Framework; diff --git a/Src/Newtonsoft.Json.Tests/Converters/DataSetConverterTests.cs b/Src/Newtonsoft.Json.Tests/Converters/DataSetConverterTests.cs index ba1dad39..7e759aea 100644 --- a/Src/Newtonsoft.Json.Tests/Converters/DataSetConverterTests.cs +++ b/Src/Newtonsoft.Json.Tests/Converters/DataSetConverterTests.cs @@ -1,8 +1,5 @@ #if !(SILVERLIGHT || NETFX_CORE) using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; using Newtonsoft.Json.Converters; #if !NETFX_CORE using NUnit.Framework; diff --git a/Src/Newtonsoft.Json.Tests/Converters/DataTableConverterTests.cs b/Src/Newtonsoft.Json.Tests/Converters/DataTableConverterTests.cs index 3b392806..d59db293 100644 --- a/Src/Newtonsoft.Json.Tests/Converters/DataTableConverterTests.cs +++ b/Src/Newtonsoft.Json.Tests/Converters/DataTableConverterTests.cs @@ -1,9 +1,5 @@ #if !(SILVERLIGHT || NETFX_CORE) using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using Newtonsoft.Json.Converters; #if !NETFX_CORE using NUnit.Framework; #else @@ -11,7 +7,6 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; using TestFixture = Microsoft.VisualStudio.TestTools.UnitTesting.TestClassAttribute; using Test = Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute; #endif -using Newtonsoft.Json.Tests.TestObjects; #if !NETFX_CORE using System.Data; #endif diff --git a/Src/Newtonsoft.Json.Tests/Converters/IsoDateTimeConverterTests.cs b/Src/Newtonsoft.Json.Tests/Converters/IsoDateTimeConverterTests.cs index 634ca036..bbd6b32e 100644 --- a/Src/Newtonsoft.Json.Tests/Converters/IsoDateTimeConverterTests.cs +++ b/Src/Newtonsoft.Json.Tests/Converters/IsoDateTimeConverterTests.cs @@ -24,9 +24,6 @@ #endregion using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; using Newtonsoft.Json.Tests.TestObjects; #if !NETFX_CORE using NUnit.Framework; @@ -38,7 +35,6 @@ using Test = Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute; using Newtonsoft.Json.Converters; using Newtonsoft.Json.Utilities; using System.Globalization; -using System.Xml; namespace Newtonsoft.Json.Tests.Converters { diff --git a/Src/Newtonsoft.Json.Tests/Converters/JavaScriptDateTimeConverterTests.cs b/Src/Newtonsoft.Json.Tests/Converters/JavaScriptDateTimeConverterTests.cs index f0111266..850d3426 100644 --- a/Src/Newtonsoft.Json.Tests/Converters/JavaScriptDateTimeConverterTests.cs +++ b/Src/Newtonsoft.Json.Tests/Converters/JavaScriptDateTimeConverterTests.cs @@ -24,10 +24,6 @@ #endregion using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using Newtonsoft.Json.Tests.TestObjects; #if !NETFX_CORE using NUnit.Framework; #else @@ -36,6 +32,7 @@ using TestFixture = Microsoft.VisualStudio.TestTools.UnitTesting.TestClassAttrib using Test = Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute; #endif using Newtonsoft.Json.Converters; +using Newtonsoft.Json.Tests.TestObjects; namespace Newtonsoft.Json.Tests.Converters { diff --git a/Src/Newtonsoft.Json.Tests/Converters/ObjectIdConverterTests.cs b/Src/Newtonsoft.Json.Tests/Converters/ObjectIdConverterTests.cs index 40d99f84..292437b8 100644 --- a/Src/Newtonsoft.Json.Tests/Converters/ObjectIdConverterTests.cs +++ b/Src/Newtonsoft.Json.Tests/Converters/ObjectIdConverterTests.cs @@ -1,10 +1,5 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Text; +using System.IO; using Newtonsoft.Json.Bson; -using Newtonsoft.Json.Tests.TestObjects; using Newtonsoft.Json.Utilities; #if !NETFX_CORE using NUnit.Framework; diff --git a/Src/Newtonsoft.Json.Tests/Converters/RegexConverterTests.cs b/Src/Newtonsoft.Json.Tests/Converters/RegexConverterTests.cs index f5ed3f61..03a486d4 100644 --- a/Src/Newtonsoft.Json.Tests/Converters/RegexConverterTests.cs +++ b/Src/Newtonsoft.Json.Tests/Converters/RegexConverterTests.cs @@ -7,8 +7,6 @@ using System.Data.Linq; using System.Data.SqlTypes; #endif using System.IO; -using System.Linq; -using System.Text; using System.Text.RegularExpressions; using Newtonsoft.Json.Bson; using Newtonsoft.Json.Converters; diff --git a/Src/Newtonsoft.Json.Tests/Converters/StringEnumConverterTests.cs b/Src/Newtonsoft.Json.Tests/Converters/StringEnumConverterTests.cs index 4a7226c2..a7fc1265 100644 --- a/Src/Newtonsoft.Json.Tests/Converters/StringEnumConverterTests.cs +++ b/Src/Newtonsoft.Json.Tests/Converters/StringEnumConverterTests.cs @@ -1,8 +1,5 @@ using System; -using System.Collections.Generic; -using System.Linq; using System.Runtime.Serialization; -using System.Text; using Newtonsoft.Json.Converters; #if !NETFX_CORE using NUnit.Framework; diff --git a/Src/Newtonsoft.Json.Tests/Converters/VersionConverterTests.cs b/Src/Newtonsoft.Json.Tests/Converters/VersionConverterTests.cs index 48f08585..146045a1 100644 --- a/Src/Newtonsoft.Json.Tests/Converters/VersionConverterTests.cs +++ b/Src/Newtonsoft.Json.Tests/Converters/VersionConverterTests.cs @@ -24,9 +24,6 @@ #endregion using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; using Newtonsoft.Json.Converters; #if !NETFX_CORE using NUnit.Framework; @@ -35,7 +32,6 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; using TestFixture = Microsoft.VisualStudio.TestTools.UnitTesting.TestClassAttribute; using Test = Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute; #endif -using Newtonsoft.Json.Tests.TestObjects; namespace Newtonsoft.Json.Tests.Converters { diff --git a/Src/Newtonsoft.Json.Tests/ExceptionTests.cs b/Src/Newtonsoft.Json.Tests/ExceptionTests.cs index 2fb82123..261f1811 100644 --- a/Src/Newtonsoft.Json.Tests/ExceptionTests.cs +++ b/Src/Newtonsoft.Json.Tests/ExceptionTests.cs @@ -24,9 +24,6 @@ #endregion using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; using Newtonsoft.Json.Schema; #if !NETFX_CORE using NUnit.Framework; diff --git a/Src/Newtonsoft.Json.Tests/JsonArrayAttributeTests.cs b/Src/Newtonsoft.Json.Tests/JsonArrayAttributeTests.cs index fa45495f..bf2af7dd 100644 --- a/Src/Newtonsoft.Json.Tests/JsonArrayAttributeTests.cs +++ b/Src/Newtonsoft.Json.Tests/JsonArrayAttributeTests.cs @@ -23,10 +23,6 @@ // OTHER DEALINGS IN THE SOFTWARE. #endregion -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; #if !NETFX_CORE using NUnit.Framework; #else diff --git a/Src/Newtonsoft.Json.Tests/JsonValidatingReaderTests.cs b/Src/Newtonsoft.Json.Tests/JsonValidatingReaderTests.cs index fabfa6b6..4ec070f6 100644 --- a/Src/Newtonsoft.Json.Tests/JsonValidatingReaderTests.cs +++ b/Src/Newtonsoft.Json.Tests/JsonValidatingReaderTests.cs @@ -1,7 +1,9 @@ using System; using System.Collections.Generic; using System.IO; -using System.Linq; +#if NET20 +using Newtonsoft.Json.Utilities.LinqBridge; +#endif using System.Text; #if !NETFX_CORE using NUnit.Framework; diff --git a/Src/Newtonsoft.Json.Tests/Linq/ComponentModel/BindingTests.cs b/Src/Newtonsoft.Json.Tests/Linq/ComponentModel/BindingTests.cs index dc54d4dc..2ac3342d 100644 --- a/Src/Newtonsoft.Json.Tests/Linq/ComponentModel/BindingTests.cs +++ b/Src/Newtonsoft.Json.Tests/Linq/ComponentModel/BindingTests.cs @@ -24,10 +24,6 @@ #endregion #if !PocketPC && !SILVERLIGHT && !NETFX_CORE -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; using NUnit.Framework; using System.Web.UI; using Newtonsoft.Json.Linq; diff --git a/Src/Newtonsoft.Json.Tests/Linq/ComponentModel/JPropertyDescriptorTests.cs b/Src/Newtonsoft.Json.Tests/Linq/ComponentModel/JPropertyDescriptorTests.cs index df790477..1d677ecd 100644 --- a/Src/Newtonsoft.Json.Tests/Linq/ComponentModel/JPropertyDescriptorTests.cs +++ b/Src/Newtonsoft.Json.Tests/Linq/ComponentModel/JPropertyDescriptorTests.cs @@ -24,10 +24,6 @@ #endregion #if !SILVERLIGHT && !NETFX_CORE -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; using NUnit.Framework; using Newtonsoft.Json.Linq; diff --git a/Src/Newtonsoft.Json.Tests/Linq/JArrayTests.cs b/Src/Newtonsoft.Json.Tests/Linq/JArrayTests.cs index 6ebef284..bb8141d3 100644 --- a/Src/Newtonsoft.Json.Tests/Linq/JArrayTests.cs +++ b/Src/Newtonsoft.Json.Tests/Linq/JArrayTests.cs @@ -1,8 +1,6 @@ using System; using System.Collections.Generic; using System.ComponentModel; -using System.Linq; -using System.Text; #if !NETFX_CORE using NUnit.Framework; #else @@ -11,6 +9,11 @@ using TestFixture = Microsoft.VisualStudio.TestTools.UnitTesting.TestClassAttrib using Test = Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute; #endif using Newtonsoft.Json.Linq; +#if NET20 +using Newtonsoft.Json.Utilities.LinqBridge; +#else +using System.Linq; +#endif namespace Newtonsoft.Json.Tests.Linq { diff --git a/Src/Newtonsoft.Json.Tests/Linq/JConstructorTests.cs b/Src/Newtonsoft.Json.Tests/Linq/JConstructorTests.cs index 7c43332f..e4ad6385 100644 --- a/Src/Newtonsoft.Json.Tests/Linq/JConstructorTests.cs +++ b/Src/Newtonsoft.Json.Tests/Linq/JConstructorTests.cs @@ -1,7 +1,5 @@ using System; using System.Collections.Generic; -using System.Linq; -using System.Text; using Newtonsoft.Json.Linq; #if !NETFX_CORE using NUnit.Framework; @@ -11,6 +9,11 @@ using TestFixture = Microsoft.VisualStudio.TestTools.UnitTesting.TestClassAttrib using Test = Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute; #endif using System.IO; +#if NET20 +using Newtonsoft.Json.Utilities.LinqBridge; +#else +using System.Linq; +#endif namespace Newtonsoft.Json.Tests.Linq { diff --git a/Src/Newtonsoft.Json.Tests/Linq/JObjectTests.cs b/Src/Newtonsoft.Json.Tests/Linq/JObjectTests.cs index 5790febc..9e8c69a7 100644 --- a/Src/Newtonsoft.Json.Tests/Linq/JObjectTests.cs +++ b/Src/Newtonsoft.Json.Tests/Linq/JObjectTests.cs @@ -1,8 +1,7 @@ using System; using System.Collections.Generic; +using System.Collections.Specialized; using System.ComponentModel; -using System.Linq; -using System.Text; using Newtonsoft.Json.Tests.TestObjects; #if !NETFX_CORE using NUnit.Framework; @@ -12,19 +11,35 @@ using TestFixture = Microsoft.VisualStudio.TestTools.UnitTesting.TestClassAttrib using Test = Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute; #endif using Newtonsoft.Json.Linq; -using Newtonsoft.Json.Converters; using System.IO; using System.Collections; -using System.Collections.Specialized; #if !PocketPC && !SILVERLIGHT && !NETFX_CORE using System.Web.UI; #endif +#if NET20 +using Newtonsoft.Json.Utilities.LinqBridge; +#else +using System.Linq; +#endif namespace Newtonsoft.Json.Tests.Linq { [TestFixture] public class JObjectTests : TestFixtureBase { + [Test] + public void Keys() + { + var o = new JObject(); + var d = (IDictionary) o; + + Assert.AreEqual(0, d.Keys.Count); + + o["value"] = true; + + Assert.AreEqual(1, d.Keys.Count); + } + [Test] public void TryGetValue() { diff --git a/Src/Newtonsoft.Json.Tests/Linq/JPathTests.cs b/Src/Newtonsoft.Json.Tests/Linq/JPathTests.cs index 108df02d..b4de0d31 100644 --- a/Src/Newtonsoft.Json.Tests/Linq/JPathTests.cs +++ b/Src/Newtonsoft.Json.Tests/Linq/JPathTests.cs @@ -1,9 +1,5 @@ using System; using System.Collections.Generic; -using System.ComponentModel; -using System.Linq; -using System.Text; -using Newtonsoft.Json.Tests.TestObjects; #if !NETFX_CORE using NUnit.Framework; #else @@ -12,9 +8,11 @@ using TestFixture = Microsoft.VisualStudio.TestTools.UnitTesting.TestClassAttrib using Test = Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute; #endif using Newtonsoft.Json.Linq; -using Newtonsoft.Json.Converters; -using System.IO; -using System.Collections; +#if NET20 +using Newtonsoft.Json.Utilities.LinqBridge; +#else +using System.Linq; +#endif namespace Newtonsoft.Json.Tests.Linq { diff --git a/Src/Newtonsoft.Json.Tests/Linq/JPropertyTests.cs b/Src/Newtonsoft.Json.Tests/Linq/JPropertyTests.cs index a1881965..d2105263 100644 --- a/Src/Newtonsoft.Json.Tests/Linq/JPropertyTests.cs +++ b/Src/Newtonsoft.Json.Tests/Linq/JPropertyTests.cs @@ -2,8 +2,6 @@ using System.Collections; using System.Collections.Generic; using System.ComponentModel; -using System.Linq; -using System.Text; using Newtonsoft.Json.Linq; #if !NETFX_CORE using NUnit.Framework; diff --git a/Src/Newtonsoft.Json.Tests/Linq/JRawTests.cs b/Src/Newtonsoft.Json.Tests/Linq/JRawTests.cs index d52d4ab1..8d849066 100644 --- a/Src/Newtonsoft.Json.Tests/Linq/JRawTests.cs +++ b/Src/Newtonsoft.Json.Tests/Linq/JRawTests.cs @@ -1,8 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -#if !NETFX_CORE +#if !NETFX_CORE using NUnit.Framework; #else using Microsoft.VisualStudio.TestTools.UnitTesting; diff --git a/Src/Newtonsoft.Json.Tests/Linq/JTokenEqualityComparerTests.cs b/Src/Newtonsoft.Json.Tests/Linq/JTokenEqualityComparerTests.cs index feb8c6ef..e58d9309 100644 --- a/Src/Newtonsoft.Json.Tests/Linq/JTokenEqualityComparerTests.cs +++ b/Src/Newtonsoft.Json.Tests/Linq/JTokenEqualityComparerTests.cs @@ -1,7 +1,4 @@ -using System; using System.Collections.Generic; -using System.Linq; -using System.Text; #if !NETFX_CORE using NUnit.Framework; #else diff --git a/Src/Newtonsoft.Json.Tests/Linq/JTokenTests.cs b/Src/Newtonsoft.Json.Tests/Linq/JTokenTests.cs index 1cf69068..79821c70 100644 --- a/Src/Newtonsoft.Json.Tests/Linq/JTokenTests.cs +++ b/Src/Newtonsoft.Json.Tests/Linq/JTokenTests.cs @@ -25,7 +25,6 @@ using System; using System.Collections.Generic; -using System.Linq; using System.Text; using Newtonsoft.Json.Converters; #if !NETFX_CORE @@ -37,6 +36,11 @@ using Test = Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute; #endif using Newtonsoft.Json.Linq; using System.IO; +#if NET20 +using Newtonsoft.Json.Utilities.LinqBridge; +#else +using System.Linq; +#endif namespace Newtonsoft.Json.Tests.Linq { diff --git a/Src/Newtonsoft.Json.Tests/Linq/JTokenWriterTest.cs b/Src/Newtonsoft.Json.Tests/Linq/JTokenWriterTest.cs index 08f28546..943fef3f 100644 --- a/Src/Newtonsoft.Json.Tests/Linq/JTokenWriterTest.cs +++ b/Src/Newtonsoft.Json.Tests/Linq/JTokenWriterTest.cs @@ -36,7 +36,11 @@ using Test = Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute; using Newtonsoft.Json; using System.IO; using Newtonsoft.Json.Linq; +#if NET20 +using Newtonsoft.Json.Utilities.LinqBridge; +#else using System.Linq; +#endif namespace Newtonsoft.Json.Tests.Linq { diff --git a/Src/Newtonsoft.Json.Tests/Linq/JValueTests.cs b/Src/Newtonsoft.Json.Tests/Linq/JValueTests.cs index b6373756..f472a52d 100644 --- a/Src/Newtonsoft.Json.Tests/Linq/JValueTests.cs +++ b/Src/Newtonsoft.Json.Tests/Linq/JValueTests.cs @@ -25,7 +25,6 @@ using System; using System.Collections.Generic; -using System.Linq; using System.Text; #if !NETFX_CORE using NUnit.Framework; @@ -36,6 +35,11 @@ using Test = Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute; #endif using Newtonsoft.Json.Linq; using System.Globalization; +#if NET20 +using Newtonsoft.Json.Utilities.LinqBridge; +#else +using System.Linq; +#endif namespace Newtonsoft.Json.Tests.Linq { diff --git a/Src/Newtonsoft.Json.Tests/Linq/LinqToJsonTest.cs b/Src/Newtonsoft.Json.Tests/Linq/LinqToJsonTest.cs index 7975b6d0..fbf09f37 100644 --- a/Src/Newtonsoft.Json.Tests/Linq/LinqToJsonTest.cs +++ b/Src/Newtonsoft.Json.Tests/Linq/LinqToJsonTest.cs @@ -26,21 +26,23 @@ using System; using System.Collections.Generic; using System.Globalization; -using System.Linq; -using System.Text; #if !NETFX_CORE +using System.IO; using NUnit.Framework; #else using Microsoft.VisualStudio.TestTools.UnitTesting; using TestFixture = Microsoft.VisualStudio.TestTools.UnitTesting.TestClassAttribute; using Test = Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute; #endif -using Newtonsoft.Json.Linq; -using System.Xml; -using System.IO; using Newtonsoft.Json.Converters; +using Newtonsoft.Json.Linq; using Newtonsoft.Json.Tests.Serialization; using Newtonsoft.Json.Tests.TestObjects; +#if NET20 +using Newtonsoft.Json.Utilities.LinqBridge; +#else +using System.Linq; +#endif namespace Newtonsoft.Json.Tests.Linq { diff --git a/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs b/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs index c6bc0562..e55896be 100644 --- a/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs +++ b/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs @@ -72,5 +72,5 @@ using System.Security; // by using the '*' as shown below: [assembly: AssemblyVersion("4.0.8.0")] #if !PocketPC -[assembly: AssemblyFileVersion("4.0.8.14715")] +[assembly: AssemblyFileVersion("4.0.8.14718")] #endif diff --git a/Src/Newtonsoft.Json.Tests/Schema/ExtensionsTests.cs b/Src/Newtonsoft.Json.Tests/Schema/ExtensionsTests.cs index 586f7931..fa10be12 100644 --- a/Src/Newtonsoft.Json.Tests/Schema/ExtensionsTests.cs +++ b/Src/Newtonsoft.Json.Tests/Schema/ExtensionsTests.cs @@ -25,8 +25,6 @@ using System; using System.Collections.Generic; -using System.Linq; -using System.Text; #if !NETFX_CORE using NUnit.Framework; #else diff --git a/Src/Newtonsoft.Json.Tests/Schema/JsonSchemaBuilderTests.cs b/Src/Newtonsoft.Json.Tests/Schema/JsonSchemaBuilderTests.cs index 97912867..50721d0d 100644 --- a/Src/Newtonsoft.Json.Tests/Schema/JsonSchemaBuilderTests.cs +++ b/Src/Newtonsoft.Json.Tests/Schema/JsonSchemaBuilderTests.cs @@ -24,9 +24,6 @@ #endregion using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; #if !NETFX_CORE using NUnit.Framework; #else diff --git a/Src/Newtonsoft.Json.Tests/Schema/JsonSchemaGeneratorTests.cs b/Src/Newtonsoft.Json.Tests/Schema/JsonSchemaGeneratorTests.cs index 1e29588e..7f91c86c 100644 --- a/Src/Newtonsoft.Json.Tests/Schema/JsonSchemaGeneratorTests.cs +++ b/Src/Newtonsoft.Json.Tests/Schema/JsonSchemaGeneratorTests.cs @@ -40,10 +40,14 @@ using Test = Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute; #endif using Newtonsoft.Json.Schema; using System.IO; -using System.Linq; using Newtonsoft.Json.Linq; using System.Text; using Extensions=Newtonsoft.Json.Schema.Extensions; +#if NET20 +using Newtonsoft.Json.Utilities.LinqBridge; +#else +using System.Linq; +#endif namespace Newtonsoft.Json.Tests.Schema { diff --git a/Src/Newtonsoft.Json.Tests/Schema/JsonSchemaModelBuilderTests.cs b/Src/Newtonsoft.Json.Tests/Schema/JsonSchemaModelBuilderTests.cs index accc5ed2..9806c529 100644 --- a/Src/Newtonsoft.Json.Tests/Schema/JsonSchemaModelBuilderTests.cs +++ b/Src/Newtonsoft.Json.Tests/Schema/JsonSchemaModelBuilderTests.cs @@ -23,11 +23,6 @@ // OTHER DEALINGS IN THE SOFTWARE. #endregion -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Text; using Newtonsoft.Json.Schema; #if !NETFX_CORE using NUnit.Framework; diff --git a/Src/Newtonsoft.Json.Tests/Schema/JsonSchemaNodeTests.cs b/Src/Newtonsoft.Json.Tests/Schema/JsonSchemaNodeTests.cs index 35542bcb..92ccde98 100644 --- a/Src/Newtonsoft.Json.Tests/Schema/JsonSchemaNodeTests.cs +++ b/Src/Newtonsoft.Json.Tests/Schema/JsonSchemaNodeTests.cs @@ -23,10 +23,6 @@ // OTHER DEALINGS IN THE SOFTWARE. #endregion -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; using Newtonsoft.Json.Schema; #if !NETFX_CORE using NUnit.Framework; diff --git a/Src/Newtonsoft.Json.Tests/Schema/JsonSchemaTests.cs b/Src/Newtonsoft.Json.Tests/Schema/JsonSchemaTests.cs index 89f26e7a..15c010d7 100644 --- a/Src/Newtonsoft.Json.Tests/Schema/JsonSchemaTests.cs +++ b/Src/Newtonsoft.Json.Tests/Schema/JsonSchemaTests.cs @@ -23,11 +23,8 @@ // OTHER DEALINGS IN THE SOFTWARE. #endregion -using System; using System.Collections.Generic; using System.IO; -using System.Linq; -using System.Text; using Newtonsoft.Json.Schema; #if !NETFX_CORE using NUnit.Framework; diff --git a/Src/Newtonsoft.Json.Tests/Serialization/CamelCasePropertyNamesContractResolverTests.cs b/Src/Newtonsoft.Json.Tests/Serialization/CamelCasePropertyNamesContractResolverTests.cs index d3b0d507..26985cc3 100644 --- a/Src/Newtonsoft.Json.Tests/Serialization/CamelCasePropertyNamesContractResolverTests.cs +++ b/Src/Newtonsoft.Json.Tests/Serialization/CamelCasePropertyNamesContractResolverTests.cs @@ -25,8 +25,6 @@ using System; using System.Collections.Generic; -using System.Linq; -using System.Text; using Newtonsoft.Json.Serialization; #if !NETFX_CORE using NUnit.Framework; diff --git a/Src/Newtonsoft.Json.Tests/Serialization/JsonPropertyCollectionTests.cs b/Src/Newtonsoft.Json.Tests/Serialization/JsonPropertyCollectionTests.cs index 6ab0df56..5f927dbb 100644 --- a/Src/Newtonsoft.Json.Tests/Serialization/JsonPropertyCollectionTests.cs +++ b/Src/Newtonsoft.Json.Tests/Serialization/JsonPropertyCollectionTests.cs @@ -23,10 +23,6 @@ // OTHER DEALINGS IN THE SOFTWARE. #endregion -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; #if !NETFX_CORE using NUnit.Framework; #else diff --git a/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs b/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs index e8f1a894..2f038a09 100644 --- a/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs +++ b/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs @@ -52,11 +52,11 @@ using System.Xml.Serialization; using System.Collections.ObjectModel; using Newtonsoft.Json.Bson; using Newtonsoft.Json.Linq; -using System.Linq; using Newtonsoft.Json.Converters; #if !PocketPC && !NET20 && !WINDOWS_PHONE using System.Runtime.Serialization.Json; #endif +using Newtonsoft.Json.Serialization; using Newtonsoft.Json.Tests.TestObjects; using System.Runtime.Serialization; using System.Globalization; @@ -72,6 +72,11 @@ using System.Linq.Expressions; using System.Dynamic; using System.ComponentModel; #endif +#if NET20 +using Newtonsoft.Json.Utilities.LinqBridge; +#else +using System.Linq; +#endif namespace Newtonsoft.Json.Tests.Serialization { @@ -2789,6 +2794,86 @@ To force JSON objects to deserialize add the JsonObjectAttribute to the type. Li } } +#if DEBUG + [Test] + public void SerializeISerializableInPartialTrustWithIgnoreInterface() + { + try + { + JsonTypeReflector.SetFullyTrusted(false); + ISerializableTestObject value = new ISerializableTestObject("string!", 0, default(DateTimeOffset), null); + + string json = JsonConvert.SerializeObject(value, new JsonSerializerSettings + { + ContractResolver = new DefaultContractResolver(false) + { + IgnoreSerializableInterface = true + } + }); + + Assert.AreEqual("{}", json); + + value = JsonConvert.DeserializeObject("{booleanValue:true}", new JsonSerializerSettings + { + ContractResolver = new DefaultContractResolver(false) + { + IgnoreSerializableInterface = true + } + }); + + Assert.IsNotNull(value); + Assert.AreEqual(false, value._booleanValue); + } + finally + { + JsonTypeReflector.SetFullyTrusted(true); + } + } + + [Test] + public void SerializeISerializableInPartialTrust() + { + try + { + ExceptionAssert.Throws( + @"Type 'Newtonsoft.Json.Tests.Serialization.JsonSerializerTest+ISerializableTestObject' implements ISerializable but cannot be deserialized using the ISerializable interface because the current application is not fully trusted and ISerializable can expose secure data. +To fix this error either change the environment to be fully trusted, change the application to not deserialize the type, add to JsonObjectAttribute to the type or change the JsonSerializer setting ContractResolver to use a new DefaultContractResolver with IgnoreSerializableInterface set to true.", + () => + { + JsonTypeReflector.SetFullyTrusted(false); + + JsonConvert.DeserializeObject("{booleanValue:true}"); + }); + } + finally + { + JsonTypeReflector.SetFullyTrusted(true); + } + } + + [Test] + public void DeserializeISerializableInPartialTrust() + { + try + { + ExceptionAssert.Throws( + @"Type 'Newtonsoft.Json.Tests.Serialization.JsonSerializerTest+ISerializableTestObject' implements ISerializable but cannot be serialized using the ISerializable interface because the current application is not fully trusted and ISerializable can expose secure data. +To fix this error either change the environment to be fully trusted, change the application to not deserialize the type, add to JsonObjectAttribute to the type or change the JsonSerializer setting ContractResolver to use a new DefaultContractResolver with IgnoreSerializableInterface set to true.", + () => + { + JsonTypeReflector.SetFullyTrusted(false); + ISerializableTestObject value = new ISerializableTestObject("string!", 0, default(DateTimeOffset), null); + + JsonConvert.SerializeObject(value); + }); + } + finally + { + JsonTypeReflector.SetFullyTrusted(true); + } + } +#endif + [Test] public void SerializeISerializableTestObject_IsoDate() { diff --git a/Src/Newtonsoft.Json.Tests/Serialization/PopulateTests.cs b/Src/Newtonsoft.Json.Tests/Serialization/PopulateTests.cs index b79f1375..8ed61219 100644 --- a/Src/Newtonsoft.Json.Tests/Serialization/PopulateTests.cs +++ b/Src/Newtonsoft.Json.Tests/Serialization/PopulateTests.cs @@ -26,8 +26,6 @@ using System; using System.Collections.Generic; using System.IO; -using System.Linq; -using System.Text; using Newtonsoft.Json.Tests.TestObjects; #if !NETFX_CORE using NUnit.Framework; @@ -36,7 +34,6 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; using TestFixture = Microsoft.VisualStudio.TestTools.UnitTesting.TestClassAttribute; using Test = Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute; #endif -using Newtonsoft.Json.Linq; namespace Newtonsoft.Json.Tests.Serialization { diff --git a/Src/Newtonsoft.Json.Tests/Serialization/PreserveReferencesHandlingTests.cs b/Src/Newtonsoft.Json.Tests/Serialization/PreserveReferencesHandlingTests.cs index e6012bf4..97e82a68 100644 --- a/Src/Newtonsoft.Json.Tests/Serialization/PreserveReferencesHandlingTests.cs +++ b/Src/Newtonsoft.Json.Tests/Serialization/PreserveReferencesHandlingTests.cs @@ -26,7 +26,6 @@ using System; using System.Collections.Generic; using System.IO; -using System.Linq; using System.Text; using Newtonsoft.Json.Linq; using Newtonsoft.Json.Tests.TestObjects; diff --git a/Src/Newtonsoft.Json.Tests/Serialization/SerializationErrorHandlingTests.cs b/Src/Newtonsoft.Json.Tests/Serialization/SerializationErrorHandlingTests.cs index 5387a786..fda45089 100644 --- a/Src/Newtonsoft.Json.Tests/Serialization/SerializationErrorHandlingTests.cs +++ b/Src/Newtonsoft.Json.Tests/Serialization/SerializationErrorHandlingTests.cs @@ -25,8 +25,6 @@ using System; using System.Collections.Generic; -using System.Linq; -using System.Text; using Newtonsoft.Json.Converters; using Newtonsoft.Json.Tests.TestObjects; #if !NETFX_CORE @@ -36,7 +34,6 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; using TestFixture = Microsoft.VisualStudio.TestTools.UnitTesting.TestClassAttribute; using Test = Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute; #endif -using Newtonsoft.Json.Serialization; using System.IO; using ErrorEventArgs=Newtonsoft.Json.Serialization.ErrorEventArgs; diff --git a/Src/Newtonsoft.Json.Tests/Serialization/SerializationEventAttributeTests.cs b/Src/Newtonsoft.Json.Tests/Serialization/SerializationEventAttributeTests.cs index 27a0aa4b..16b2b635 100644 --- a/Src/Newtonsoft.Json.Tests/Serialization/SerializationEventAttributeTests.cs +++ b/Src/Newtonsoft.Json.Tests/Serialization/SerializationEventAttributeTests.cs @@ -27,12 +27,9 @@ using System; using System.Diagnostics; using System.IO; -using System.Linq; using System.Reflection; using System.Runtime.Serialization; -using System.Text; using Newtonsoft.Json.Serialization; -using Newtonsoft.Json.Tests; using Newtonsoft.Json.Tests.TestObjects; #if !NETFX_CORE using NUnit.Framework; @@ -42,8 +39,6 @@ using TestFixture = Microsoft.VisualStudio.TestTools.UnitTesting.TestClassAttrib using Test = Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute; #endif using Newtonsoft.Json.Linq; -using Newtonsoft.Json.Converters; -using Newtonsoft.Json.Utilities; namespace Newtonsoft.Json.Tests.Serialization { diff --git a/Src/Newtonsoft.Json.Tests/Serialization/TypeNameHandlingTests.cs b/Src/Newtonsoft.Json.Tests/Serialization/TypeNameHandlingTests.cs index be2aeff6..c5b2fefd 100644 --- a/Src/Newtonsoft.Json.Tests/Serialization/TypeNameHandlingTests.cs +++ b/Src/Newtonsoft.Json.Tests/Serialization/TypeNameHandlingTests.cs @@ -26,9 +26,7 @@ using System; using System.Collections; using System.Collections.Generic; -using System.Linq; using System.Runtime.Serialization.Formatters; -using System.Text; using Newtonsoft.Json.Linq; using Newtonsoft.Json.Tests.TestObjects; #if !NETFX_CORE diff --git a/Src/Newtonsoft.Json.Tests/TestFixtureBase.cs b/Src/Newtonsoft.Json.Tests/TestFixtureBase.cs index 90162e9b..c40987a5 100644 --- a/Src/Newtonsoft.Json.Tests/TestFixtureBase.cs +++ b/Src/Newtonsoft.Json.Tests/TestFixtureBase.cs @@ -27,7 +27,10 @@ using System; using System.Collections.Generic; using System.Globalization; using System.IO; -using System.Linq; +#if NET20 +using Newtonsoft.Json.Serialization; +using Newtonsoft.Json.Utilities.LinqBridge; +#endif using System.Text; using System.Threading; #if !NETFX_CORE @@ -75,12 +78,14 @@ namespace Newtonsoft.Json.Tests #endif } +#if NETFX_CORE public static class Console { public static void WriteLine(params object[] args) { } } +#endif public static class CustomAssert { diff --git a/Src/Newtonsoft.Json.Tests/TestObjects/Car.cs b/Src/Newtonsoft.Json.Tests/TestObjects/Car.cs index 0e27200e..47c81509 100644 --- a/Src/Newtonsoft.Json.Tests/TestObjects/Car.cs +++ b/Src/Newtonsoft.Json.Tests/TestObjects/Car.cs @@ -1,7 +1,5 @@ using System; using System.Collections.Generic; -using System.Linq; -using System.Text; namespace Newtonsoft.Json.Tests.TestObjects { diff --git a/Src/Newtonsoft.Json.Tests/TestObjects/CircularReferenceClass.cs b/Src/Newtonsoft.Json.Tests/TestObjects/CircularReferenceClass.cs index bdb40127..acfe149a 100644 --- a/Src/Newtonsoft.Json.Tests/TestObjects/CircularReferenceClass.cs +++ b/Src/Newtonsoft.Json.Tests/TestObjects/CircularReferenceClass.cs @@ -23,11 +23,6 @@ // OTHER DEALINGS IN THE SOFTWARE. #endregion -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - namespace Newtonsoft.Json.Tests.TestObjects { public class CircularReferenceClass diff --git a/Src/Newtonsoft.Json.Tests/TestObjects/CircularReferenceWithIdClass.cs b/Src/Newtonsoft.Json.Tests/TestObjects/CircularReferenceWithIdClass.cs index 4d4c8671..8ceaab08 100644 --- a/Src/Newtonsoft.Json.Tests/TestObjects/CircularReferenceWithIdClass.cs +++ b/Src/Newtonsoft.Json.Tests/TestObjects/CircularReferenceWithIdClass.cs @@ -23,11 +23,6 @@ // OTHER DEALINGS IN THE SOFTWARE. #endregion -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - namespace Newtonsoft.Json.Tests.TestObjects { [JsonObject(Id = "MyExplicitId")] diff --git a/Src/Newtonsoft.Json.Tests/TestObjects/ContentBaseClass.cs b/Src/Newtonsoft.Json.Tests/TestObjects/ContentBaseClass.cs index 6e505eb6..f3090f07 100644 --- a/Src/Newtonsoft.Json.Tests/TestObjects/ContentBaseClass.cs +++ b/Src/Newtonsoft.Json.Tests/TestObjects/ContentBaseClass.cs @@ -1,9 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace Newtonsoft.Json.Tests.TestObjects +namespace Newtonsoft.Json.Tests.TestObjects { public abstract class ContentBaseClass { diff --git a/Src/Newtonsoft.Json.Tests/TestObjects/ContentSubClass.cs b/Src/Newtonsoft.Json.Tests/TestObjects/ContentSubClass.cs index b88e2c87..609784d4 100644 --- a/Src/Newtonsoft.Json.Tests/TestObjects/ContentSubClass.cs +++ b/Src/Newtonsoft.Json.Tests/TestObjects/ContentSubClass.cs @@ -1,9 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace Newtonsoft.Json.Tests.TestObjects +namespace Newtonsoft.Json.Tests.TestObjects { public class ContentSubClass : ContentBaseClass { diff --git a/Src/Newtonsoft.Json.Tests/TestObjects/EmployeeReference.cs b/Src/Newtonsoft.Json.Tests/TestObjects/EmployeeReference.cs index 97c8b7a1..4dff08c9 100644 --- a/Src/Newtonsoft.Json.Tests/TestObjects/EmployeeReference.cs +++ b/Src/Newtonsoft.Json.Tests/TestObjects/EmployeeReference.cs @@ -23,11 +23,6 @@ // OTHER DEALINGS IN THE SOFTWARE. #endregion -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - namespace Newtonsoft.Json.Tests.TestObjects { [JsonObject(IsReference = true)] diff --git a/Src/Newtonsoft.Json.Tests/TestObjects/GenericListAndDictionaryInterfaceProperties.cs b/Src/Newtonsoft.Json.Tests/TestObjects/GenericListAndDictionaryInterfaceProperties.cs index 5514778d..59c619ff 100644 --- a/Src/Newtonsoft.Json.Tests/TestObjects/GenericListAndDictionaryInterfaceProperties.cs +++ b/Src/Newtonsoft.Json.Tests/TestObjects/GenericListAndDictionaryInterfaceProperties.cs @@ -1,7 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; +using System.Collections.Generic; namespace Newtonsoft.Json.Tests.TestObjects { diff --git a/Src/Newtonsoft.Json.Tests/TestObjects/HolderClass.cs b/Src/Newtonsoft.Json.Tests/TestObjects/HolderClass.cs index 14bcd5e8..01e10a46 100644 --- a/Src/Newtonsoft.Json.Tests/TestObjects/HolderClass.cs +++ b/Src/Newtonsoft.Json.Tests/TestObjects/HolderClass.cs @@ -1,7 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; +using System.Collections.Generic; namespace Newtonsoft.Json.Tests.TestObjects { diff --git a/Src/Newtonsoft.Json.Tests/TestObjects/JaggedArray.cs b/Src/Newtonsoft.Json.Tests/TestObjects/JaggedArray.cs index 9f52e98c..2efe77f2 100644 --- a/Src/Newtonsoft.Json.Tests/TestObjects/JaggedArray.cs +++ b/Src/Newtonsoft.Json.Tests/TestObjects/JaggedArray.cs @@ -1,9 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace Newtonsoft.Json.Tests.TestObjects +namespace Newtonsoft.Json.Tests.TestObjects { public class JaggedArray { diff --git a/Src/Newtonsoft.Json.Tests/TestObjects/Movie.cs b/Src/Newtonsoft.Json.Tests/TestObjects/Movie.cs index 40dbba2d..cf18dfc7 100644 --- a/Src/Newtonsoft.Json.Tests/TestObjects/Movie.cs +++ b/Src/Newtonsoft.Json.Tests/TestObjects/Movie.cs @@ -1,7 +1,5 @@ using System; using System.Collections.Generic; -using System.Linq; -using System.Text; namespace Newtonsoft.Json.Tests.TestObjects { diff --git a/Src/Newtonsoft.Json.Tests/TestObjects/PersonError.cs b/Src/Newtonsoft.Json.Tests/TestObjects/PersonError.cs index 7a9483bf..06e04bf0 100644 --- a/Src/Newtonsoft.Json.Tests/TestObjects/PersonError.cs +++ b/Src/Newtonsoft.Json.Tests/TestObjects/PersonError.cs @@ -1,8 +1,6 @@ using System; using System.Collections.Generic; -using System.Linq; using System.Runtime.Serialization; -using System.Text; using Newtonsoft.Json.Serialization; namespace Newtonsoft.Json.Tests.TestObjects diff --git a/Src/Newtonsoft.Json.Tests/TestObjects/PersonPropertyClass.cs b/Src/Newtonsoft.Json.Tests/TestObjects/PersonPropertyClass.cs index a8d9a859..0ad339a8 100644 --- a/Src/Newtonsoft.Json.Tests/TestObjects/PersonPropertyClass.cs +++ b/Src/Newtonsoft.Json.Tests/TestObjects/PersonPropertyClass.cs @@ -1,9 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace Newtonsoft.Json.Tests.TestObjects +namespace Newtonsoft.Json.Tests.TestObjects { public class PersonPropertyClass { diff --git a/Src/Newtonsoft.Json.Tests/TestObjects/PrivateMembersClass.cs b/Src/Newtonsoft.Json.Tests/TestObjects/PrivateMembersClass.cs index c552d447..88ff087c 100644 --- a/Src/Newtonsoft.Json.Tests/TestObjects/PrivateMembersClass.cs +++ b/Src/Newtonsoft.Json.Tests/TestObjects/PrivateMembersClass.cs @@ -23,11 +23,6 @@ // OTHER DEALINGS IN THE SOFTWARE. #endregion -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - namespace Newtonsoft.Json.Tests.TestObjects { public class PrivateMembersClass diff --git a/Src/Newtonsoft.Json.Tests/TestObjects/PublicParametizedConstructorRequiringConverterTestClass.cs b/Src/Newtonsoft.Json.Tests/TestObjects/PublicParametizedConstructorRequiringConverterTestClass.cs index 0b94146d..194d1ae6 100644 --- a/Src/Newtonsoft.Json.Tests/TestObjects/PublicParametizedConstructorRequiringConverterTestClass.cs +++ b/Src/Newtonsoft.Json.Tests/TestObjects/PublicParametizedConstructorRequiringConverterTestClass.cs @@ -1,7 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; namespace Newtonsoft.Json.Tests.TestObjects { diff --git a/Src/Newtonsoft.Json.Tests/TestObjects/PublicParametizedConstructorTestClass.cs b/Src/Newtonsoft.Json.Tests/TestObjects/PublicParametizedConstructorTestClass.cs index 6cace1ff..8273ddeb 100644 --- a/Src/Newtonsoft.Json.Tests/TestObjects/PublicParametizedConstructorTestClass.cs +++ b/Src/Newtonsoft.Json.Tests/TestObjects/PublicParametizedConstructorTestClass.cs @@ -1,9 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace Newtonsoft.Json.Tests.TestObjects +namespace Newtonsoft.Json.Tests.TestObjects { public class PublicParametizedConstructorTestClass { diff --git a/Src/Newtonsoft.Json.Tests/TestObjects/PublicParametizedConstructorWithNonPropertyParameterTestClass.cs b/Src/Newtonsoft.Json.Tests/TestObjects/PublicParametizedConstructorWithNonPropertyParameterTestClass.cs index 57dbb649..694b7a05 100644 --- a/Src/Newtonsoft.Json.Tests/TestObjects/PublicParametizedConstructorWithNonPropertyParameterTestClass.cs +++ b/Src/Newtonsoft.Json.Tests/TestObjects/PublicParametizedConstructorWithNonPropertyParameterTestClass.cs @@ -1,9 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace Newtonsoft.Json.Tests.TestObjects +namespace Newtonsoft.Json.Tests.TestObjects { public class PublicParametizedConstructorWithNonPropertyParameterTestClass { diff --git a/Src/Newtonsoft.Json.Tests/TestObjects/PublicParametizedConstructorWithPropertyNameConflict.cs b/Src/Newtonsoft.Json.Tests/TestObjects/PublicParametizedConstructorWithPropertyNameConflict.cs index 1baaa7e3..76d0c527 100644 --- a/Src/Newtonsoft.Json.Tests/TestObjects/PublicParametizedConstructorWithPropertyNameConflict.cs +++ b/Src/Newtonsoft.Json.Tests/TestObjects/PublicParametizedConstructorWithPropertyNameConflict.cs @@ -1,7 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; namespace Newtonsoft.Json.Tests.TestObjects { diff --git a/Src/Newtonsoft.Json.Tests/TestObjects/RequiredMembersClass.cs b/Src/Newtonsoft.Json.Tests/TestObjects/RequiredMembersClass.cs index ce2f5b0b..1778a619 100644 --- a/Src/Newtonsoft.Json.Tests/TestObjects/RequiredMembersClass.cs +++ b/Src/Newtonsoft.Json.Tests/TestObjects/RequiredMembersClass.cs @@ -24,9 +24,6 @@ #endregion using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; namespace Newtonsoft.Json.Tests.TestObjects { diff --git a/Src/Newtonsoft.Json.Tests/TestObjects/SerializationEventTestObject.cs b/Src/Newtonsoft.Json.Tests/TestObjects/SerializationEventTestObject.cs index 9289b149..3853ca4e 100644 --- a/Src/Newtonsoft.Json.Tests/TestObjects/SerializationEventTestObject.cs +++ b/Src/Newtonsoft.Json.Tests/TestObjects/SerializationEventTestObject.cs @@ -25,9 +25,7 @@ #if !PocketPC using System; -using System.Linq; using System.Runtime.Serialization; -using System.Text; using Newtonsoft.Json.Serialization; namespace Newtonsoft.Json.Tests.TestObjects diff --git a/Src/Newtonsoft.Json.Tests/TestObjects/Shortie.cs b/Src/Newtonsoft.Json.Tests/TestObjects/Shortie.cs index f1691ec2..754b5535 100644 --- a/Src/Newtonsoft.Json.Tests/TestObjects/Shortie.cs +++ b/Src/Newtonsoft.Json.Tests/TestObjects/Shortie.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Xml.Serialization; - -namespace Newtonsoft.Json.Tests.TestObjects +namespace Newtonsoft.Json.Tests.TestObjects { public class Shortie { diff --git a/Src/Newtonsoft.Json.Tests/TestObjects/WagePerson.cs b/Src/Newtonsoft.Json.Tests/TestObjects/WagePerson.cs index d8c267e3..2b63f244 100644 --- a/Src/Newtonsoft.Json.Tests/TestObjects/WagePerson.cs +++ b/Src/Newtonsoft.Json.Tests/TestObjects/WagePerson.cs @@ -1,9 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace Newtonsoft.Json.Tests.TestObjects +namespace Newtonsoft.Json.Tests.TestObjects { public class WagePerson : Person { diff --git a/Src/Newtonsoft.Json.Tests/Utilities/DynamicReflectionDelegateFactoryTests.cs b/Src/Newtonsoft.Json.Tests/Utilities/DynamicReflectionDelegateFactoryTests.cs index 6da97cb5..384f71a0 100644 --- a/Src/Newtonsoft.Json.Tests/Utilities/DynamicReflectionDelegateFactoryTests.cs +++ b/Src/Newtonsoft.Json.Tests/Utilities/DynamicReflectionDelegateFactoryTests.cs @@ -25,12 +25,7 @@ #if !PocketPC && !SILVERLIGHT using System; -using System.Collections.Generic; -using System.Linq; using System.Reflection; -using System.Reflection.Emit; -using System.Text; -using Newtonsoft.Json.Tests.Linq; #if !NETFX_CORE using NUnit.Framework; #else @@ -38,6 +33,7 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; using TestFixture = Microsoft.VisualStudio.TestTools.UnitTesting.TestClassAttribute; using Test = Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute; #endif +using Newtonsoft.Json.Serialization; using Newtonsoft.Json.Utilities; using Newtonsoft.Json.Tests.TestObjects; using Newtonsoft.Json.Tests.Serialization; diff --git a/Src/Newtonsoft.Json.Tests/Utilities/ReflectionUtilsTests.cs b/Src/Newtonsoft.Json.Tests/Utilities/ReflectionUtilsTests.cs index a4b08385..d0e3cfa3 100644 --- a/Src/Newtonsoft.Json.Tests/Utilities/ReflectionUtilsTests.cs +++ b/Src/Newtonsoft.Json.Tests/Utilities/ReflectionUtilsTests.cs @@ -1,8 +1,6 @@ using System; using System.Collections.Generic; -using System.Linq; using System.Runtime.Serialization.Formatters; -using System.Text; #if !NETFX_CORE using NUnit.Framework; #else diff --git a/Src/Newtonsoft.Json/Bson/BsonBinaryWriter.cs b/Src/Newtonsoft.Json/Bson/BsonBinaryWriter.cs index 7b5f8ceb..de2a3a9d 100644 --- a/Src/Newtonsoft.Json/Bson/BsonBinaryWriter.cs +++ b/Src/Newtonsoft.Json/Bson/BsonBinaryWriter.cs @@ -1,8 +1,6 @@ using System; -using System.Collections.Generic; using System.Globalization; using System.IO; -using System.Linq; using System.Text; using Newtonsoft.Json.Utilities; diff --git a/Src/Newtonsoft.Json/Bson/BsonObjectId.cs b/Src/Newtonsoft.Json/Bson/BsonObjectId.cs index 7fcf223d..b2fd4ee9 100644 --- a/Src/Newtonsoft.Json/Bson/BsonObjectId.cs +++ b/Src/Newtonsoft.Json/Bson/BsonObjectId.cs @@ -24,9 +24,6 @@ #endregion using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; using Newtonsoft.Json.Utilities; namespace Newtonsoft.Json.Bson diff --git a/Src/Newtonsoft.Json/ConstructorHandling.cs b/Src/Newtonsoft.Json/ConstructorHandling.cs index 123977bf..f561e01f 100644 --- a/Src/Newtonsoft.Json/ConstructorHandling.cs +++ b/Src/Newtonsoft.Json/ConstructorHandling.cs @@ -23,11 +23,6 @@ // OTHER DEALINGS IN THE SOFTWARE. #endregion -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - namespace Newtonsoft.Json { /// diff --git a/Src/Newtonsoft.Json/Converters/BsonObjectIdConverter.cs b/Src/Newtonsoft.Json/Converters/BsonObjectIdConverter.cs index e5697c22..ffa9f52b 100644 --- a/Src/Newtonsoft.Json/Converters/BsonObjectIdConverter.cs +++ b/Src/Newtonsoft.Json/Converters/BsonObjectIdConverter.cs @@ -1,7 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; using Newtonsoft.Json.Bson; using System.Globalization; using Newtonsoft.Json.Utilities; diff --git a/Src/Newtonsoft.Json/Converters/DateTimeConverterBase.cs b/Src/Newtonsoft.Json/Converters/DateTimeConverterBase.cs index 8c2f0681..e153ffc8 100644 --- a/Src/Newtonsoft.Json/Converters/DateTimeConverterBase.cs +++ b/Src/Newtonsoft.Json/Converters/DateTimeConverterBase.cs @@ -1,7 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; namespace Newtonsoft.Json.Converters { diff --git a/Src/Newtonsoft.Json/Converters/KeyValuePairConverter.cs b/Src/Newtonsoft.Json/Converters/KeyValuePairConverter.cs index 7ea57ea4..7beb3cf6 100644 --- a/Src/Newtonsoft.Json/Converters/KeyValuePairConverter.cs +++ b/Src/Newtonsoft.Json/Converters/KeyValuePairConverter.cs @@ -1,7 +1,5 @@ using System; using System.Collections.Generic; -using System.Linq; -using System.Text; using Newtonsoft.Json.Utilities; using System.Reflection; diff --git a/Src/Newtonsoft.Json/Converters/RegexConverter.cs b/Src/Newtonsoft.Json/Converters/RegexConverter.cs index 02ef524d..ac5aa7a2 100644 --- a/Src/Newtonsoft.Json/Converters/RegexConverter.cs +++ b/Src/Newtonsoft.Json/Converters/RegexConverter.cs @@ -1,7 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; using System.Text.RegularExpressions; using Newtonsoft.Json.Bson; using System.Globalization; diff --git a/Src/Newtonsoft.Json/Converters/StringEnumConverter.cs b/Src/Newtonsoft.Json/Converters/StringEnumConverter.cs index 137bc64e..6697b28d 100644 --- a/Src/Newtonsoft.Json/Converters/StringEnumConverter.cs +++ b/Src/Newtonsoft.Json/Converters/StringEnumConverter.cs @@ -26,10 +26,14 @@ using System; using System.Collections.Generic; using System.Globalization; -using System.Linq; using System.Reflection; using System.Runtime.Serialization; using Newtonsoft.Json.Utilities; +#if NET20 +using Newtonsoft.Json.Utilities.LinqBridge; +#else +using System.Linq; +#endif namespace Newtonsoft.Json.Converters { diff --git a/Src/Newtonsoft.Json/Converters/VersionConverter.cs b/Src/Newtonsoft.Json/Converters/VersionConverter.cs index 67a9b66d..ae551561 100644 --- a/Src/Newtonsoft.Json/Converters/VersionConverter.cs +++ b/Src/Newtonsoft.Json/Converters/VersionConverter.cs @@ -24,9 +24,6 @@ #endregion using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; using System.Globalization; using Newtonsoft.Json.Utilities; diff --git a/Src/Newtonsoft.Json/Converters/XmlNodeConverter.cs b/Src/Newtonsoft.Json/Converters/XmlNodeConverter.cs index 300e5e85..faa3bdfc 100644 --- a/Src/Newtonsoft.Json/Converters/XmlNodeConverter.cs +++ b/Src/Newtonsoft.Json/Converters/XmlNodeConverter.cs @@ -32,7 +32,11 @@ using System.Xml; using System.Xml.Linq; #endif using Newtonsoft.Json.Utilities; +#if NET20 +using Newtonsoft.Json.Utilities.LinqBridge; +#else using System.Linq; +#endif namespace Newtonsoft.Json.Converters { diff --git a/Src/Newtonsoft.Json/DefaultValueHandling.cs b/Src/Newtonsoft.Json/DefaultValueHandling.cs index 72c265fa..42123b4f 100644 --- a/Src/Newtonsoft.Json/DefaultValueHandling.cs +++ b/Src/Newtonsoft.Json/DefaultValueHandling.cs @@ -24,9 +24,6 @@ #endregion using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; namespace Newtonsoft.Json { diff --git a/Src/Newtonsoft.Json/IJsonLineInfo.cs b/Src/Newtonsoft.Json/IJsonLineInfo.cs index 29c38f0e..18cc788b 100644 --- a/Src/Newtonsoft.Json/IJsonLineInfo.cs +++ b/Src/Newtonsoft.Json/IJsonLineInfo.cs @@ -1,9 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace Newtonsoft.Json +namespace Newtonsoft.Json { /// /// Provides an interface to enable a class to return line and position information. diff --git a/Src/Newtonsoft.Json/JsonArrayAttribute.cs b/Src/Newtonsoft.Json/JsonArrayAttribute.cs index 64a5d02a..7720fd5c 100644 --- a/Src/Newtonsoft.Json/JsonArrayAttribute.cs +++ b/Src/Newtonsoft.Json/JsonArrayAttribute.cs @@ -24,9 +24,6 @@ #endregion using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; namespace Newtonsoft.Json { diff --git a/Src/Newtonsoft.Json/JsonConstructorAttribute.cs b/Src/Newtonsoft.Json/JsonConstructorAttribute.cs index 192a5066..ef3e07c8 100644 --- a/Src/Newtonsoft.Json/JsonConstructorAttribute.cs +++ b/Src/Newtonsoft.Json/JsonConstructorAttribute.cs @@ -1,7 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; namespace Newtonsoft.Json { diff --git a/Src/Newtonsoft.Json/JsonContainerAttribute.cs b/Src/Newtonsoft.Json/JsonContainerAttribute.cs index a83c8287..f65193a1 100644 --- a/Src/Newtonsoft.Json/JsonContainerAttribute.cs +++ b/Src/Newtonsoft.Json/JsonContainerAttribute.cs @@ -24,9 +24,6 @@ #endregion using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; namespace Newtonsoft.Json { diff --git a/Src/Newtonsoft.Json/JsonConverterAttribute.cs b/Src/Newtonsoft.Json/JsonConverterAttribute.cs index ae3a8d57..92a48d5c 100644 --- a/Src/Newtonsoft.Json/JsonConverterAttribute.cs +++ b/Src/Newtonsoft.Json/JsonConverterAttribute.cs @@ -1,7 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; using Newtonsoft.Json.Utilities; using System.Globalization; diff --git a/Src/Newtonsoft.Json/JsonReader.cs b/Src/Newtonsoft.Json/JsonReader.cs index 98ab579f..30363830 100644 --- a/Src/Newtonsoft.Json/JsonReader.cs +++ b/Src/Newtonsoft.Json/JsonReader.cs @@ -27,9 +27,12 @@ using System; using System.Collections.Generic; using System.IO; using System.Globalization; -using System.Linq; -using Newtonsoft.Json.Linq; using Newtonsoft.Json.Utilities; +#if NET20 +using Newtonsoft.Json.Utilities.LinqBridge; +#else +using System.Linq; +#endif namespace Newtonsoft.Json { diff --git a/Src/Newtonsoft.Json/JsonSerializerSettings.cs b/Src/Newtonsoft.Json/JsonSerializerSettings.cs index 729d58ad..b60596dc 100644 --- a/Src/Newtonsoft.Json/JsonSerializerSettings.cs +++ b/Src/Newtonsoft.Json/JsonSerializerSettings.cs @@ -1,11 +1,8 @@ using System; using System.Collections.Generic; using System.Globalization; -using System.Linq; using System.Runtime.Serialization.Formatters; -using System.Text; using Newtonsoft.Json.Serialization; -using Newtonsoft.Json.Utilities; using System.Runtime.Serialization; namespace Newtonsoft.Json diff --git a/Src/Newtonsoft.Json/JsonValidatingReader.cs b/Src/Newtonsoft.Json/JsonValidatingReader.cs index 2bf2af66..cb26d100 100644 --- a/Src/Newtonsoft.Json/JsonValidatingReader.cs +++ b/Src/Newtonsoft.Json/JsonValidatingReader.cs @@ -25,14 +25,17 @@ using System; using System.Collections.Generic; -using System.Linq; -using System.Text; using Newtonsoft.Json.Linq; using Newtonsoft.Json.Schema; using Newtonsoft.Json.Utilities; using System.Globalization; using System.Text.RegularExpressions; using System.IO; +#if NET20 +using Newtonsoft.Json.Utilities.LinqBridge; +#else +using System.Linq; +#endif namespace Newtonsoft.Json { diff --git a/Src/Newtonsoft.Json/JsonWriter.cs b/Src/Newtonsoft.Json/JsonWriter.cs index 9882f4fb..b0d87d91 100644 --- a/Src/Newtonsoft.Json/JsonWriter.cs +++ b/Src/Newtonsoft.Json/JsonWriter.cs @@ -25,16 +25,17 @@ using System; using System.Collections.Generic; -using System.Linq; -using System.Text; using System.IO; -using System.Xml; using Newtonsoft.Json.Utilities; -using Newtonsoft.Json.Linq; using System.Globalization; #if NETFX_CORE using IConvertible = Newtonsoft.Json.Utilities.Convertible; #endif +#if NET20 +using Newtonsoft.Json.Utilities.LinqBridge; +#else +using System.Linq; +#endif namespace Newtonsoft.Json { diff --git a/Src/Newtonsoft.Json/Linq/Extensions.cs b/Src/Newtonsoft.Json/Linq/Extensions.cs index 250bbf94..80c4de94 100644 --- a/Src/Newtonsoft.Json/Linq/Extensions.cs +++ b/Src/Newtonsoft.Json/Linq/Extensions.cs @@ -25,11 +25,13 @@ using System; using System.Collections.Generic; -using System.Linq; -using System.Text; using Newtonsoft.Json.Utilities; -using System.Collections; using System.Globalization; +#if NET20 +using Newtonsoft.Json.Utilities.LinqBridge; +#else +using System.Linq; +#endif namespace Newtonsoft.Json.Linq { diff --git a/Src/Newtonsoft.Json/Linq/IJEnumerable.cs b/Src/Newtonsoft.Json/Linq/IJEnumerable.cs index 6291e99f..d90afac3 100644 --- a/Src/Newtonsoft.Json/Linq/IJEnumerable.cs +++ b/Src/Newtonsoft.Json/Linq/IJEnumerable.cs @@ -1,7 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; +using System.Collections.Generic; namespace Newtonsoft.Json.Linq { diff --git a/Src/Newtonsoft.Json/Linq/JArray.cs b/Src/Newtonsoft.Json/Linq/JArray.cs index da7d2a0d..a42fafeb 100644 --- a/Src/Newtonsoft.Json/Linq/JArray.cs +++ b/Src/Newtonsoft.Json/Linq/JArray.cs @@ -25,9 +25,6 @@ using System; using System.Collections.Generic; -using System.ComponentModel; -using System.Linq; -using System.Text; using Newtonsoft.Json.Utilities; using System.IO; using System.Globalization; diff --git a/Src/Newtonsoft.Json/Linq/JConstructor.cs b/Src/Newtonsoft.Json/Linq/JConstructor.cs index 3a9d26d4..87a36b6c 100644 --- a/Src/Newtonsoft.Json/Linq/JConstructor.cs +++ b/Src/Newtonsoft.Json/Linq/JConstructor.cs @@ -25,8 +25,6 @@ using System; using System.Collections.Generic; -using System.Linq; -using System.Text; using Newtonsoft.Json.Utilities; using System.Globalization; diff --git a/Src/Newtonsoft.Json/Linq/JContainer.cs b/Src/Newtonsoft.Json/Linq/JContainer.cs index 05f54dad..41d593fe 100644 --- a/Src/Newtonsoft.Json/Linq/JContainer.cs +++ b/Src/Newtonsoft.Json/Linq/JContainer.cs @@ -25,14 +25,17 @@ using System; using System.Collections.Generic; -using System.Linq; +using System.Collections.Specialized; using System.Threading; using Newtonsoft.Json.Utilities; using System.Collections; -using System.Diagnostics; using System.Globalization; using System.ComponentModel; -using System.Collections.Specialized; +#if NET20 +using Newtonsoft.Json.Utilities.LinqBridge; +#else +using System.Linq; +#endif namespace Newtonsoft.Json.Linq { diff --git a/Src/Newtonsoft.Json/Linq/JEnumerable.cs b/Src/Newtonsoft.Json/Linq/JEnumerable.cs index 799e411b..5bee8b2d 100644 --- a/Src/Newtonsoft.Json/Linq/JEnumerable.cs +++ b/Src/Newtonsoft.Json/Linq/JEnumerable.cs @@ -1,7 +1,9 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; +#if NET20 +using Newtonsoft.Json.Utilities.LinqBridge; +#else using System.Linq; -using System.Text; +#endif using Newtonsoft.Json.Utilities; using System.Collections; diff --git a/Src/Newtonsoft.Json/Linq/JObject.cs b/Src/Newtonsoft.Json/Linq/JObject.cs index 7d8dd29f..d24a7466 100644 --- a/Src/Newtonsoft.Json/Linq/JObject.cs +++ b/Src/Newtonsoft.Json/Linq/JObject.cs @@ -32,10 +32,14 @@ using System.ComponentModel; using System.Dynamic; using System.Linq.Expressions; #endif -using System.Linq; using System.IO; using Newtonsoft.Json.Utilities; using System.Globalization; +#if NET20 +using Newtonsoft.Json.Utilities.LinqBridge; +#else +using System.Linq; +#endif namespace Newtonsoft.Json.Linq { @@ -50,40 +54,7 @@ namespace Newtonsoft.Json.Linq , INotifyPropertyChanging #endif { - private class JPropertKeyedCollection : KeyedCollection - { - public JPropertKeyedCollection(IEqualityComparer comparer) - : base(comparer) - { - } - - protected override string GetKeyForItem(JToken item) - { - return ((JProperty) item).Name; - } - - protected override void InsertItem(int index, JToken item) - { - if (Dictionary == null) - { - base.InsertItem(index, item); - } - else - { - // need to override so that the dictionary key is always set rather than added - string keyForItem = GetKeyForItem(item); - Dictionary[keyForItem] = item; - Items.Insert(index, item); - } - } - - public new IDictionary Dictionary - { - get { return base.Dictionary; } - } - } - - private readonly JPropertKeyedCollection _properties = new JPropertKeyedCollection(StringComparer.Ordinal); + private readonly JPropertyKeyedCollection _properties = new JPropertyKeyedCollection(); /// /// Gets the container's children tokens. @@ -172,7 +143,7 @@ namespace Newtonsoft.Json.Linq return; } - if (_properties.Dictionary != null && _properties.Dictionary.TryGetValue(newProperty.Name, out existing)) + if (_properties.TryGetValue(newProperty.Name, out existing)) throw new ArgumentException("Can not add property {0} to {1}. Property with the same name already exists on object.".FormatWith(CultureInfo.InvariantCulture, newProperty.Name, GetType())); } @@ -224,13 +195,11 @@ namespace Newtonsoft.Json.Linq /// A with the specified name or null. public JProperty Property(string name) { - if (_properties.Dictionary == null) - return null; if (name == null) return null; JToken property; - _properties.Dictionary.TryGetValue(name, out property); + _properties.TryGetValue(name, out property); return (JProperty)property; } @@ -406,15 +375,13 @@ namespace Newtonsoft.Json.Linq bool IDictionary.ContainsKey(string key) { - if (_properties.Dictionary == null) - return false; - - return _properties.Dictionary.ContainsKey(key); + return _properties.Contains(key); } ICollection IDictionary.Keys { - get { return _properties.Dictionary.Keys; } + // todo: make order the collection returned match JObject order + get { return _properties.Keys; } } /// @@ -453,7 +420,11 @@ namespace Newtonsoft.Json.Linq ICollection IDictionary.Values { - get { return _properties.Dictionary.Values; } + get + { + // todo: need to wrap _properties.Values with a collection to get the JProperty value + throw new NotImplementedException(); + } } #endregion diff --git a/Src/Newtonsoft.Json/Linq/JProperty.cs b/Src/Newtonsoft.Json/Linq/JProperty.cs index 44568184..4c139c38 100644 --- a/Src/Newtonsoft.Json/Linq/JProperty.cs +++ b/Src/Newtonsoft.Json/Linq/JProperty.cs @@ -25,10 +25,6 @@ using System; using System.Collections.Generic; -using System.Collections.ObjectModel; -using System.ComponentModel; -using System.Linq; -using System.Text; using Newtonsoft.Json.Utilities; using System.Diagnostics; using System.Globalization; diff --git a/Src/Newtonsoft.Json/Linq/JPropertyKeyedCollection.cs b/Src/Newtonsoft.Json/Linq/JPropertyKeyedCollection.cs new file mode 100644 index 00000000..e72344fc --- /dev/null +++ b/Src/Newtonsoft.Json/Linq/JPropertyKeyedCollection.cs @@ -0,0 +1,168 @@ +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; + +namespace Newtonsoft.Json.Linq +{ + internal class JPropertyKeyedCollection : Collection + { + private static readonly IEqualityComparer Comparer = StringComparer.Ordinal; + + private Dictionary _dictionary; + + private void AddKey(string key, JToken item) + { + EnsureDictionary(); + _dictionary[key] = item; + } + + protected void ChangeItemKey(JToken item, string newKey) + { + if (!ContainsItem(item)) + throw new ArgumentException("The specified item does not exist in this KeyedCollection."); + + string keyForItem = GetKeyForItem(item); + if (!Comparer.Equals(keyForItem, newKey)) + { + if (newKey != null) + AddKey(newKey, item); + + if (keyForItem != null) + RemoveKey(keyForItem); + } + } + + protected override void ClearItems() + { + base.ClearItems(); + + if (_dictionary != null) + _dictionary.Clear(); + } + + public bool Contains(string key) + { + if (key == null) + throw new ArgumentNullException("key"); + + if (_dictionary != null) + return _dictionary.ContainsKey(key); + + return false; + } + + private bool ContainsItem(JToken item) + { + if (_dictionary == null) + return false; + + string key = GetKeyForItem(item); + JToken value; + return _dictionary.TryGetValue(key, out value); + } + + private void EnsureDictionary() + { + if (_dictionary == null) + _dictionary = new Dictionary(Comparer); + } + + private string GetKeyForItem(JToken item) + { + return ((JProperty)item).Name; + } + + protected override void InsertItem(int index, JToken item) + { + AddKey(GetKeyForItem(item), item); + base.InsertItem(index, item); + } + + public bool Remove(string key) + { + if (key == null) + throw new ArgumentNullException("key"); + + if (_dictionary != null) + return _dictionary.ContainsKey(key) && Remove(_dictionary[key]); + + return false; + } + + protected override void RemoveItem(int index) + { + string keyForItem = GetKeyForItem(Items[index]); + RemoveKey(keyForItem); + base.RemoveItem(index); + } + + private void RemoveKey(string key) + { + if (_dictionary != null) + _dictionary.Remove(key); + } + + protected override void SetItem(int index, JToken item) + { + string keyForItem = GetKeyForItem(item); + string keyAtIndex = GetKeyForItem(Items[index]); + + if (Comparer.Equals(keyAtIndex, keyForItem)) + { + if (_dictionary != null) + _dictionary[keyForItem] = item; + } + else + { + AddKey(keyForItem, item); + + if (keyAtIndex != null) + RemoveKey(keyAtIndex); + } + base.SetItem(index, item); + } + + public JToken this[string key] + { + get + { + if (key == null) + throw new ArgumentNullException("key"); + + if (_dictionary != null) + return _dictionary[key]; + + throw new KeyNotFoundException(); + } + } + + public bool TryGetValue(string key, out JToken value) + { + if (_dictionary == null) + { + value = null; + return false; + } + + return _dictionary.TryGetValue(key, out value); + } + + public ICollection Keys + { + get + { + EnsureDictionary(); + return _dictionary.Keys; + } + } + + public ICollection Values + { + get + { + EnsureDictionary(); + return _dictionary.Values; + } + } + } +} \ No newline at end of file diff --git a/Src/Newtonsoft.Json/Linq/JRaw.cs b/Src/Newtonsoft.Json/Linq/JRaw.cs index 7e24713e..339b7dda 100644 --- a/Src/Newtonsoft.Json/Linq/JRaw.cs +++ b/Src/Newtonsoft.Json/Linq/JRaw.cs @@ -1,9 +1,5 @@ -using System; -using System.Collections.Generic; -using System.Globalization; +using System.Globalization; using System.IO; -using System.Linq; -using System.Text; namespace Newtonsoft.Json.Linq { diff --git a/Src/Newtonsoft.Json/Linq/JToken.cs b/Src/Newtonsoft.Json/Linq/JToken.cs index 35d94ab0..47c9c941 100644 --- a/Src/Newtonsoft.Json/Linq/JToken.cs +++ b/Src/Newtonsoft.Json/Linq/JToken.cs @@ -29,13 +29,16 @@ using System.Collections.Generic; using System.Dynamic; using System.Linq.Expressions; #endif -using System.Linq; using System.IO; using Newtonsoft.Json.Utilities; using System.Diagnostics; using System.Globalization; using System.Collections; -using System.ComponentModel; +#if NET20 +using Newtonsoft.Json.Utilities.LinqBridge; +#else +using System.Linq; +#endif namespace Newtonsoft.Json.Linq { diff --git a/Src/Newtonsoft.Json/Linq/JTokenEqualityComparer.cs b/Src/Newtonsoft.Json/Linq/JTokenEqualityComparer.cs index cedb573f..9b0b1649 100644 --- a/Src/Newtonsoft.Json/Linq/JTokenEqualityComparer.cs +++ b/Src/Newtonsoft.Json/Linq/JTokenEqualityComparer.cs @@ -1,7 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; +using System.Collections.Generic; namespace Newtonsoft.Json.Linq { diff --git a/Src/Newtonsoft.Json/Linq/JTokenReader.cs b/Src/Newtonsoft.Json/Linq/JTokenReader.cs index 32c6bf14..d2dcc364 100644 --- a/Src/Newtonsoft.Json/Linq/JTokenReader.cs +++ b/Src/Newtonsoft.Json/Linq/JTokenReader.cs @@ -1,9 +1,5 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; using Newtonsoft.Json.Utilities; -using System.Globalization; namespace Newtonsoft.Json.Linq { diff --git a/Src/Newtonsoft.Json/Linq/JTokenWriter.cs b/Src/Newtonsoft.Json/Linq/JTokenWriter.cs index 6e5bcf8e..1297e465 100644 --- a/Src/Newtonsoft.Json/Linq/JTokenWriter.cs +++ b/Src/Newtonsoft.Json/Linq/JTokenWriter.cs @@ -1,8 +1,5 @@ using System; -using System.Collections.Generic; using System.Globalization; -using System.Linq; -using System.Text; using Newtonsoft.Json.Utilities; namespace Newtonsoft.Json.Linq diff --git a/Src/Newtonsoft.Json/Linq/JValue.cs b/Src/Newtonsoft.Json/Linq/JValue.cs index 11d76747..19a6859c 100644 --- a/Src/Newtonsoft.Json/Linq/JValue.cs +++ b/Src/Newtonsoft.Json/Linq/JValue.cs @@ -25,11 +25,8 @@ using System; using System.Collections.Generic; -using System.Linq; -using System.Text; using Newtonsoft.Json.Utilities; using System.Globalization; -using System.ComponentModel; #if !(NET35 || NET20 || WINDOWS_PHONE) using System.Dynamic; using System.Linq.Expressions; diff --git a/Src/Newtonsoft.Json/MemberSerialization.cs b/Src/Newtonsoft.Json/MemberSerialization.cs index 606ae81b..59bd2277 100644 --- a/Src/Newtonsoft.Json/MemberSerialization.cs +++ b/Src/Newtonsoft.Json/MemberSerialization.cs @@ -23,11 +23,6 @@ // OTHER DEALINGS IN THE SOFTWARE. #endregion -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - namespace Newtonsoft.Json { /// diff --git a/Src/Newtonsoft.Json/Newtonsoft.Json.Net20.csproj b/Src/Newtonsoft.Json/Newtonsoft.Json.Net20.csproj index 36b4d2b7..ec54083c 100644 --- a/Src/Newtonsoft.Json/Newtonsoft.Json.Net20.csproj +++ b/Src/Newtonsoft.Json/Newtonsoft.Json.Net20.csproj @@ -98,6 +98,7 @@ + diff --git a/Src/Newtonsoft.Json/Newtonsoft.Json.Net35.csproj b/Src/Newtonsoft.Json/Newtonsoft.Json.Net35.csproj index 4d76b440..bfd00cb5 100644 --- a/Src/Newtonsoft.Json/Newtonsoft.Json.Net35.csproj +++ b/Src/Newtonsoft.Json/Newtonsoft.Json.Net35.csproj @@ -111,6 +111,7 @@ + diff --git a/Src/Newtonsoft.Json/Newtonsoft.Json.Silverlight.csproj b/Src/Newtonsoft.Json/Newtonsoft.Json.Silverlight.csproj index 3dae2a83..323e2f0b 100644 --- a/Src/Newtonsoft.Json/Newtonsoft.Json.Silverlight.csproj +++ b/Src/Newtonsoft.Json/Newtonsoft.Json.Silverlight.csproj @@ -141,6 +141,7 @@ + diff --git a/Src/Newtonsoft.Json/Newtonsoft.Json.WindowsPhone.csproj b/Src/Newtonsoft.Json/Newtonsoft.Json.WindowsPhone.csproj index e5380222..fd8a131e 100644 --- a/Src/Newtonsoft.Json/Newtonsoft.Json.WindowsPhone.csproj +++ b/Src/Newtonsoft.Json/Newtonsoft.Json.WindowsPhone.csproj @@ -114,6 +114,7 @@ + diff --git a/Src/Newtonsoft.Json/Newtonsoft.Json.csproj b/Src/Newtonsoft.Json/Newtonsoft.Json.csproj index 5fb317e3..cb672daa 100644 --- a/Src/Newtonsoft.Json/Newtonsoft.Json.csproj +++ b/Src/Newtonsoft.Json/Newtonsoft.Json.csproj @@ -108,6 +108,7 @@ + diff --git a/Src/Newtonsoft.Json/NullValueHandling.cs b/Src/Newtonsoft.Json/NullValueHandling.cs index 8a5d966a..c7a95895 100644 --- a/Src/Newtonsoft.Json/NullValueHandling.cs +++ b/Src/Newtonsoft.Json/NullValueHandling.cs @@ -23,11 +23,6 @@ // OTHER DEALINGS IN THE SOFTWARE. #endregion -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - namespace Newtonsoft.Json { /// diff --git a/Src/Newtonsoft.Json/ObjectCreationHandling.cs b/Src/Newtonsoft.Json/ObjectCreationHandling.cs index c2e9d6e1..540fdc8a 100644 --- a/Src/Newtonsoft.Json/ObjectCreationHandling.cs +++ b/Src/Newtonsoft.Json/ObjectCreationHandling.cs @@ -23,11 +23,6 @@ // OTHER DEALINGS IN THE SOFTWARE. #endregion -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - namespace Newtonsoft.Json { /// diff --git a/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs b/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs index b9f40df1..47b1f423 100644 --- a/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs +++ b/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs @@ -85,7 +85,7 @@ using System.Security; // by using the '*' as shown below: [assembly: AssemblyVersion("4.0.8.0")] #if !PocketPC -[assembly: AssemblyFileVersion("4.0.8.14715")] +[assembly: AssemblyFileVersion("4.0.8.14718")] #endif [assembly: CLSCompliant(true)] diff --git a/Src/Newtonsoft.Json/Schema/Extensions.cs b/Src/Newtonsoft.Json/Schema/Extensions.cs index a93e8f28..c2534ae8 100644 --- a/Src/Newtonsoft.Json/Schema/Extensions.cs +++ b/Src/Newtonsoft.Json/Schema/Extensions.cs @@ -23,10 +23,7 @@ // OTHER DEALINGS IN THE SOFTWARE. #endregion -using System; using System.Collections.Generic; -using System.Linq; -using System.Text; using Newtonsoft.Json.Linq; using Newtonsoft.Json.Utilities; diff --git a/Src/Newtonsoft.Json/Schema/JsonSchema.cs b/Src/Newtonsoft.Json/Schema/JsonSchema.cs index 5eeb88b5..0f347806 100644 --- a/Src/Newtonsoft.Json/Schema/JsonSchema.cs +++ b/Src/Newtonsoft.Json/Schema/JsonSchema.cs @@ -24,10 +24,7 @@ #endregion using System; -using System.Collections; using System.Collections.Generic; -using System.Linq; -using System.Text; using System.IO; using Newtonsoft.Json.Linq; using Newtonsoft.Json.Utilities; diff --git a/Src/Newtonsoft.Json/Schema/JsonSchemaBuilder.cs b/Src/Newtonsoft.Json/Schema/JsonSchemaBuilder.cs index 7a140d6e..7b9cc8c6 100644 --- a/Src/Newtonsoft.Json/Schema/JsonSchemaBuilder.cs +++ b/Src/Newtonsoft.Json/Schema/JsonSchemaBuilder.cs @@ -25,8 +25,11 @@ using System; using System.Collections.Generic; +#if NET20 +using Newtonsoft.Json.Utilities.LinqBridge; +#else using System.Linq; -using System.Text; +#endif using System.Globalization; using Newtonsoft.Json.Utilities; using Newtonsoft.Json.Linq; @@ -428,4 +431,4 @@ namespace Newtonsoft.Json.Schema return JsonSchemaConstants.JsonSchemaTypeMapping.Single(kv => kv.Value == type).Key; } } -} +} \ No newline at end of file diff --git a/Src/Newtonsoft.Json/Schema/JsonSchemaConstants.cs b/Src/Newtonsoft.Json/Schema/JsonSchemaConstants.cs index 0e9eda1e..79fee1e7 100644 --- a/Src/Newtonsoft.Json/Schema/JsonSchemaConstants.cs +++ b/Src/Newtonsoft.Json/Schema/JsonSchemaConstants.cs @@ -23,10 +23,7 @@ // OTHER DEALINGS IN THE SOFTWARE. #endregion -using System; using System.Collections.Generic; -using System.Linq; -using System.Text; namespace Newtonsoft.Json.Schema { @@ -68,7 +65,7 @@ namespace Newtonsoft.Json.Schema public const string ReferencePropertyName = "$ref"; - public static readonly IDictionary JsonSchemaTypeMapping = new Dictionary() + public static readonly IDictionary JsonSchemaTypeMapping = new Dictionary { {"string", JsonSchemaType.String}, {"object", JsonSchemaType.Object}, diff --git a/Src/Newtonsoft.Json/Schema/JsonSchemaGenerator.cs b/Src/Newtonsoft.Json/Schema/JsonSchemaGenerator.cs index 6f623e51..181871b1 100644 --- a/Src/Newtonsoft.Json/Schema/JsonSchemaGenerator.cs +++ b/Src/Newtonsoft.Json/Schema/JsonSchemaGenerator.cs @@ -24,17 +24,20 @@ #endregion using System; -using System.Linq; using System.Globalization; using System.ComponentModel; using System.Collections.Generic; using Newtonsoft.Json.Linq; using Newtonsoft.Json.Utilities; using Newtonsoft.Json.Serialization; -using System.Reflection; #if NETFX_CORE using IConvertible = Newtonsoft.Json.Utilities.Convertible; #endif +#if NET20 +using Newtonsoft.Json.Utilities.LinqBridge; +#else +using System.Linq; +#endif namespace Newtonsoft.Json.Schema { diff --git a/Src/Newtonsoft.Json/Schema/JsonSchemaModel.cs b/Src/Newtonsoft.Json/Schema/JsonSchemaModel.cs index 0d438b4b..17b65db1 100644 --- a/Src/Newtonsoft.Json/Schema/JsonSchemaModel.cs +++ b/Src/Newtonsoft.Json/Schema/JsonSchemaModel.cs @@ -23,9 +23,7 @@ // OTHER DEALINGS IN THE SOFTWARE. #endregion -using System; using System.Collections.Generic; -using System.Linq; using Newtonsoft.Json.Linq; using Newtonsoft.Json.Utilities; diff --git a/Src/Newtonsoft.Json/Schema/JsonSchemaModelBuilder.cs b/Src/Newtonsoft.Json/Schema/JsonSchemaModelBuilder.cs index f126b9aa..636e8be7 100644 --- a/Src/Newtonsoft.Json/Schema/JsonSchemaModelBuilder.cs +++ b/Src/Newtonsoft.Json/Schema/JsonSchemaModelBuilder.cs @@ -23,10 +23,12 @@ // OTHER DEALINGS IN THE SOFTWARE. #endregion -using System; using System.Collections.Generic; +#if NET20 +using Newtonsoft.Json.Utilities.LinqBridge; +#else using System.Linq; -using System.Text; +#endif namespace Newtonsoft.Json.Schema { diff --git a/Src/Newtonsoft.Json/Schema/JsonSchemaNode.cs b/Src/Newtonsoft.Json/Schema/JsonSchemaNode.cs index 5f1a23c9..30b576d9 100644 --- a/Src/Newtonsoft.Json/Schema/JsonSchemaNode.cs +++ b/Src/Newtonsoft.Json/Schema/JsonSchemaNode.cs @@ -26,7 +26,11 @@ using System; using System.Collections.Generic; using System.Collections.ObjectModel; +#if NET20 +using Newtonsoft.Json.Utilities.LinqBridge; +#else using System.Linq; +#endif namespace Newtonsoft.Json.Schema { diff --git a/Src/Newtonsoft.Json/Schema/JsonSchemaResolver.cs b/Src/Newtonsoft.Json/Schema/JsonSchemaResolver.cs index d7c514f2..40952ad5 100644 --- a/Src/Newtonsoft.Json/Schema/JsonSchemaResolver.cs +++ b/Src/Newtonsoft.Json/Schema/JsonSchemaResolver.cs @@ -23,13 +23,12 @@ // OTHER DEALINGS IN THE SOFTWARE. #endregion -using System; using System.Collections.Generic; +#if NET20 +using Newtonsoft.Json.Utilities.LinqBridge; +#else using System.Linq; -using System.Net; -using System.Text; -using System.Globalization; -using Newtonsoft.Json.Utilities; +#endif namespace Newtonsoft.Json.Schema { diff --git a/Src/Newtonsoft.Json/Schema/JsonSchemaType.cs b/Src/Newtonsoft.Json/Schema/JsonSchemaType.cs index bf7c1fbe..e80aef57 100644 --- a/Src/Newtonsoft.Json/Schema/JsonSchemaType.cs +++ b/Src/Newtonsoft.Json/Schema/JsonSchemaType.cs @@ -24,9 +24,6 @@ #endregion using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; namespace Newtonsoft.Json.Schema { diff --git a/Src/Newtonsoft.Json/Schema/JsonSchemaWriter.cs b/Src/Newtonsoft.Json/Schema/JsonSchemaWriter.cs index eeaa0dbd..38d0b82a 100644 --- a/Src/Newtonsoft.Json/Schema/JsonSchemaWriter.cs +++ b/Src/Newtonsoft.Json/Schema/JsonSchemaWriter.cs @@ -23,12 +23,14 @@ // OTHER DEALINGS IN THE SOFTWARE. #endregion -using System; using System.Collections.Generic; -using System.Linq; -using System.Text; using Newtonsoft.Json.Linq; using Newtonsoft.Json.Utilities; +#if NET20 +using Newtonsoft.Json.Utilities.LinqBridge; +#else +using System.Linq; +#endif namespace Newtonsoft.Json.Schema { diff --git a/Src/Newtonsoft.Json/Serialization/CachedAttributeGetter.cs b/Src/Newtonsoft.Json/Serialization/CachedAttributeGetter.cs index 255f7482..6aca84f0 100644 --- a/Src/Newtonsoft.Json/Serialization/CachedAttributeGetter.cs +++ b/Src/Newtonsoft.Json/Serialization/CachedAttributeGetter.cs @@ -24,10 +24,7 @@ #endregion using System; -using System.Collections.Generic; -using System.Linq; using System.Reflection; -using System.Text; using Newtonsoft.Json.Utilities; #if NETFX_CORE using ICustomAttributeProvider = Newtonsoft.Json.Utilities.CustomAttributeProvider; diff --git a/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs b/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs index ce4ea622..6199e992 100644 --- a/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs +++ b/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs @@ -34,7 +34,6 @@ using System.ComponentModel; using System.Dynamic; #endif using System.Globalization; -using System.Linq; using System.Reflection; using System.Runtime.Serialization; #if !NETFX_CORE @@ -48,6 +47,11 @@ using System.Runtime.CompilerServices; #if NETFX_CORE using ICustomAttributeProvider = Newtonsoft.Json.Utilities.CustomAttributeProvider; #endif +#if NET20 +using Newtonsoft.Json.Utilities.LinqBridge; +#else +using System.Linq; +#endif namespace Newtonsoft.Json.Serialization { @@ -147,6 +151,16 @@ namespace Newtonsoft.Json.Serialization /// public bool SerializeCompilerGeneratedMembers { get; set; } +#if !SILVERLIGHT && !PocketPC && !NETFX_CORE + /// + /// Gets or sets a value indicating whether to ignore the ISerializable interface when serializing and deserializing types. + /// + /// + /// true if the ISerializable interface will be ignored when serializing and deserializing types; otherwise, false. + /// + public bool IgnoreSerializableInterface { get; set; } +#endif + /// /// Initializes a new instance of the class. /// @@ -686,7 +700,7 @@ namespace Newtonsoft.Json.Serialization return CreateStringContract(objectType); #if !SILVERLIGHT && !PocketPC && !NETFX_CORE - if (typeof(ISerializable).IsAssignableFrom(t)) + if (!IgnoreSerializableInterface && typeof(ISerializable).IsAssignableFrom(t)) return CreateISerializableContract(objectType); #endif diff --git a/Src/Newtonsoft.Json/Serialization/DefaultReferenceResolver.cs b/Src/Newtonsoft.Json/Serialization/DefaultReferenceResolver.cs index b1de3f72..3d9b95b3 100644 --- a/Src/Newtonsoft.Json/Serialization/DefaultReferenceResolver.cs +++ b/Src/Newtonsoft.Json/Serialization/DefaultReferenceResolver.cs @@ -24,10 +24,6 @@ #endregion using System; -using System.Collections.Generic; -using System.Linq; -using System.Runtime.CompilerServices; -using System.Text; using Newtonsoft.Json.Utilities; using System.Globalization; diff --git a/Src/Newtonsoft.Json/Serialization/DynamicValueProvider.cs b/Src/Newtonsoft.Json/Serialization/DynamicValueProvider.cs index ec302bbb..1e464d35 100644 --- a/Src/Newtonsoft.Json/Serialization/DynamicValueProvider.cs +++ b/Src/Newtonsoft.Json/Serialization/DynamicValueProvider.cs @@ -26,7 +26,9 @@ #if !PocketPC && !SILVERLIGHT using System; using System.Collections.Generic; -using System.Linq; +#if NET20 +using Newtonsoft.Json.Utilities.LinqBridge; +#endif using System.Text; using System.Reflection; using Newtonsoft.Json.Utilities; diff --git a/Src/Newtonsoft.Json/Serialization/ErrorContext.cs b/Src/Newtonsoft.Json/Serialization/ErrorContext.cs index 75cc87b2..c7bfff0f 100644 --- a/Src/Newtonsoft.Json/Serialization/ErrorContext.cs +++ b/Src/Newtonsoft.Json/Serialization/ErrorContext.cs @@ -24,9 +24,6 @@ #endregion using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; namespace Newtonsoft.Json.Serialization { diff --git a/Src/Newtonsoft.Json/Serialization/ErrorEventArgs.cs b/Src/Newtonsoft.Json/Serialization/ErrorEventArgs.cs index f4c7c5b8..5886a03b 100644 --- a/Src/Newtonsoft.Json/Serialization/ErrorEventArgs.cs +++ b/Src/Newtonsoft.Json/Serialization/ErrorEventArgs.cs @@ -1,7 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; namespace Newtonsoft.Json.Serialization { diff --git a/Src/Newtonsoft.Json/Serialization/IContractResolver.cs b/Src/Newtonsoft.Json/Serialization/IContractResolver.cs index 75459226..4c201924 100644 --- a/Src/Newtonsoft.Json/Serialization/IContractResolver.cs +++ b/Src/Newtonsoft.Json/Serialization/IContractResolver.cs @@ -24,9 +24,6 @@ #endregion using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; namespace Newtonsoft.Json.Serialization { diff --git a/Src/Newtonsoft.Json/Serialization/JsonArrayContract.cs b/Src/Newtonsoft.Json/Serialization/JsonArrayContract.cs index 234cca30..8582a3bf 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonArrayContract.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonArrayContract.cs @@ -25,10 +25,14 @@ using System; using System.Collections.Generic; -using System.Linq; using System.Reflection; using Newtonsoft.Json.Utilities; using System.Collections; +#if NET20 +using Newtonsoft.Json.Utilities.LinqBridge; +#else +using System.Linq; +#endif namespace Newtonsoft.Json.Serialization { diff --git a/Src/Newtonsoft.Json/Serialization/JsonDictionaryContract.cs b/Src/Newtonsoft.Json/Serialization/JsonDictionaryContract.cs index bf36008c..762cbdb3 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonDictionaryContract.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonDictionaryContract.cs @@ -28,6 +28,9 @@ using System.Collections.Generic; using System.Reflection; using Newtonsoft.Json.Utilities; using System.Collections; +#if NET20 +using Newtonsoft.Json.Utilities.LinqBridge; +#endif namespace Newtonsoft.Json.Serialization { diff --git a/Src/Newtonsoft.Json/Serialization/JsonFormatterConverter.cs b/Src/Newtonsoft.Json/Serialization/JsonFormatterConverter.cs index 49f19b47..a2fee70d 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonFormatterConverter.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonFormatterConverter.cs @@ -25,11 +25,8 @@ #if !(SILVERLIGHT || NETFX_CORE) using System; -using System.Collections.Generic; using System.Globalization; -using System.Linq; using System.Runtime.Serialization; -using System.Text; using Newtonsoft.Json.Utilities; using Newtonsoft.Json.Linq; diff --git a/Src/Newtonsoft.Json/Serialization/JsonISerializableContract.cs b/Src/Newtonsoft.Json/Serialization/JsonISerializableContract.cs index b8b9a119..403968ae 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonISerializableContract.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonISerializableContract.cs @@ -25,10 +25,6 @@ #if !SILVERLIGHT && !PocketPC && !NETFX_CORE using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using Newtonsoft.Json.Utilities; namespace Newtonsoft.Json.Serialization { diff --git a/Src/Newtonsoft.Json/Serialization/JsonLinqContract.cs b/Src/Newtonsoft.Json/Serialization/JsonLinqContract.cs index 1f100884..e44aba14 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonLinqContract.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonLinqContract.cs @@ -1,7 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; namespace Newtonsoft.Json.Serialization { diff --git a/Src/Newtonsoft.Json/Serialization/JsonProperty.cs b/Src/Newtonsoft.Json/Serialization/JsonProperty.cs index f2240eca..4f0952a6 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonProperty.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonProperty.cs @@ -24,6 +24,9 @@ #endregion using System; +#if NET20 +using Newtonsoft.Json.Utilities.LinqBridge; +#endif namespace Newtonsoft.Json.Serialization { diff --git a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs index 7684298d..e9f451e8 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs @@ -31,11 +31,15 @@ using System.Collections.ObjectModel; using System.Dynamic; #endif using System.Globalization; -using System.Linq; using System.Reflection; using System.Runtime.Serialization; using Newtonsoft.Json.Linq; using Newtonsoft.Json.Utilities; +#if NET20 +using Newtonsoft.Json.Utilities.LinqBridge; +#else +using System.Linq; +#endif namespace Newtonsoft.Json.Serialization { @@ -825,6 +829,12 @@ To force JSON arrays to deserialize add the JsonArrayAttribute to the type.".For { Type objectType = contract.UnderlyingType; + if (!JsonTypeReflector.FullyTrusted) + { + throw new JsonSerializationException(@"Type '{0}' implements ISerializable but cannot be deserialized using the ISerializable interface because the current application is not fully trusted and ISerializable can expose secure data. +To fix this error either change the environment to be fully trusted, change the application to not deserialize the type, add to JsonObjectAttribute to the type or change the JsonSerializer setting ContractResolver to use a new DefaultContractResolver with IgnoreSerializableInterface set to true.".FormatWith(CultureInfo.InvariantCulture, objectType)); + } + SerializationInfo serializationInfo = new SerializationInfo(contract.UnderlyingType, GetFormatterConverter()); bool exit = false; diff --git a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalWriter.cs b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalWriter.cs index 03705d73..77e6142a 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalWriter.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalWriter.cs @@ -31,13 +31,15 @@ using System.ComponentModel; using System.Dynamic; #endif using System.Globalization; -using System.Linq; -using System.Reflection; -using System.Runtime.Serialization.Formatters; +using System.Security; using Newtonsoft.Json.Linq; using Newtonsoft.Json.Utilities; using System.Runtime.Serialization; -using System.Security; +#if NET20 +using Newtonsoft.Json.Utilities.LinqBridge; +#else +using System.Linq; +#endif namespace Newtonsoft.Json.Serialization { @@ -469,6 +471,12 @@ namespace Newtonsoft.Json.Serialization #endif private void SerializeISerializable(JsonWriter writer, ISerializable value, JsonISerializableContract contract, JsonProperty member, JsonContract collectionValueContract) { + if (!JsonTypeReflector.FullyTrusted) + { + throw new JsonSerializationException(@"Type '{0}' implements ISerializable but cannot be serialized using the ISerializable interface because the current application is not fully trusted and ISerializable can expose secure data. +To fix this error either change the environment to be fully trusted, change the application to not deserialize the type, add to JsonObjectAttribute to the type or change the JsonSerializer setting ContractResolver to use a new DefaultContractResolver with IgnoreSerializableInterface set to true.".FormatWith(CultureInfo.InvariantCulture, value.GetType())); + } + contract.InvokeOnSerializing(value, Serializer.Context); _serializeStack.Add(value); diff --git a/Src/Newtonsoft.Json/Serialization/JsonStringContract.cs b/Src/Newtonsoft.Json/Serialization/JsonStringContract.cs index b76925ec..370828b2 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonStringContract.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonStringContract.cs @@ -24,9 +24,6 @@ #endregion using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; namespace Newtonsoft.Json.Serialization { diff --git a/Src/Newtonsoft.Json/Serialization/JsonTypeReflector.cs b/Src/Newtonsoft.Json/Serialization/JsonTypeReflector.cs index 12e8ea59..180ce080 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonTypeReflector.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonTypeReflector.cs @@ -26,16 +26,20 @@ using System; using System.ComponentModel; using System.Globalization; -using System.Linq; using System.Reflection; -using System.Runtime.Serialization; #if !NETFX_CORE +using System.Runtime.Serialization; using System.Security.Permissions; #endif using Newtonsoft.Json.Utilities; #if NETFX_CORE using ICustomAttributeProvider = Newtonsoft.Json.Utilities.CustomAttributeProvider; #endif +#if NET20 +using Newtonsoft.Json.Utilities.LinqBridge; +#else +using System.Linq; +#endif namespace Newtonsoft.Json.Serialization { @@ -350,6 +354,19 @@ namespace Newtonsoft.Json.Serialization } private static bool? _dynamicCodeGeneration; + private static bool? _fullyTrusted; + +#if DEBUG + internal static void SetFullyTrusted(bool fullyTrusted) + { + _fullyTrusted = fullyTrusted; + } + + internal static void SetDynamicCodeGeneration(bool dynamicCodeGeneration) + { + _dynamicCodeGeneration = dynamicCodeGeneration; + } +#endif public static bool DynamicCodeGeneration { @@ -380,6 +397,25 @@ namespace Newtonsoft.Json.Serialization } } + public static bool FullyTrusted + { + get + { + if (_fullyTrusted == null) + { +#if !(NET20 || NET35 || SILVERLIGHT) + AppDomain appDomain = AppDomain.CurrentDomain; + + _fullyTrusted = appDomain.IsHomogenous && appDomain.IsFullyTrusted; +#else + _fullyTrusted = true; +#endif + } + + return _fullyTrusted.Value; + } + } + public static ReflectionDelegateFactory ReflectionDelegateFactory { get diff --git a/Src/Newtonsoft.Json/Serialization/ReflectionValueProvider.cs b/Src/Newtonsoft.Json/Serialization/ReflectionValueProvider.cs index 4013cf36..5b2b1dee 100644 --- a/Src/Newtonsoft.Json/Serialization/ReflectionValueProvider.cs +++ b/Src/Newtonsoft.Json/Serialization/ReflectionValueProvider.cs @@ -24,9 +24,6 @@ #endregion using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; using System.Reflection; using Newtonsoft.Json.Utilities; using System.Globalization; diff --git a/Src/Newtonsoft.Json/TypeNameHandling.cs b/Src/Newtonsoft.Json/TypeNameHandling.cs index 2a80c9ca..147af159 100644 --- a/Src/Newtonsoft.Json/TypeNameHandling.cs +++ b/Src/Newtonsoft.Json/TypeNameHandling.cs @@ -1,7 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; namespace Newtonsoft.Json { diff --git a/Src/Newtonsoft.Json/Utilities/BidirectionalDictionary.cs b/Src/Newtonsoft.Json/Utilities/BidirectionalDictionary.cs index 1a949fe0..f681c25b 100644 --- a/Src/Newtonsoft.Json/Utilities/BidirectionalDictionary.cs +++ b/Src/Newtonsoft.Json/Utilities/BidirectionalDictionary.cs @@ -25,8 +25,6 @@ using System; using System.Collections.Generic; -using System.Linq; -using System.Text; namespace Newtonsoft.Json.Utilities { diff --git a/Src/Newtonsoft.Json/Utilities/CollectionUtils.cs b/Src/Newtonsoft.Json/Utilities/CollectionUtils.cs index 197f03a5..772937db 100644 --- a/Src/Newtonsoft.Json/Utilities/CollectionUtils.cs +++ b/Src/Newtonsoft.Json/Utilities/CollectionUtils.cs @@ -29,8 +29,13 @@ using System.Collections.ObjectModel; using System.Reflection; using System.Text; using System.Collections; +#if NET20 +using Newtonsoft.Json.Utilities.LinqBridge; +#else using System.Linq; +#endif using System.Globalization; +using Newtonsoft.Json.Serialization; namespace Newtonsoft.Json.Utilities { diff --git a/Src/Newtonsoft.Json/Utilities/CollectionWrapper.cs b/Src/Newtonsoft.Json/Utilities/CollectionWrapper.cs index e6676371..53bca726 100644 --- a/Src/Newtonsoft.Json/Utilities/CollectionWrapper.cs +++ b/Src/Newtonsoft.Json/Utilities/CollectionWrapper.cs @@ -27,9 +27,12 @@ using System; using System.Collections; using System.Collections.Generic; using System.Threading; -using Newtonsoft.Json.Utilities; -using System.Linq; using System.Globalization; +#if NET20 +using Newtonsoft.Json.Utilities.LinqBridge; +#else +using System.Linq; +#endif namespace Newtonsoft.Json.Utilities { diff --git a/Src/Newtonsoft.Json/Utilities/ConvertUtils.cs b/Src/Newtonsoft.Json/Utilities/ConvertUtils.cs index 5f5a3478..7cd140eb 100644 --- a/Src/Newtonsoft.Json/Utilities/ConvertUtils.cs +++ b/Src/Newtonsoft.Json/Utilities/ConvertUtils.cs @@ -24,14 +24,13 @@ #endregion using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; using System.Globalization; using System.ComponentModel; using Newtonsoft.Json.Serialization; -using Newtonsoft.Json.Utilities; using System.Reflection; +#if NET20 +using Newtonsoft.Json.Utilities.LinqBridge; +#endif #if !(SILVERLIGHT || NETFX_CORE) using System.Data.SqlTypes; diff --git a/Src/Newtonsoft.Json/Utilities/DateTimeUtils.cs b/Src/Newtonsoft.Json/Utilities/DateTimeUtils.cs index 52345e4b..3a922c2e 100644 --- a/Src/Newtonsoft.Json/Utilities/DateTimeUtils.cs +++ b/Src/Newtonsoft.Json/Utilities/DateTimeUtils.cs @@ -1,7 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; using System.Xml; using System.Globalization; diff --git a/Src/Newtonsoft.Json/Utilities/DictionaryWrapper.cs b/Src/Newtonsoft.Json/Utilities/DictionaryWrapper.cs index 6d43f654..93acf1b0 100644 --- a/Src/Newtonsoft.Json/Utilities/DictionaryWrapper.cs +++ b/Src/Newtonsoft.Json/Utilities/DictionaryWrapper.cs @@ -1,9 +1,12 @@ using System; using System.Collections.Generic; -using System.Linq; -using System.Text; using System.Collections; using System.Threading; +#if NET20 +using Newtonsoft.Json.Utilities.LinqBridge; +#else +using System.Linq; +#endif namespace Newtonsoft.Json.Utilities { diff --git a/Src/Newtonsoft.Json/Utilities/DynamicReflectionDelegateFactory.cs b/Src/Newtonsoft.Json/Utilities/DynamicReflectionDelegateFactory.cs index e6b92702..7e84040a 100644 --- a/Src/Newtonsoft.Json/Utilities/DynamicReflectionDelegateFactory.cs +++ b/Src/Newtonsoft.Json/Utilities/DynamicReflectionDelegateFactory.cs @@ -26,10 +26,12 @@ #if !PocketPC && !SILVERLIGHT using System; using System.Collections.Generic; -using System.Linq; +#if NET20 +using Newtonsoft.Json.Utilities.LinqBridge; +#endif using System.Reflection; using System.Reflection.Emit; -using System.Text; +using Newtonsoft.Json.Serialization; using System.Globalization; namespace Newtonsoft.Json.Utilities diff --git a/Src/Newtonsoft.Json/Utilities/DynamicWrapper.cs b/Src/Newtonsoft.Json/Utilities/DynamicWrapper.cs index 5882bd6d..80c837f2 100644 --- a/Src/Newtonsoft.Json/Utilities/DynamicWrapper.cs +++ b/Src/Newtonsoft.Json/Utilities/DynamicWrapper.cs @@ -2,13 +2,15 @@ using System; using System.Collections.Generic; using System.IO; -using System.Linq; using System.Reflection; using System.Reflection.Emit; using System.Resources; -using System.Text; -using System.Threading; using System.Globalization; +#if NET20 +using Newtonsoft.Json.Utilities.LinqBridge; +#else +using System.Linq; +#endif namespace Newtonsoft.Json.Utilities { diff --git a/Src/Newtonsoft.Json/Utilities/EnumUtils.cs b/Src/Newtonsoft.Json/Utilities/EnumUtils.cs index 57e034f9..d5e82e2e 100644 --- a/Src/Newtonsoft.Json/Utilities/EnumUtils.cs +++ b/Src/Newtonsoft.Json/Utilities/EnumUtils.cs @@ -26,7 +26,11 @@ using System; using System.Collections.Generic; using System.Globalization; +#if NET20 +using Newtonsoft.Json.Utilities.LinqBridge; +#else using System.Linq; +#endif using System.Reflection; namespace Newtonsoft.Json.Utilities diff --git a/Src/Newtonsoft.Json/Utilities/EnumValue.cs b/Src/Newtonsoft.Json/Utilities/EnumValue.cs index 40355f2a..1c0f1c53 100644 --- a/Src/Newtonsoft.Json/Utilities/EnumValue.cs +++ b/Src/Newtonsoft.Json/Utilities/EnumValue.cs @@ -23,11 +23,6 @@ // OTHER DEALINGS IN THE SOFTWARE. #endregion -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - namespace Newtonsoft.Json.Utilities { internal class EnumValue where T : struct diff --git a/Src/Newtonsoft.Json/Utilities/EnumValues.cs b/Src/Newtonsoft.Json/Utilities/EnumValues.cs index ae385c20..b4252ed7 100644 --- a/Src/Newtonsoft.Json/Utilities/EnumValues.cs +++ b/Src/Newtonsoft.Json/Utilities/EnumValues.cs @@ -23,11 +23,7 @@ // OTHER DEALINGS IN THE SOFTWARE. #endregion -using System; -using System.Collections.Generic; using System.Collections.ObjectModel; -using System.Linq; -using System.Text; namespace Newtonsoft.Json.Utilities { diff --git a/Src/Newtonsoft.Json/Utilities/ILGeneratorExtensions.cs b/Src/Newtonsoft.Json/Utilities/ILGeneratorExtensions.cs index 1739d124..9806ecf0 100644 --- a/Src/Newtonsoft.Json/Utilities/ILGeneratorExtensions.cs +++ b/Src/Newtonsoft.Json/Utilities/ILGeneratorExtensions.cs @@ -25,10 +25,7 @@ #if !PocketPC && !SILVERLIGHT using System; -using System.Collections.Generic; -using System.Linq; using System.Reflection.Emit; -using System.Text; using System.Reflection; namespace Newtonsoft.Json.Utilities diff --git a/Src/Newtonsoft.Json/Utilities/LateBoundReflectionDelegateFactory.cs b/Src/Newtonsoft.Json/Utilities/LateBoundReflectionDelegateFactory.cs index f83e3160..653db4be 100644 --- a/Src/Newtonsoft.Json/Utilities/LateBoundReflectionDelegateFactory.cs +++ b/Src/Newtonsoft.Json/Utilities/LateBoundReflectionDelegateFactory.cs @@ -24,8 +24,11 @@ #endregion using System; -using System.Globalization; +using Newtonsoft.Json.Serialization; using System.Reflection; +#if NET20 +using Newtonsoft.Json.Utilities.LinqBridge; +#endif namespace Newtonsoft.Json.Utilities { diff --git a/Src/Newtonsoft.Json/Utilities/LinqBridge.cs b/Src/Newtonsoft.Json/Utilities/LinqBridge.cs index 21a3400c..25810c1b 100644 --- a/Src/Newtonsoft.Json/Utilities/LinqBridge.cs +++ b/Src/Newtonsoft.Json/Utilities/LinqBridge.cs @@ -1,7 +1,5 @@ #if NET20 -#define LINQBRIDGE_LIB - #region License, Terms and Author(s) // // LINQBridge @@ -29,3080 +27,2984 @@ // #endregion -// $Id: Enumerable.cs 240 2010-10-19 21:49:03Z azizatif $ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Diagnostics; +using Newtonsoft.Json.Serialization; -namespace System.Linq +namespace Newtonsoft.Json.Utilities.LinqBridge { - #region Imports - - using System; - using System.Collections; - using System.Collections.Generic; - using System.Diagnostics; - using LinqBridge; - - #endregion - + /// + /// Provides a set of static (Shared in Visual Basic) methods for + /// querying objects that implement . + /// + internal static partial class Enumerable + { /// - /// Provides a set of static (Shared in Visual Basic) methods for - /// querying objects that implement . + /// Returns the input typed as . /// - static partial class Enumerable + public static IEnumerable AsEnumerable(IEnumerable source) { - /// - /// Returns the input typed as . - /// - - public static IEnumerable AsEnumerable(IEnumerable source) - { - return source; - } - - /// - /// Returns an empty that has the - /// specified type argument. - /// - - public static IEnumerable Empty() - { - return Sequence.Empty; - } - - /// - /// Converts the elements of an to the - /// specified type. - /// - - public static IEnumerable Cast( - this IEnumerable source) - { - CheckNotNull(source, "source"); - - return CastYield(source); - } - - private static IEnumerable CastYield( - IEnumerable source) - { - foreach (var item in source) - yield return (TResult) item; - } - - /// - /// Filters the elements of an based on a specified type. - /// - - public static IEnumerable OfType( - this IEnumerable source) - { - CheckNotNull(source, "source"); - - return OfTypeYield(source); - } - - private static IEnumerable OfTypeYield( - IEnumerable source) - { - foreach (var item in source) - if (item is TResult) - yield return (TResult) item; - } - - /// - /// Generates a sequence of integral numbers within a specified range. - /// - /// The value of the first integer in the sequence. - /// The number of sequential integers to generate. - - public static IEnumerable Range(int start, int count) - { - if (count < 0) - throw new ArgumentOutOfRangeException("count", count, null); - - var end = (long) start + count; - if (end - 1 >= int.MaxValue) - throw new ArgumentOutOfRangeException("count", count, null); - - return RangeYield(start, end); - } - - private static IEnumerable RangeYield(int start, long end) - { - for (var i = start; i < end; i++) - yield return i; - } - - /// - /// Generates a sequence that contains one repeated value. - /// - - public static IEnumerable Repeat(TResult element, int count) - { - if (count < 0) throw new ArgumentOutOfRangeException("count", count, null); - - return RepeatYield(element, count); - } - - private static IEnumerable RepeatYield(TResult element, int count) - { - for (var i = 0; i < count; i++) - yield return element; - } - - /// - /// Filters a sequence of values based on a predicate. - /// - - public static IEnumerable Where( - this IEnumerable source, - Func predicate) - { - CheckNotNull(predicate, "predicate"); - - return source.Where((item, i) => predicate(item)); - } - - /// - /// Filters a sequence of values based on a predicate. - /// Each element's index is used in the logic of the predicate function. - /// - - public static IEnumerable Where( - this IEnumerable source, - Func predicate) - { - CheckNotNull(source, "source"); - CheckNotNull(predicate, "predicate"); - - return WhereYield(source, predicate); - } - - private static IEnumerable WhereYield( - IEnumerable source, - Func predicate) - { - var i = 0; - foreach (var item in source) - if (predicate(item, i++)) - yield return item; - } - - /// - /// Projects each element of a sequence into a new form. - /// - - public static IEnumerable Select( - this IEnumerable source, - Func selector) - { - CheckNotNull(selector, "selector"); - - return source.Select((item, i) => selector(item)); - } - - /// - /// Projects each element of a sequence into a new form by - /// incorporating the element's index. - /// - - public static IEnumerable Select( - this IEnumerable source, - Func selector) - { - CheckNotNull(source, "source"); - CheckNotNull(selector, "selector"); - - return SelectYield(source, selector); - } - - private static IEnumerable SelectYield( - IEnumerable source, - Func selector) - { - var i = 0; - foreach (var item in source) - yield return selector(item, i++); - } - - /// - /// Projects each element of a sequence to an - /// and flattens the resulting sequences into one sequence. - /// - - public static IEnumerable SelectMany( - this IEnumerable source, - Func> selector) - { - CheckNotNull(selector, "selector"); - - return source.SelectMany((item, i) => selector(item)); - } - - /// - /// Projects each element of a sequence to an , - /// and flattens the resulting sequences into one sequence. The - /// index of each source element is used in the projected form of - /// that element. - /// - - public static IEnumerable SelectMany( - this IEnumerable source, - Func> selector) - { - CheckNotNull(selector, "selector"); - - return source.SelectMany(selector, (item, subitem) => subitem); - } - - /// - /// Projects each element of a sequence to an , - /// flattens the resulting sequences into one sequence, and invokes - /// a result selector function on each element therein. - /// - - public static IEnumerable SelectMany( - this IEnumerable source, - Func> collectionSelector, - Func resultSelector) - { - CheckNotNull(collectionSelector, "collectionSelector"); - - return source.SelectMany((item, i) => collectionSelector(item), resultSelector); - } - - /// - /// Projects each element of a sequence to an , - /// flattens the resulting sequences into one sequence, and invokes - /// a result selector function on each element therein. The index of - /// each source element is used in the intermediate projected form - /// of that element. - /// - - public static IEnumerable SelectMany( - this IEnumerable source, - Func> collectionSelector, - Func resultSelector) - { - CheckNotNull(source, "source"); - CheckNotNull(collectionSelector, "collectionSelector"); - CheckNotNull(resultSelector, "resultSelector"); - - return SelectManyYield(source, collectionSelector, resultSelector); - } - - private static IEnumerable SelectManyYield( - this IEnumerable source, - Func> collectionSelector, - Func resultSelector) - { - var i = 0; - foreach (var item in source) - foreach (var subitem in collectionSelector(item, i++)) - yield return resultSelector(item, subitem); - } - - /// - /// Returns elements from a sequence as long as a specified condition is true. - /// - - public static IEnumerable TakeWhile( - this IEnumerable source, - Func predicate) - { - CheckNotNull(predicate, "predicate"); - - return source.TakeWhile((item, i) => predicate(item)); - } - - /// - /// Returns elements from a sequence as long as a specified condition is true. - /// The element's index is used in the logic of the predicate function. - /// - - public static IEnumerable TakeWhile( - this IEnumerable source, - Func predicate) - { - CheckNotNull(source, "source"); - CheckNotNull(predicate, "predicate"); - - return TakeWhileYield(source, predicate); - } - - private static IEnumerable TakeWhileYield( - this IEnumerable source, - Func predicate) - { - var i = 0; - foreach (var item in source) - if (predicate(item, i++)) - yield return item; - else - break; - } - - private static class Futures - { - public static readonly Func Default = () => default(T); - public static readonly Func Undefined = () => { throw new InvalidOperationException(); }; - } - - /// - /// Base implementation of First operator. - /// - - private static TSource FirstImpl( - this IEnumerable source, - Func empty) - { - CheckNotNull(source, "source"); - Debug.Assert(empty != null); - - var list = source as IList; // optimized case for lists - if (list != null) - return list.Count > 0 ? list[0] : empty(); - - using (var e = source.GetEnumerator()) // fallback for enumeration - return e.MoveNext() ? e.Current : empty(); - } - - /// - /// Returns the first element of a sequence. - /// - - public static TSource First( - this IEnumerable source) - { - return source.FirstImpl(Futures.Undefined); - } - - /// - /// Returns the first element in a sequence that satisfies a specified condition. - /// - - public static TSource First( - this IEnumerable source, - Func predicate) - { - return First(source.Where(predicate)); - } - - /// - /// Returns the first element of a sequence, or a default value if - /// the sequence contains no elements. - /// - - public static TSource FirstOrDefault( - this IEnumerable source) - { - return source.FirstImpl(Futures.Default); - } - - /// - /// Returns the first element of the sequence that satisfies a - /// condition or a default value if no such element is found. - /// - - public static TSource FirstOrDefault( - this IEnumerable source, - Func predicate) - { - return FirstOrDefault(source.Where(predicate)); - } - - /// - /// Base implementation of Last operator. - /// - - private static TSource LastImpl( - this IEnumerable source, - Func empty) - { - CheckNotNull(source, "source"); - - var list = source as IList; // optimized case for lists - if (list != null) - return list.Count > 0 ? list[list.Count - 1] : empty(); - - using (var e = source.GetEnumerator()) - { - if (!e.MoveNext()) - return empty(); - - var last = e.Current; - while (e.MoveNext()) - last = e.Current; - - return last; - } - } - - /// - /// Returns the last element of a sequence. - /// - public static TSource Last( - this IEnumerable source) - { - return source.LastImpl(Futures.Undefined); - } - - /// - /// Returns the last element of a sequence that satisfies a - /// specified condition. - /// - - public static TSource Last( - this IEnumerable source, - Func predicate) - { - return Last(source.Where(predicate)); - } - - /// - /// Returns the last element of a sequence, or a default value if - /// the sequence contains no elements. - /// - - public static TSource LastOrDefault( - this IEnumerable source) - { - return source.LastImpl(Futures.Default); - } - - /// - /// Returns the last element of a sequence that satisfies a - /// condition or a default value if no such element is found. - /// - - public static TSource LastOrDefault( - this IEnumerable source, - Func predicate) - { - return LastOrDefault(source.Where(predicate)); - } - - /// - /// Base implementation of Single operator. - /// - - private static TSource SingleImpl( - this IEnumerable source, - Func empty) - { - CheckNotNull(source, "source"); - - using (var e = source.GetEnumerator()) - { - if (e.MoveNext()) - { - var single = e.Current; - if (!e.MoveNext()) - return single; - - throw new InvalidOperationException(); - } - - return empty(); - } - } - - /// - /// Returns the only element of a sequence, and throws an exception - /// if there is not exactly one element in the sequence. - /// - - public static TSource Single( - this IEnumerable source) - { - return source.SingleImpl(Futures.Undefined); - } - - /// - /// Returns the only element of a sequence that satisfies a - /// specified condition, and throws an exception if more than one - /// such element exists. - /// - - public static TSource Single( - this IEnumerable source, - Func predicate) - { - return Single(source.Where(predicate)); - } - - /// - /// Returns the only element of a sequence, or a default value if - /// the sequence is empty; this method throws an exception if there - /// is more than one element in the sequence. - /// - - public static TSource SingleOrDefault( - this IEnumerable source) - { - return source.SingleImpl(Futures.Default); - } - - /// - /// Returns the only element of a sequence that satisfies a - /// specified condition or a default value if no such element - /// exists; this method throws an exception if more than one element - /// satisfies the condition. - /// - - public static TSource SingleOrDefault( - this IEnumerable source, - Func predicate) - { - return SingleOrDefault(source.Where(predicate)); - } - - /// - /// Returns the element at a specified index in a sequence. - /// - - public static TSource ElementAt( - this IEnumerable source, - int index) - { - CheckNotNull(source, "source"); - - if (index < 0) - throw new ArgumentOutOfRangeException("index", index, null); - - var list = source as IList; - if (list != null) - return list[index]; - - try - { - return source.SkipWhile((item, i) => i < index).First(); - } - catch (InvalidOperationException) // if thrown by First - { - throw new ArgumentOutOfRangeException("index", index, null); - } - } - - /// - /// Returns the element at a specified index in a sequence or a - /// default value if the index is out of range. - /// - - public static TSource ElementAtOrDefault( - this IEnumerable source, - int index) - { - CheckNotNull(source, "source"); - - if (index < 0) - return default(TSource); - - var list = source as IList; - if (list != null) - return index < list.Count ? list[index] : default(TSource); - - return source.SkipWhile((item, i) => i < index).FirstOrDefault(); - } - - /// - /// Inverts the order of the elements in a sequence. - /// - - public static IEnumerable Reverse( - this IEnumerable source) - { - CheckNotNull(source, "source"); - - return ReverseYield(source); - } - - private static IEnumerable ReverseYield(IEnumerable source) - { - var stack = new Stack(); - foreach (var item in source) - stack.Push(item); - - foreach (var item in stack) - yield return item; - } - - /// - /// Returns a specified number of contiguous elements from the start - /// of a sequence. - /// - - public static IEnumerable Take( - this IEnumerable source, - int count) - { - return source.Where((item, i) => i < count); - } - - /// - /// Bypasses a specified number of elements in a sequence and then - /// returns the remaining elements. - /// - - public static IEnumerable Skip( - this IEnumerable source, - int count) - { - return source.Where((item, i) => i >= count); - } - - /// - /// Bypasses elements in a sequence as long as a specified condition - /// is true and then returns the remaining elements. - /// - - public static IEnumerable SkipWhile( - this IEnumerable source, - Func predicate) - { - CheckNotNull(predicate, "predicate"); - - return source.SkipWhile((item, i) => predicate(item)); - } - - /// - /// Bypasses elements in a sequence as long as a specified condition - /// is true and then returns the remaining elements. The element's - /// index is used in the logic of the predicate function. - /// - - public static IEnumerable SkipWhile( - this IEnumerable source, - Func predicate) - { - CheckNotNull(source, "source"); - CheckNotNull(predicate, "predicate"); - - return SkipWhileYield(source, predicate); - } - - private static IEnumerable SkipWhileYield( - IEnumerable source, - Func predicate) - { - using (var e = source.GetEnumerator()) - { - for (var i = 0; ; i++) - { - if (!e.MoveNext()) - yield break; - - if (!predicate(e.Current, i)) - break; - } - - do { yield return e.Current; } while (e.MoveNext()); - } - } - - /// - /// Returns the number of elements in a sequence. - /// - - public static int Count( - this IEnumerable source) - { - CheckNotNull(source, "source"); - - var collection = source as ICollection; - return collection != null - ? collection.Count - : source.Aggregate(0, (count, item) => checked(count + 1)); - } - - /// - /// Returns a number that represents how many elements in the - /// specified sequence satisfy a condition. - /// - - public static int Count( - this IEnumerable source, - Func predicate) - { - return Count(source.Where(predicate)); - } - - /// - /// Returns an that represents the total number - /// of elements in a sequence. - /// - - public static long LongCount( - this IEnumerable source) - { - CheckNotNull(source, "source"); - - var array = source as Array; - return array != null - ? array.LongLength - : source.Aggregate(0L, (count, item) => count + 1); - } - - /// - /// Returns an that represents how many elements - /// in a sequence satisfy a condition. - /// - - public static long LongCount( - this IEnumerable source, - Func predicate) - { - return LongCount(source.Where(predicate)); - } - - /// - /// Concatenates two sequences. - /// - - public static IEnumerable Concat( - this IEnumerable first, - IEnumerable second) - { - CheckNotNull(first, "first"); - CheckNotNull(second, "second"); - - return ConcatYield(first, second); - } - - private static IEnumerable ConcatYield( - IEnumerable first, - IEnumerable second) - { - foreach (var item in first) - yield return item; - - foreach (var item in second) - yield return item; - } - - /// - /// Creates a from an . - /// - - public static List ToList( - this IEnumerable source) - { - CheckNotNull(source, "source"); - - return new List(source); - } - - /// - /// Creates an array from an . - /// - - public static TSource[] ToArray( - this IEnumerable source) - { - return source.ToList().ToArray(); - } - - /// - /// Returns distinct elements from a sequence by using the default - /// equality comparer to compare values. - /// - - public static IEnumerable Distinct( - this IEnumerable source) - { - return Distinct(source, /* comparer */ null); - } - - /// - /// Returns distinct elements from a sequence by using a specified - /// to compare values. - /// - - public static IEnumerable Distinct( - this IEnumerable source, - IEqualityComparer comparer) - { - CheckNotNull(source, "source"); - - return DistinctYield(source, comparer); - } - - private static IEnumerable DistinctYield( - IEnumerable source, - IEqualityComparer comparer) - { - var set = new Dictionary(comparer); - var gotNull = false; - - foreach (var item in source) - { - if (item == null) - { - if (gotNull) - continue; - gotNull = true; - } - else - { - if (set.ContainsKey(item)) - continue; - set.Add(item, null); - } - - yield return item; - } - } - - /// - /// Creates a from an - /// according to a specified key - /// selector function. - /// - - public static ILookup ToLookup( - this IEnumerable source, - Func keySelector) - { - return ToLookup(source, keySelector, e => e, /* comparer */ null); - } - - /// - /// Creates a from an - /// according to a specified key - /// selector function and a key comparer. - /// - - public static ILookup ToLookup( - this IEnumerable source, - Func keySelector, - IEqualityComparer comparer) - { - return ToLookup(source, keySelector, e => e, comparer); - } - - /// - /// Creates a from an - /// according to specified key - /// and element selector functions. - /// - - public static ILookup ToLookup( - this IEnumerable source, - Func keySelector, - Func elementSelector) - { - return ToLookup(source, keySelector, elementSelector, /* comparer */ null); - } - - /// - /// Creates a from an - /// according to a specified key - /// selector function, a comparer and an element selector function. - /// - - public static ILookup ToLookup( - this IEnumerable source, - Func keySelector, - Func elementSelector, - IEqualityComparer comparer) - { - CheckNotNull(source, "source"); - CheckNotNull(keySelector, "keySelector"); - CheckNotNull(elementSelector, "elementSelector"); - - var lookup = new Lookup(comparer); - - foreach (var item in source) - { - var key = keySelector(item); - - var grouping = (Grouping) lookup.Find(key); - if (grouping == null) - { - grouping = new Grouping(key); - lookup.Add(grouping); - } - - grouping.Add(elementSelector(item)); - } - - return lookup; - } - - /// - /// Groups the elements of a sequence according to a specified key - /// selector function. - /// - - public static IEnumerable> GroupBy( - this IEnumerable source, - Func keySelector) - { - return GroupBy(source, keySelector, /* comparer */ null); - } - - /// - /// Groups the elements of a sequence according to a specified key - /// selector function and compares the keys by using a specified - /// comparer. - /// - - public static IEnumerable> GroupBy( - this IEnumerable source, - Func keySelector, - IEqualityComparer comparer) - { - return GroupBy(source, keySelector, e => e, comparer); - } - - /// - /// Groups the elements of a sequence according to a specified key - /// selector function and projects the elements for each group by - /// using a specified function. - /// - - public static IEnumerable> GroupBy( - this IEnumerable source, - Func keySelector, - Func elementSelector) - { - return GroupBy(source, keySelector, elementSelector, /* comparer */ null); - } - - /// - /// Groups the elements of a sequence according to a specified key - /// selector function and creates a result value from each group and - /// its key. - /// - - public static IEnumerable> GroupBy( - this IEnumerable source, - Func keySelector, - Func elementSelector, - IEqualityComparer comparer) - { - CheckNotNull(source, "source"); - CheckNotNull(keySelector, "keySelector"); - CheckNotNull(elementSelector, "elementSelector"); - - return ToLookup(source, keySelector, elementSelector, comparer); - } - - /// - /// Groups the elements of a sequence according to a key selector - /// function. The keys are compared by using a comparer and each - /// group's elements are projected by using a specified function. - /// - - public static IEnumerable GroupBy( - this IEnumerable source, - Func keySelector, - Func, TResult> resultSelector) - { - return GroupBy(source, keySelector, resultSelector, /* comparer */ null); - } - - /// - /// Groups the elements of a sequence according to a specified key - /// selector function and creates a result value from each group and - /// its key. The elements of each group are projected by using a - /// specified function. - /// - - public static IEnumerable GroupBy( - this IEnumerable source, - Func keySelector, - Func, TResult> resultSelector, - IEqualityComparer comparer) - { - CheckNotNull(source, "source"); - CheckNotNull(keySelector, "keySelector"); - CheckNotNull(resultSelector, "resultSelector"); - - return ToLookup(source, keySelector, comparer).Select(g => resultSelector(g.Key, g)); - } - - /// - /// Groups the elements of a sequence according to a specified key - /// selector function and creates a result value from each group and - /// its key. The keys are compared by using a specified comparer. - /// - - public static IEnumerable GroupBy( - this IEnumerable source, - Func keySelector, - Func elementSelector, - Func, TResult> resultSelector) - { - return GroupBy(source, keySelector, elementSelector, resultSelector, /* comparer */ null); - } - - /// - /// Groups the elements of a sequence according to a specified key - /// selector function and creates a result value from each group and - /// its key. Key values are compared by using a specified comparer, - /// and the elements of each group are projected by using a - /// specified function. - /// - - public static IEnumerable GroupBy( - this IEnumerable source, - Func keySelector, - Func elementSelector, - Func, TResult> resultSelector, - IEqualityComparer comparer) - { - CheckNotNull(source, "source"); - CheckNotNull(keySelector, "keySelector"); - CheckNotNull(elementSelector, "elementSelector"); - CheckNotNull(resultSelector, "resultSelector"); - - return ToLookup(source, keySelector, elementSelector, comparer) - .Select(g => resultSelector(g.Key, g)); - } - - /// - /// Applies an accumulator function over a sequence. - /// - - public static TSource Aggregate( - this IEnumerable source, - Func func) - { - CheckNotNull(source, "source"); - CheckNotNull(func, "func"); - - using (var e = source.GetEnumerator()) - { - if (!e.MoveNext()) - throw new InvalidOperationException(); - - return e.Renumerable().Skip(1).Aggregate(e.Current, func); - } - } - - /// - /// Applies an accumulator function over a sequence. The specified - /// seed value is used as the initial accumulator value. - /// - - public static TAccumulate Aggregate( - this IEnumerable source, - TAccumulate seed, - Func func) - { - return Aggregate(source, seed, func, r => r); - } - - /// - /// Applies an accumulator function over a sequence. The specified - /// seed value is used as the initial accumulator value, and the - /// specified function is used to select the result value. - /// - - public static TResult Aggregate( - this IEnumerable source, - TAccumulate seed, - Func func, - Func resultSelector) - { - CheckNotNull(source, "source"); - CheckNotNull(func, "func"); - CheckNotNull(resultSelector, "resultSelector"); - - var result = seed; - - foreach (var item in source) - result = func(result, item); - - return resultSelector(result); - } - - /// - /// Produces the set union of two sequences by using the default - /// equality comparer. - /// - - public static IEnumerable Union( - this IEnumerable first, - IEnumerable second) - { - return Union(first, second, /* comparer */ null); - } - - /// - /// Produces the set union of two sequences by using a specified - /// . - /// - - public static IEnumerable Union( - this IEnumerable first, - IEnumerable second, - IEqualityComparer comparer) - { - return first.Concat(second).Distinct(comparer); - } - - /// - /// Returns the elements of the specified sequence or the type - /// parameter's default value in a singleton collection if the - /// sequence is empty. - /// - - public static IEnumerable DefaultIfEmpty( - this IEnumerable source) - { - return source.DefaultIfEmpty(default(TSource)); - } - - /// - /// Returns the elements of the specified sequence or the specified - /// value in a singleton collection if the sequence is empty. - /// - - public static IEnumerable DefaultIfEmpty( - this IEnumerable source, - TSource defaultValue) - { - CheckNotNull(source, "source"); - - return DefaultIfEmptyYield(source, defaultValue); - } - - private static IEnumerable DefaultIfEmptyYield( - IEnumerable source, - TSource defaultValue) - { - using (var e = source.GetEnumerator()) - { - if (!e.MoveNext()) - yield return defaultValue; - else - do { yield return e.Current; } while (e.MoveNext()); - } - } - - /// - /// Determines whether all elements of a sequence satisfy a condition. - /// - - public static bool All( - this IEnumerable source, - Func predicate) - { - CheckNotNull(source, "source"); - CheckNotNull(predicate, "predicate"); - - foreach (var item in source) - if (!predicate(item)) - return false; - - return true; - } - - /// - /// Determines whether a sequence contains any elements. - /// - - public static bool Any( - this IEnumerable source) - { - CheckNotNull(source, "source"); - - using (var e = source.GetEnumerator()) - return e.MoveNext(); - } - - /// - /// Determines whether any element of a sequence satisfies a - /// condition. - /// - - public static bool Any( - this IEnumerable source, - Func predicate) - { - return source.Where(predicate).Any(); - } - - /// - /// Determines whether a sequence contains a specified element by - /// using the default equality comparer. - /// - - public static bool Contains( - this IEnumerable source, - TSource value) - { - return source.Contains(value, /* comparer */ null); - } - - /// - /// Determines whether a sequence contains a specified element by - /// using a specified . - /// - - public static bool Contains( - this IEnumerable source, - TSource value, - IEqualityComparer comparer) - { - CheckNotNull(source, "source"); - - if (comparer == null) - { - var collection = source as ICollection; - if (collection != null) - return collection.Contains(value); - } - - comparer = comparer ?? EqualityComparer.Default; - return source.Any(item => comparer.Equals(item, value)); - } - - /// - /// Determines whether two sequences are equal by comparing the - /// elements by using the default equality comparer for their type. - /// - - public static bool SequenceEqual( - this IEnumerable first, - IEnumerable second) - { - return first.SequenceEqual(second, /* comparer */ null); - } - - /// - /// Determines whether two sequences are equal by comparing their - /// elements by using a specified . - /// - - public static bool SequenceEqual( - this IEnumerable first, - IEnumerable second, - IEqualityComparer comparer) - { - CheckNotNull(first, "frist"); - CheckNotNull(second, "second"); - - comparer = comparer ?? EqualityComparer.Default; - - using (IEnumerator lhs = first.GetEnumerator(), - rhs = second.GetEnumerator()) - { - do - { - if (!lhs.MoveNext()) - return !rhs.MoveNext(); - - if (!rhs.MoveNext()) - return false; - } - while (comparer.Equals(lhs.Current, rhs.Current)); - } - - return false; - } - - /// - /// Base implementation for Min/Max operator. - /// - - private static TSource MinMaxImpl( - this IEnumerable source, - Func lesser) - { - CheckNotNull(source, "source"); - Debug.Assert(lesser != null); - - return source.Aggregate((a, item) => lesser(a, item) ? a : item); - } - - /// - /// Base implementation for Min/Max operator for nullable types. - /// - - private static TSource? MinMaxImpl( - this IEnumerable source, - TSource? seed, Func lesser) where TSource : struct - { - CheckNotNull(source, "source"); - Debug.Assert(lesser != null); - - return source.Aggregate(seed, (a, item) => lesser(a, item) ? a : item); - // == MinMaxImpl(Repeat(null, 1).Concat(source), lesser); - } - - /// - /// Returns the minimum value in a generic sequence. - /// - - public static TSource Min( - this IEnumerable source) - { - var comparer = Comparer.Default; - return source.MinMaxImpl((x, y) => comparer.Compare(x, y) < 0); - } - - /// - /// Invokes a transform function on each element of a generic - /// sequence and returns the minimum resulting value. - /// - - public static TResult Min( - this IEnumerable source, - Func selector) - { - return source.Select(selector).Min(); - } - - /// - /// Returns the maximum value in a generic sequence. - /// - - public static TSource Max( - this IEnumerable source) - { - var comparer = Comparer.Default; - return source.MinMaxImpl((x, y) => comparer.Compare(x, y) > 0); - } - - /// - /// Invokes a transform function on each element of a generic - /// sequence and returns the maximum resulting value. - /// - - public static TResult Max( - this IEnumerable source, - Func selector) - { - return source.Select(selector).Max(); - } - - /// - /// Makes an enumerator seen as enumerable once more. - /// - /// - /// The supplied enumerator must have been started. The first element - /// returned is the element the enumerator was on when passed in. - /// DO NOT use this method if the caller must be a generator. It is - /// mostly safe among aggregate operations. - /// - - private static IEnumerable Renumerable(this IEnumerator e) - { - Debug.Assert(e != null); - - do { yield return e.Current; } while (e.MoveNext()); - } - - /// - /// Sorts the elements of a sequence in ascending order according to a key. - /// - - public static IOrderedEnumerable OrderBy( - this IEnumerable source, - Func keySelector) - { - return source.OrderBy(keySelector, /* comparer */ null); - } - - /// - /// Sorts the elements of a sequence in ascending order by using a - /// specified comparer. - /// - - public static IOrderedEnumerable OrderBy( - this IEnumerable source, - Func keySelector, - IComparer comparer) - { - CheckNotNull(source, "source"); - CheckNotNull(keySelector, "keySelector"); - - return new OrderedEnumerable(source, keySelector, comparer, /* descending */ false); - } - - /// - /// Sorts the elements of a sequence in descending order according to a key. - /// - - public static IOrderedEnumerable OrderByDescending( - this IEnumerable source, - Func keySelector) - { - return source.OrderByDescending(keySelector, /* comparer */ null); - } - - /// - /// Sorts the elements of a sequence in descending order by using a - /// specified comparer. - /// - - public static IOrderedEnumerable OrderByDescending( - this IEnumerable source, - Func keySelector, - IComparer comparer) - { - CheckNotNull(source, "source"); - CheckNotNull(source, "keySelector"); - - return new OrderedEnumerable(source, keySelector, comparer, /* descending */ true); - } - - /// - /// Performs a subsequent ordering of the elements in a sequence in - /// ascending order according to a key. - /// - - public static IOrderedEnumerable ThenBy( - this IOrderedEnumerable source, - Func keySelector) - { - return source.ThenBy(keySelector, /* comparer */ null); - } - - /// - /// Performs a subsequent ordering of the elements in a sequence in - /// ascending order by using a specified comparer. - /// - - public static IOrderedEnumerable ThenBy( - this IOrderedEnumerable source, - Func keySelector, - IComparer comparer) - { - CheckNotNull(source, "source"); - - return source.CreateOrderedEnumerable(keySelector, comparer, /* descending */ false); - } - - /// - /// Performs a subsequent ordering of the elements in a sequence in - /// descending order, according to a key. - /// - - public static IOrderedEnumerable ThenByDescending( - this IOrderedEnumerable source, - Func keySelector) - { - return source.ThenByDescending(keySelector, /* comparer */ null); - } - - /// - /// Performs a subsequent ordering of the elements in a sequence in - /// descending order by using a specified comparer. - /// - - public static IOrderedEnumerable ThenByDescending( - this IOrderedEnumerable source, - Func keySelector, - IComparer comparer) - { - CheckNotNull(source, "source"); - - return source.CreateOrderedEnumerable(keySelector, comparer, /* descending */ true); - } - - /// - /// Base implementation for Intersect and Except operators. - /// - - private static IEnumerable IntersectExceptImpl( - this IEnumerable first, - IEnumerable second, - IEqualityComparer comparer, - bool flag) - { - CheckNotNull(first, "first"); - CheckNotNull(second, "second"); - - var keys = new List(); - var flags = new Dictionary(comparer); - - foreach (var item in first.Where(k => !flags.ContainsKey(k))) - { - flags.Add(item, !flag); - keys.Add(item); - } - - foreach (var item in second.Where(flags.ContainsKey)) - flags[item] = flag; - - // - // As per docs, "the marked elements are yielded in the order in - // which they were collected. - // - - return keys.Where(item => flags[item]); - } - - /// - /// Produces the set intersection of two sequences by using the - /// default equality comparer to compare values. - /// - - public static IEnumerable Intersect( - this IEnumerable first, - IEnumerable second) - { - return first.Intersect(second, /* comparer */ null); - } - - /// - /// Produces the set intersection of two sequences by using the - /// specified to compare values. - /// - - public static IEnumerable Intersect( - this IEnumerable first, - IEnumerable second, - IEqualityComparer comparer) - { - return IntersectExceptImpl(first, second, comparer, /* flag */ true); - } - - /// - /// Produces the set difference of two sequences by using the - /// default equality comparer to compare values. - /// - - public static IEnumerable Except( - this IEnumerable first, - IEnumerable second) - { - return first.Except(second, /* comparer */ null); - } - - /// - /// Produces the set difference of two sequences by using the - /// specified to compare values. - /// - - public static IEnumerable Except( - this IEnumerable first, - IEnumerable second, - IEqualityComparer comparer) - { - return IntersectExceptImpl(first, second, comparer, /* flag */ false); - } - - /// - /// Creates a from an - /// according to a specified key - /// selector function. - /// - - public static Dictionary ToDictionary( - this IEnumerable source, - Func keySelector) - { - return source.ToDictionary(keySelector, /* comparer */ null); - } - - /// - /// Creates a from an - /// according to a specified key - /// selector function and key comparer. - /// - - public static Dictionary ToDictionary( - this IEnumerable source, - Func keySelector, - IEqualityComparer comparer) - { - return source.ToDictionary(keySelector, e => e); - } - - /// - /// Creates a from an - /// according to specified key - /// selector and element selector functions. - /// - - public static Dictionary ToDictionary( - this IEnumerable source, - Func keySelector, - Func elementSelector) - { - return source.ToDictionary(keySelector, elementSelector, /* comparer */ null); - } - - /// - /// Creates a from an - /// according to a specified key - /// selector function, a comparer, and an element selector function. - /// - - public static Dictionary ToDictionary( - this IEnumerable source, - Func keySelector, - Func elementSelector, - IEqualityComparer comparer) - { - CheckNotNull(source, "source"); - CheckNotNull(keySelector, "keySelector"); - CheckNotNull(elementSelector, "elementSelector"); - - var dict = new Dictionary(comparer); - - foreach (var item in source) - { - // - // ToDictionary is meant to throw ArgumentNullException if - // keySelector produces a key that is null and - // Argument exception if keySelector produces duplicate keys - // for two elements. Incidentally, the doucmentation for - // IDictionary.Add says that the Add method - // throws the same exceptions under the same circumstances - // so we don't need to do any additional checking or work - // here and let the Add implementation do all the heavy - // lifting. - // - - dict.Add(keySelector(item), elementSelector(item)); - } - - return dict; - } - - /// - /// Correlates the elements of two sequences based on matching keys. - /// The default equality comparer is used to compare keys. - /// - - public static IEnumerable Join( - this IEnumerable outer, - IEnumerable inner, - Func outerKeySelector, - Func innerKeySelector, - Func resultSelector) - { - return outer.Join(inner, outerKeySelector, innerKeySelector, resultSelector, /* comparer */ null); - } - - /// - /// Correlates the elements of two sequences based on matching keys. - /// The default equality comparer is used to compare keys. A - /// specified is used to compare keys. - /// - - public static IEnumerable Join( - this IEnumerable outer, - IEnumerable inner, - Func outerKeySelector, - Func innerKeySelector, - Func resultSelector, - IEqualityComparer comparer) - { - CheckNotNull(outer, "outer"); - CheckNotNull(inner, "inner"); - CheckNotNull(outerKeySelector, "outerKeySelector"); - CheckNotNull(innerKeySelector, "innerKeySelector"); - CheckNotNull(resultSelector, "resultSelector"); - - var lookup = inner.ToLookup(innerKeySelector, comparer); - - return - from o in outer - from i in lookup[outerKeySelector(o)] - select resultSelector(o, i); - } - - /// - /// Correlates the elements of two sequences based on equality of - /// keys and groups the results. The default equality comparer is - /// used to compare keys. - /// - - public static IEnumerable GroupJoin( - this IEnumerable outer, - IEnumerable inner, - Func outerKeySelector, - Func innerKeySelector, - Func, TResult> resultSelector) - { - return outer.GroupJoin(inner, outerKeySelector, innerKeySelector, resultSelector, /* comparer */ null); - } - - /// - /// Correlates the elements of two sequences based on equality of - /// keys and groups the results. The default equality comparer is - /// used to compare keys. A specified - /// is used to compare keys. - /// - - public static IEnumerable GroupJoin( - this IEnumerable outer, - IEnumerable inner, - Func outerKeySelector, - Func innerKeySelector, - Func, TResult> resultSelector, - IEqualityComparer comparer) - { - CheckNotNull(outer, "outer"); - CheckNotNull(inner, "inner"); - CheckNotNull(outerKeySelector, "outerKeySelector"); - CheckNotNull(innerKeySelector, "innerKeySelector"); - CheckNotNull(resultSelector, "resultSelector"); - - var lookup = inner.ToLookup(innerKeySelector, comparer); - return outer.Select(o => resultSelector(o, lookup[outerKeySelector(o)])); - } - - [DebuggerStepThrough] - private static void CheckNotNull(T value, string name) where T : class - { - if (value == null) - throw new ArgumentNullException(name); - } - - private static class Sequence - { - public static readonly IEnumerable Empty = new T[0]; - } - - private sealed class Grouping : List, IGrouping - { - internal Grouping(K key) - { - Key = key; - } - - public K Key { get; private set; } - } + return source; } + + /// + /// Returns an empty that has the + /// specified type argument. + /// + + public static IEnumerable Empty() + { + return Sequence.Empty; + } + + /// + /// Converts the elements of an to the + /// specified type. + /// + + public static IEnumerable Cast( + this IEnumerable source) + { + CheckNotNull(source, "source"); + + return CastYield(source); + } + + private static IEnumerable CastYield( + IEnumerable source) + { + foreach (var item in source) + yield return (TResult) item; + } + + /// + /// Filters the elements of an based on a specified type. + /// + + public static IEnumerable OfType( + this IEnumerable source) + { + CheckNotNull(source, "source"); + + return OfTypeYield(source); + } + + private static IEnumerable OfTypeYield( + IEnumerable source) + { + foreach (var item in source) + if (item is TResult) + yield return (TResult) item; + } + + /// + /// Generates a sequence of integral numbers within a specified range. + /// + /// The value of the first integer in the sequence. + /// The number of sequential integers to generate. + + public static IEnumerable Range(int start, int count) + { + if (count < 0) + throw new ArgumentOutOfRangeException("count", count, null); + + var end = (long) start + count; + if (end - 1 >= int.MaxValue) + throw new ArgumentOutOfRangeException("count", count, null); + + return RangeYield(start, end); + } + + private static IEnumerable RangeYield(int start, long end) + { + for (var i = start; i < end; i++) + yield return i; + } + + /// + /// Generates a sequence that contains one repeated value. + /// + + public static IEnumerable Repeat(TResult element, int count) + { + if (count < 0) throw new ArgumentOutOfRangeException("count", count, null); + + return RepeatYield(element, count); + } + + private static IEnumerable RepeatYield(TResult element, int count) + { + for (var i = 0; i < count; i++) + yield return element; + } + + /// + /// Filters a sequence of values based on a predicate. + /// + + public static IEnumerable Where( + this IEnumerable source, + Func predicate) + { + CheckNotNull(predicate, "predicate"); + + return source.Where((item, i) => predicate(item)); + } + + /// + /// Filters a sequence of values based on a predicate. + /// Each element's index is used in the logic of the predicate function. + /// + + public static IEnumerable Where( + this IEnumerable source, + Func predicate) + { + CheckNotNull(source, "source"); + CheckNotNull(predicate, "predicate"); + + return WhereYield(source, predicate); + } + + private static IEnumerable WhereYield( + IEnumerable source, + Func predicate) + { + var i = 0; + foreach (var item in source) + if (predicate(item, i++)) + yield return item; + } + + /// + /// Projects each element of a sequence into a new form. + /// + + public static IEnumerable Select( + this IEnumerable source, + Func selector) + { + CheckNotNull(selector, "selector"); + + return source.Select((item, i) => selector(item)); + } + + /// + /// Projects each element of a sequence into a new form by + /// incorporating the element's index. + /// + + public static IEnumerable Select( + this IEnumerable source, + Func selector) + { + CheckNotNull(source, "source"); + CheckNotNull(selector, "selector"); + + return SelectYield(source, selector); + } + + private static IEnumerable SelectYield( + IEnumerable source, + Func selector) + { + var i = 0; + foreach (var item in source) + yield return selector(item, i++); + } + + /// + /// Projects each element of a sequence to an + /// and flattens the resulting sequences into one sequence. + /// + + public static IEnumerable SelectMany( + this IEnumerable source, + Func> selector) + { + CheckNotNull(selector, "selector"); + + return source.SelectMany((item, i) => selector(item)); + } + + /// + /// Projects each element of a sequence to an , + /// and flattens the resulting sequences into one sequence. The + /// index of each source element is used in the projected form of + /// that element. + /// + + public static IEnumerable SelectMany( + this IEnumerable source, + Func> selector) + { + CheckNotNull(selector, "selector"); + + return source.SelectMany(selector, (item, subitem) => subitem); + } + + /// + /// Projects each element of a sequence to an , + /// flattens the resulting sequences into one sequence, and invokes + /// a result selector function on each element therein. + /// + + public static IEnumerable SelectMany( + this IEnumerable source, + Func> collectionSelector, + Func resultSelector) + { + CheckNotNull(collectionSelector, "collectionSelector"); + + return source.SelectMany((item, i) => collectionSelector(item), resultSelector); + } + + /// + /// Projects each element of a sequence to an , + /// flattens the resulting sequences into one sequence, and invokes + /// a result selector function on each element therein. The index of + /// each source element is used in the intermediate projected form + /// of that element. + /// + + public static IEnumerable SelectMany( + this IEnumerable source, + Func> collectionSelector, + Func resultSelector) + { + CheckNotNull(source, "source"); + CheckNotNull(collectionSelector, "collectionSelector"); + CheckNotNull(resultSelector, "resultSelector"); + + return SelectManyYield(source, collectionSelector, resultSelector); + } + + private static IEnumerable SelectManyYield( + this IEnumerable source, + Func> collectionSelector, + Func resultSelector) + { + var i = 0; + foreach (var item in source) + foreach (var subitem in collectionSelector(item, i++)) + yield return resultSelector(item, subitem); + } + + /// + /// Returns elements from a sequence as long as a specified condition is true. + /// + + public static IEnumerable TakeWhile( + this IEnumerable source, + Func predicate) + { + CheckNotNull(predicate, "predicate"); + + return source.TakeWhile((item, i) => predicate(item)); + } + + /// + /// Returns elements from a sequence as long as a specified condition is true. + /// The element's index is used in the logic of the predicate function. + /// + + public static IEnumerable TakeWhile( + this IEnumerable source, + Func predicate) + { + CheckNotNull(source, "source"); + CheckNotNull(predicate, "predicate"); + + return TakeWhileYield(source, predicate); + } + + private static IEnumerable TakeWhileYield( + this IEnumerable source, + Func predicate) + { + var i = 0; + foreach (var item in source) + if (predicate(item, i++)) + yield return item; + else + break; + } + + private static class Futures + { + public static readonly Func Default = () => default(T); + public static readonly Func Undefined = () => { throw new InvalidOperationException(); }; + } + + /// + /// Base implementation of First operator. + /// + + private static TSource FirstImpl( + this IEnumerable source, + Func empty) + { + CheckNotNull(source, "source"); + Debug.Assert(empty != null); + + var list = source as IList; // optimized case for lists + if (list != null) + return list.Count > 0 ? list[0] : empty(); + + using (var e = source.GetEnumerator()) // fallback for enumeration + return e.MoveNext() ? e.Current : empty(); + } + + /// + /// Returns the first element of a sequence. + /// + + public static TSource First( + this IEnumerable source) + { + return source.FirstImpl(Futures.Undefined); + } + + /// + /// Returns the first element in a sequence that satisfies a specified condition. + /// + + public static TSource First( + this IEnumerable source, + Func predicate) + { + return First(source.Where(predicate)); + } + + /// + /// Returns the first element of a sequence, or a default value if + /// the sequence contains no elements. + /// + + public static TSource FirstOrDefault( + this IEnumerable source) + { + return source.FirstImpl(Futures.Default); + } + + /// + /// Returns the first element of the sequence that satisfies a + /// condition or a default value if no such element is found. + /// + + public static TSource FirstOrDefault( + this IEnumerable source, + Func predicate) + { + return FirstOrDefault(source.Where(predicate)); + } + + /// + /// Base implementation of Last operator. + /// + + private static TSource LastImpl( + this IEnumerable source, + Func empty) + { + CheckNotNull(source, "source"); + + var list = source as IList; // optimized case for lists + if (list != null) + return list.Count > 0 ? list[list.Count - 1] : empty(); + + using (var e = source.GetEnumerator()) + { + if (!e.MoveNext()) + return empty(); + + var last = e.Current; + while (e.MoveNext()) + last = e.Current; + + return last; + } + } + + /// + /// Returns the last element of a sequence. + /// + public static TSource Last( + this IEnumerable source) + { + return source.LastImpl(Futures.Undefined); + } + + /// + /// Returns the last element of a sequence that satisfies a + /// specified condition. + /// + + public static TSource Last( + this IEnumerable source, + Func predicate) + { + return Last(source.Where(predicate)); + } + + /// + /// Returns the last element of a sequence, or a default value if + /// the sequence contains no elements. + /// + + public static TSource LastOrDefault( + this IEnumerable source) + { + return source.LastImpl(Futures.Default); + } + + /// + /// Returns the last element of a sequence that satisfies a + /// condition or a default value if no such element is found. + /// + + public static TSource LastOrDefault( + this IEnumerable source, + Func predicate) + { + return LastOrDefault(source.Where(predicate)); + } + + /// + /// Base implementation of Single operator. + /// + + private static TSource SingleImpl( + this IEnumerable source, + Func empty) + { + CheckNotNull(source, "source"); + + using (var e = source.GetEnumerator()) + { + if (e.MoveNext()) + { + var single = e.Current; + if (!e.MoveNext()) + return single; + + throw new InvalidOperationException(); + } + + return empty(); + } + } + + /// + /// Returns the only element of a sequence, and throws an exception + /// if there is not exactly one element in the sequence. + /// + + public static TSource Single( + this IEnumerable source) + { + return source.SingleImpl(Futures.Undefined); + } + + /// + /// Returns the only element of a sequence that satisfies a + /// specified condition, and throws an exception if more than one + /// such element exists. + /// + + public static TSource Single( + this IEnumerable source, + Func predicate) + { + return Single(source.Where(predicate)); + } + + /// + /// Returns the only element of a sequence, or a default value if + /// the sequence is empty; this method throws an exception if there + /// is more than one element in the sequence. + /// + + public static TSource SingleOrDefault( + this IEnumerable source) + { + return source.SingleImpl(Futures.Default); + } + + /// + /// Returns the only element of a sequence that satisfies a + /// specified condition or a default value if no such element + /// exists; this method throws an exception if more than one element + /// satisfies the condition. + /// + + public static TSource SingleOrDefault( + this IEnumerable source, + Func predicate) + { + return SingleOrDefault(source.Where(predicate)); + } + + /// + /// Returns the element at a specified index in a sequence. + /// + + public static TSource ElementAt( + this IEnumerable source, + int index) + { + CheckNotNull(source, "source"); + + if (index < 0) + throw new ArgumentOutOfRangeException("index", index, null); + + var list = source as IList; + if (list != null) + return list[index]; + + try + { + return source.SkipWhile((item, i) => i < index).First(); + } + catch (InvalidOperationException) // if thrown by First + { + throw new ArgumentOutOfRangeException("index", index, null); + } + } + + /// + /// Returns the element at a specified index in a sequence or a + /// default value if the index is out of range. + /// + + public static TSource ElementAtOrDefault( + this IEnumerable source, + int index) + { + CheckNotNull(source, "source"); + + if (index < 0) + return default(TSource); + + var list = source as IList; + if (list != null) + return index < list.Count ? list[index] : default(TSource); + + return source.SkipWhile((item, i) => i < index).FirstOrDefault(); + } + + /// + /// Inverts the order of the elements in a sequence. + /// + + public static IEnumerable Reverse( + this IEnumerable source) + { + CheckNotNull(source, "source"); + + return ReverseYield(source); + } + + private static IEnumerable ReverseYield(IEnumerable source) + { + var stack = new Stack(); + foreach (var item in source) + stack.Push(item); + + foreach (var item in stack) + yield return item; + } + + /// + /// Returns a specified number of contiguous elements from the start + /// of a sequence. + /// + + public static IEnumerable Take( + this IEnumerable source, + int count) + { + return source.Where((item, i) => i < count); + } + + /// + /// Bypasses a specified number of elements in a sequence and then + /// returns the remaining elements. + /// + + public static IEnumerable Skip( + this IEnumerable source, + int count) + { + return source.Where((item, i) => i >= count); + } + + /// + /// Bypasses elements in a sequence as long as a specified condition + /// is true and then returns the remaining elements. + /// + + public static IEnumerable SkipWhile( + this IEnumerable source, + Func predicate) + { + CheckNotNull(predicate, "predicate"); + + return source.SkipWhile((item, i) => predicate(item)); + } + + /// + /// Bypasses elements in a sequence as long as a specified condition + /// is true and then returns the remaining elements. The element's + /// index is used in the logic of the predicate function. + /// + + public static IEnumerable SkipWhile( + this IEnumerable source, + Func predicate) + { + CheckNotNull(source, "source"); + CheckNotNull(predicate, "predicate"); + + return SkipWhileYield(source, predicate); + } + + private static IEnumerable SkipWhileYield( + IEnumerable source, + Func predicate) + { + using (var e = source.GetEnumerator()) + { + for (var i = 0;; i++) + { + if (!e.MoveNext()) + yield break; + + if (!predicate(e.Current, i)) + break; + } + + do + { + yield return e.Current; + } while (e.MoveNext()); + } + } + + /// + /// Returns the number of elements in a sequence. + /// + + public static int Count( + this IEnumerable source) + { + CheckNotNull(source, "source"); + + var collection = source as ICollection; + return collection != null + ? collection.Count + : source.Aggregate(0, (count, item) => checked(count + 1)); + } + + /// + /// Returns a number that represents how many elements in the + /// specified sequence satisfy a condition. + /// + + public static int Count( + this IEnumerable source, + Func predicate) + { + return Count(source.Where(predicate)); + } + + /// + /// Returns an that represents the total number + /// of elements in a sequence. + /// + + public static long LongCount( + this IEnumerable source) + { + CheckNotNull(source, "source"); + + var array = source as Array; + return array != null + ? array.LongLength + : source.Aggregate(0L, (count, item) => count + 1); + } + + /// + /// Returns an that represents how many elements + /// in a sequence satisfy a condition. + /// + + public static long LongCount( + this IEnumerable source, + Func predicate) + { + return LongCount(source.Where(predicate)); + } + + /// + /// Concatenates two sequences. + /// + + public static IEnumerable Concat( + this IEnumerable first, + IEnumerable second) + { + CheckNotNull(first, "first"); + CheckNotNull(second, "second"); + + return ConcatYield(first, second); + } + + private static IEnumerable ConcatYield( + IEnumerable first, + IEnumerable second) + { + foreach (var item in first) + yield return item; + + foreach (var item in second) + yield return item; + } + + /// + /// Creates a from an . + /// + + public static List ToList( + this IEnumerable source) + { + CheckNotNull(source, "source"); + + return new List(source); + } + + /// + /// Creates an array from an . + /// + + public static TSource[] ToArray( + this IEnumerable source) + { + return source.ToList().ToArray(); + } + + /// + /// Returns distinct elements from a sequence by using the default + /// equality comparer to compare values. + /// + + public static IEnumerable Distinct( + this IEnumerable source) + { + return Distinct(source, /* comparer */ null); + } + + /// + /// Returns distinct elements from a sequence by using a specified + /// to compare values. + /// + + public static IEnumerable Distinct( + this IEnumerable source, + IEqualityComparer comparer) + { + CheckNotNull(source, "source"); + + return DistinctYield(source, comparer); + } + + private static IEnumerable DistinctYield( + IEnumerable source, + IEqualityComparer comparer) + { + var set = new Dictionary(comparer); + var gotNull = false; + + foreach (var item in source) + { + if (item == null) + { + if (gotNull) + continue; + gotNull = true; + } + else + { + if (set.ContainsKey(item)) + continue; + set.Add(item, null); + } + + yield return item; + } + } + + /// + /// Creates a from an + /// according to a specified key + /// selector function. + /// + + public static ILookup ToLookup( + this IEnumerable source, + Func keySelector) + { + return ToLookup(source, keySelector, e => e, /* comparer */ null); + } + + /// + /// Creates a from an + /// according to a specified key + /// selector function and a key comparer. + /// + + public static ILookup ToLookup( + this IEnumerable source, + Func keySelector, + IEqualityComparer comparer) + { + return ToLookup(source, keySelector, e => e, comparer); + } + + /// + /// Creates a from an + /// according to specified key + /// and element selector functions. + /// + + public static ILookup ToLookup( + this IEnumerable source, + Func keySelector, + Func elementSelector) + { + return ToLookup(source, keySelector, elementSelector, /* comparer */ null); + } + + /// + /// Creates a from an + /// according to a specified key + /// selector function, a comparer and an element selector function. + /// + + public static ILookup ToLookup( + this IEnumerable source, + Func keySelector, + Func elementSelector, + IEqualityComparer comparer) + { + CheckNotNull(source, "source"); + CheckNotNull(keySelector, "keySelector"); + CheckNotNull(elementSelector, "elementSelector"); + + var lookup = new Lookup(comparer); + + foreach (var item in source) + { + var key = keySelector(item); + + var grouping = (Grouping) lookup.Find(key); + if (grouping == null) + { + grouping = new Grouping(key); + lookup.Add(grouping); + } + + grouping.Add(elementSelector(item)); + } + + return lookup; + } + + /// + /// Groups the elements of a sequence according to a specified key + /// selector function. + /// + + public static IEnumerable> GroupBy( + this IEnumerable source, + Func keySelector) + { + return GroupBy(source, keySelector, /* comparer */ null); + } + + /// + /// Groups the elements of a sequence according to a specified key + /// selector function and compares the keys by using a specified + /// comparer. + /// + + public static IEnumerable> GroupBy( + this IEnumerable source, + Func keySelector, + IEqualityComparer comparer) + { + return GroupBy(source, keySelector, e => e, comparer); + } + + /// + /// Groups the elements of a sequence according to a specified key + /// selector function and projects the elements for each group by + /// using a specified function. + /// + + public static IEnumerable> GroupBy( + this IEnumerable source, + Func keySelector, + Func elementSelector) + { + return GroupBy(source, keySelector, elementSelector, /* comparer */ null); + } + + /// + /// Groups the elements of a sequence according to a specified key + /// selector function and creates a result value from each group and + /// its key. + /// + + public static IEnumerable> GroupBy( + this IEnumerable source, + Func keySelector, + Func elementSelector, + IEqualityComparer comparer) + { + CheckNotNull(source, "source"); + CheckNotNull(keySelector, "keySelector"); + CheckNotNull(elementSelector, "elementSelector"); + + return ToLookup(source, keySelector, elementSelector, comparer); + } + + /// + /// Groups the elements of a sequence according to a key selector + /// function. The keys are compared by using a comparer and each + /// group's elements are projected by using a specified function. + /// + + public static IEnumerable GroupBy( + this IEnumerable source, + Func keySelector, + Func, TResult> resultSelector) + { + return GroupBy(source, keySelector, resultSelector, /* comparer */ null); + } + + /// + /// Groups the elements of a sequence according to a specified key + /// selector function and creates a result value from each group and + /// its key. The elements of each group are projected by using a + /// specified function. + /// + + public static IEnumerable GroupBy( + this IEnumerable source, + Func keySelector, + Func, TResult> resultSelector, + IEqualityComparer comparer) + { + CheckNotNull(source, "source"); + CheckNotNull(keySelector, "keySelector"); + CheckNotNull(resultSelector, "resultSelector"); + + return ToLookup(source, keySelector, comparer).Select(g => resultSelector(g.Key, g)); + } + + /// + /// Groups the elements of a sequence according to a specified key + /// selector function and creates a result value from each group and + /// its key. The keys are compared by using a specified comparer. + /// + + public static IEnumerable GroupBy( + this IEnumerable source, + Func keySelector, + Func elementSelector, + Func, TResult> resultSelector) + { + return GroupBy(source, keySelector, elementSelector, resultSelector, /* comparer */ null); + } + + /// + /// Groups the elements of a sequence according to a specified key + /// selector function and creates a result value from each group and + /// its key. Key values are compared by using a specified comparer, + /// and the elements of each group are projected by using a + /// specified function. + /// + + public static IEnumerable GroupBy( + this IEnumerable source, + Func keySelector, + Func elementSelector, + Func, TResult> resultSelector, + IEqualityComparer comparer) + { + CheckNotNull(source, "source"); + CheckNotNull(keySelector, "keySelector"); + CheckNotNull(elementSelector, "elementSelector"); + CheckNotNull(resultSelector, "resultSelector"); + + return ToLookup(source, keySelector, elementSelector, comparer) + .Select(g => resultSelector(g.Key, g)); + } + + /// + /// Applies an accumulator function over a sequence. + /// + + public static TSource Aggregate( + this IEnumerable source, + Func func) + { + CheckNotNull(source, "source"); + CheckNotNull(func, "func"); + + using (var e = source.GetEnumerator()) + { + if (!e.MoveNext()) + throw new InvalidOperationException(); + + return e.Renumerable().Skip(1).Aggregate(e.Current, func); + } + } + + /// + /// Applies an accumulator function over a sequence. The specified + /// seed value is used as the initial accumulator value. + /// + + public static TAccumulate Aggregate( + this IEnumerable source, + TAccumulate seed, + Func func) + { + return Aggregate(source, seed, func, r => r); + } + + /// + /// Applies an accumulator function over a sequence. The specified + /// seed value is used as the initial accumulator value, and the + /// specified function is used to select the result value. + /// + + public static TResult Aggregate( + this IEnumerable source, + TAccumulate seed, + Func func, + Func resultSelector) + { + CheckNotNull(source, "source"); + CheckNotNull(func, "func"); + CheckNotNull(resultSelector, "resultSelector"); + + var result = seed; + + foreach (var item in source) + result = func(result, item); + + return resultSelector(result); + } + + /// + /// Produces the set union of two sequences by using the default + /// equality comparer. + /// + + public static IEnumerable Union( + this IEnumerable first, + IEnumerable second) + { + return Union(first, second, /* comparer */ null); + } + + /// + /// Produces the set union of two sequences by using a specified + /// . + /// + + public static IEnumerable Union( + this IEnumerable first, + IEnumerable second, + IEqualityComparer comparer) + { + return first.Concat(second).Distinct(comparer); + } + + /// + /// Returns the elements of the specified sequence or the type + /// parameter's default value in a singleton collection if the + /// sequence is empty. + /// + + public static IEnumerable DefaultIfEmpty( + this IEnumerable source) + { + return source.DefaultIfEmpty(default(TSource)); + } + + /// + /// Returns the elements of the specified sequence or the specified + /// value in a singleton collection if the sequence is empty. + /// + + public static IEnumerable DefaultIfEmpty( + this IEnumerable source, + TSource defaultValue) + { + CheckNotNull(source, "source"); + + return DefaultIfEmptyYield(source, defaultValue); + } + + private static IEnumerable DefaultIfEmptyYield( + IEnumerable source, + TSource defaultValue) + { + using (var e = source.GetEnumerator()) + { + if (!e.MoveNext()) + yield return defaultValue; + else + do + { + yield return e.Current; + } while (e.MoveNext()); + } + } + + /// + /// Determines whether all elements of a sequence satisfy a condition. + /// + + public static bool All( + this IEnumerable source, + Func predicate) + { + CheckNotNull(source, "source"); + CheckNotNull(predicate, "predicate"); + + foreach (var item in source) + if (!predicate(item)) + return false; + + return true; + } + + /// + /// Determines whether a sequence contains any elements. + /// + + public static bool Any( + this IEnumerable source) + { + CheckNotNull(source, "source"); + + using (var e = source.GetEnumerator()) + return e.MoveNext(); + } + + /// + /// Determines whether any element of a sequence satisfies a + /// condition. + /// + + public static bool Any( + this IEnumerable source, + Func predicate) + { + return source.Where(predicate).Any(); + } + + /// + /// Determines whether a sequence contains a specified element by + /// using the default equality comparer. + /// + + public static bool Contains( + this IEnumerable source, + TSource value) + { + return source.Contains(value, /* comparer */ null); + } + + /// + /// Determines whether a sequence contains a specified element by + /// using a specified . + /// + + public static bool Contains( + this IEnumerable source, + TSource value, + IEqualityComparer comparer) + { + CheckNotNull(source, "source"); + + if (comparer == null) + { + var collection = source as ICollection; + if (collection != null) + return collection.Contains(value); + } + + comparer = comparer ?? EqualityComparer.Default; + return source.Any(item => comparer.Equals(item, value)); + } + + /// + /// Determines whether two sequences are equal by comparing the + /// elements by using the default equality comparer for their type. + /// + + public static bool SequenceEqual( + this IEnumerable first, + IEnumerable second) + { + return first.SequenceEqual(second, /* comparer */ null); + } + + /// + /// Determines whether two sequences are equal by comparing their + /// elements by using a specified . + /// + + public static bool SequenceEqual( + this IEnumerable first, + IEnumerable second, + IEqualityComparer comparer) + { + CheckNotNull(first, "frist"); + CheckNotNull(second, "second"); + + comparer = comparer ?? EqualityComparer.Default; + + using (IEnumerator lhs = first.GetEnumerator(), + rhs = second.GetEnumerator()) + { + do + { + if (!lhs.MoveNext()) + return !rhs.MoveNext(); + + if (!rhs.MoveNext()) + return false; + } while (comparer.Equals(lhs.Current, rhs.Current)); + } + + return false; + } + + /// + /// Base implementation for Min/Max operator. + /// + + private static TSource MinMaxImpl( + this IEnumerable source, + Func lesser) + { + CheckNotNull(source, "source"); + Debug.Assert(lesser != null); + + return source.Aggregate((a, item) => lesser(a, item) ? a : item); + } + + /// + /// Base implementation for Min/Max operator for nullable types. + /// + + private static TSource? MinMaxImpl( + this IEnumerable source, + TSource? seed, Func lesser) where TSource : struct + { + CheckNotNull(source, "source"); + Debug.Assert(lesser != null); + + return source.Aggregate(seed, (a, item) => lesser(a, item) ? a : item); + // == MinMaxImpl(Repeat(null, 1).Concat(source), lesser); + } + + /// + /// Returns the minimum value in a generic sequence. + /// + + public static TSource Min( + this IEnumerable source) + { + var comparer = Comparer.Default; + return source.MinMaxImpl((x, y) => comparer.Compare(x, y) < 0); + } + + /// + /// Invokes a transform function on each element of a generic + /// sequence and returns the minimum resulting value. + /// + + public static TResult Min( + this IEnumerable source, + Func selector) + { + return source.Select(selector).Min(); + } + + /// + /// Returns the maximum value in a generic sequence. + /// + + public static TSource Max( + this IEnumerable source) + { + var comparer = Comparer.Default; + return source.MinMaxImpl((x, y) => comparer.Compare(x, y) > 0); + } + + /// + /// Invokes a transform function on each element of a generic + /// sequence and returns the maximum resulting value. + /// + + public static TResult Max( + this IEnumerable source, + Func selector) + { + return source.Select(selector).Max(); + } + + /// + /// Makes an enumerator seen as enumerable once more. + /// + /// + /// The supplied enumerator must have been started. The first element + /// returned is the element the enumerator was on when passed in. + /// DO NOT use this method if the caller must be a generator. It is + /// mostly safe among aggregate operations. + /// + + private static IEnumerable Renumerable(this IEnumerator e) + { + Debug.Assert(e != null); + + do + { + yield return e.Current; + } while (e.MoveNext()); + } + + /// + /// Sorts the elements of a sequence in ascending order according to a key. + /// + + public static IOrderedEnumerable OrderBy( + this IEnumerable source, + Func keySelector) + { + return source.OrderBy(keySelector, /* comparer */ null); + } + + /// + /// Sorts the elements of a sequence in ascending order by using a + /// specified comparer. + /// + + public static IOrderedEnumerable OrderBy( + this IEnumerable source, + Func keySelector, + IComparer comparer) + { + CheckNotNull(source, "source"); + CheckNotNull(keySelector, "keySelector"); + + return new OrderedEnumerable(source, keySelector, comparer, /* descending */ false); + } + + /// + /// Sorts the elements of a sequence in descending order according to a key. + /// + + public static IOrderedEnumerable OrderByDescending( + this IEnumerable source, + Func keySelector) + { + return source.OrderByDescending(keySelector, /* comparer */ null); + } + + /// + /// Sorts the elements of a sequence in descending order by using a + /// specified comparer. + /// + + public static IOrderedEnumerable OrderByDescending( + this IEnumerable source, + Func keySelector, + IComparer comparer) + { + CheckNotNull(source, "source"); + CheckNotNull(source, "keySelector"); + + return new OrderedEnumerable(source, keySelector, comparer, /* descending */ true); + } + + /// + /// Performs a subsequent ordering of the elements in a sequence in + /// ascending order according to a key. + /// + + public static IOrderedEnumerable ThenBy( + this IOrderedEnumerable source, + Func keySelector) + { + return source.ThenBy(keySelector, /* comparer */ null); + } + + /// + /// Performs a subsequent ordering of the elements in a sequence in + /// ascending order by using a specified comparer. + /// + + public static IOrderedEnumerable ThenBy( + this IOrderedEnumerable source, + Func keySelector, + IComparer comparer) + { + CheckNotNull(source, "source"); + + return source.CreateOrderedEnumerable(keySelector, comparer, /* descending */ false); + } + + /// + /// Performs a subsequent ordering of the elements in a sequence in + /// descending order, according to a key. + /// + + public static IOrderedEnumerable ThenByDescending( + this IOrderedEnumerable source, + Func keySelector) + { + return source.ThenByDescending(keySelector, /* comparer */ null); + } + + /// + /// Performs a subsequent ordering of the elements in a sequence in + /// descending order by using a specified comparer. + /// + + public static IOrderedEnumerable ThenByDescending( + this IOrderedEnumerable source, + Func keySelector, + IComparer comparer) + { + CheckNotNull(source, "source"); + + return source.CreateOrderedEnumerable(keySelector, comparer, /* descending */ true); + } + + /// + /// Base implementation for Intersect and Except operators. + /// + + private static IEnumerable IntersectExceptImpl( + this IEnumerable first, + IEnumerable second, + IEqualityComparer comparer, + bool flag) + { + CheckNotNull(first, "first"); + CheckNotNull(second, "second"); + + var keys = new List(); + var flags = new Dictionary(comparer); + + foreach (var item in first.Where(k => !flags.ContainsKey(k))) + { + flags.Add(item, !flag); + keys.Add(item); + } + + foreach (var item in second.Where(flags.ContainsKey)) + flags[item] = flag; + + // + // As per docs, "the marked elements are yielded in the order in + // which they were collected. + // + + return keys.Where(item => flags[item]); + } + + /// + /// Produces the set intersection of two sequences by using the + /// default equality comparer to compare values. + /// + + public static IEnumerable Intersect( + this IEnumerable first, + IEnumerable second) + { + return first.Intersect(second, /* comparer */ null); + } + + /// + /// Produces the set intersection of two sequences by using the + /// specified to compare values. + /// + + public static IEnumerable Intersect( + this IEnumerable first, + IEnumerable second, + IEqualityComparer comparer) + { + return IntersectExceptImpl(first, second, comparer, /* flag */ true); + } + + /// + /// Produces the set difference of two sequences by using the + /// default equality comparer to compare values. + /// + + public static IEnumerable Except( + this IEnumerable first, + IEnumerable second) + { + return first.Except(second, /* comparer */ null); + } + + /// + /// Produces the set difference of two sequences by using the + /// specified to compare values. + /// + + public static IEnumerable Except( + this IEnumerable first, + IEnumerable second, + IEqualityComparer comparer) + { + return IntersectExceptImpl(first, second, comparer, /* flag */ false); + } + + /// + /// Creates a from an + /// according to a specified key + /// selector function. + /// + + public static Dictionary ToDictionary( + this IEnumerable source, + Func keySelector) + { + return source.ToDictionary(keySelector, /* comparer */ null); + } + + /// + /// Creates a from an + /// according to a specified key + /// selector function and key comparer. + /// + + public static Dictionary ToDictionary( + this IEnumerable source, + Func keySelector, + IEqualityComparer comparer) + { + return source.ToDictionary(keySelector, e => e); + } + + /// + /// Creates a from an + /// according to specified key + /// selector and element selector functions. + /// + + public static Dictionary ToDictionary( + this IEnumerable source, + Func keySelector, + Func elementSelector) + { + return source.ToDictionary(keySelector, elementSelector, /* comparer */ null); + } + + /// + /// Creates a from an + /// according to a specified key + /// selector function, a comparer, and an element selector function. + /// + + public static Dictionary ToDictionary( + this IEnumerable source, + Func keySelector, + Func elementSelector, + IEqualityComparer comparer) + { + CheckNotNull(source, "source"); + CheckNotNull(keySelector, "keySelector"); + CheckNotNull(elementSelector, "elementSelector"); + + var dict = new Dictionary(comparer); + + foreach (var item in source) + { + // + // ToDictionary is meant to throw ArgumentNullException if + // keySelector produces a key that is null and + // Argument exception if keySelector produces duplicate keys + // for two elements. Incidentally, the doucmentation for + // IDictionary.Add says that the Add method + // throws the same exceptions under the same circumstances + // so we don't need to do any additional checking or work + // here and let the Add implementation do all the heavy + // lifting. + // + + dict.Add(keySelector(item), elementSelector(item)); + } + + return dict; + } + + /// + /// Correlates the elements of two sequences based on matching keys. + /// The default equality comparer is used to compare keys. + /// + + public static IEnumerable Join( + this IEnumerable outer, + IEnumerable inner, + Func outerKeySelector, + Func innerKeySelector, + Func resultSelector) + { + return outer.Join(inner, outerKeySelector, innerKeySelector, resultSelector, /* comparer */ null); + } + + /// + /// Correlates the elements of two sequences based on matching keys. + /// The default equality comparer is used to compare keys. A + /// specified is used to compare keys. + /// + + public static IEnumerable Join( + this IEnumerable outer, + IEnumerable inner, + Func outerKeySelector, + Func innerKeySelector, + Func resultSelector, + IEqualityComparer comparer) + { + CheckNotNull(outer, "outer"); + CheckNotNull(inner, "inner"); + CheckNotNull(outerKeySelector, "outerKeySelector"); + CheckNotNull(innerKeySelector, "innerKeySelector"); + CheckNotNull(resultSelector, "resultSelector"); + + var lookup = inner.ToLookup(innerKeySelector, comparer); + + return + from o in outer + from i in lookup[outerKeySelector(o)] + select resultSelector(o, i); + } + + /// + /// Correlates the elements of two sequences based on equality of + /// keys and groups the results. The default equality comparer is + /// used to compare keys. + /// + + public static IEnumerable GroupJoin( + this IEnumerable outer, + IEnumerable inner, + Func outerKeySelector, + Func innerKeySelector, + Func, TResult> resultSelector) + { + return outer.GroupJoin(inner, outerKeySelector, innerKeySelector, resultSelector, /* comparer */ null); + } + + /// + /// Correlates the elements of two sequences based on equality of + /// keys and groups the results. The default equality comparer is + /// used to compare keys. A specified + /// is used to compare keys. + /// + + public static IEnumerable GroupJoin( + this IEnumerable outer, + IEnumerable inner, + Func outerKeySelector, + Func innerKeySelector, + Func, TResult> resultSelector, + IEqualityComparer comparer) + { + CheckNotNull(outer, "outer"); + CheckNotNull(inner, "inner"); + CheckNotNull(outerKeySelector, "outerKeySelector"); + CheckNotNull(innerKeySelector, "innerKeySelector"); + CheckNotNull(resultSelector, "resultSelector"); + + var lookup = inner.ToLookup(innerKeySelector, comparer); + return outer.Select(o => resultSelector(o, lookup[outerKeySelector(o)])); + } + + [DebuggerStepThrough] + private static void CheckNotNull(T value, string name) where T : class + { + if (value == null) + throw new ArgumentNullException(name); + } + + private static class Sequence + { + public static readonly IEnumerable Empty = new T[0]; + } + + private sealed class Grouping : List, IGrouping + { + internal Grouping(K key) + { + Key = key; + } + + public K Key { get; private set; } + } + } + + internal partial class Enumerable + { + /// + /// Computes the sum of a sequence of nullable values. + /// + + public static int Sum( + this IEnumerable source) + { + CheckNotNull(source, "source"); + + int sum = 0; + foreach (var num in source) + sum = checked(sum + num); + + return sum; + } + + /// + /// Computes the sum of a sequence of nullable + /// values that are obtained by invoking a transform function on + /// each element of the input sequence. + /// + + public static int Sum( + this IEnumerable source, + Func selector) + { + return source.Select(selector).Sum(); + } + + /// + /// Computes the average of a sequence of nullable values. + /// + + public static double Average( + this IEnumerable source) + { + CheckNotNull(source, "source"); + + long sum = 0; + long count = 0; + + foreach (var num in source) + checked + { + sum += (int) num; + count++; + } + + if (count == 0) + throw new InvalidOperationException(); + + return (double) sum/count; + } + + /// + /// Computes the average of a sequence of nullable values + /// that are obtained by invoking a transform function on each + /// element of the input sequence. + /// + + public static double Average( + this IEnumerable source, + Func selector) + { + return source.Select(selector).Average(); + } + + + /// + /// Computes the sum of a sequence of values. + /// + + public static int? Sum( + this IEnumerable source) + { + CheckNotNull(source, "source"); + + int sum = 0; + foreach (var num in source) + sum = checked(sum + (num ?? 0)); + + return sum; + } + + /// + /// Computes the sum of a sequence of + /// values that are obtained by invoking a transform function on + /// each element of the input sequence. + /// + + public static int? Sum( + this IEnumerable source, + Func selector) + { + return source.Select(selector).Sum(); + } + + /// + /// Computes the average of a sequence of values. + /// + + public static double? Average( + this IEnumerable source) + { + CheckNotNull(source, "source"); + + long sum = 0; + long count = 0; + + foreach (var num in source.Where(n => n != null)) + checked + { + sum += (int) num; + count++; + } + + if (count == 0) + return null; + + return (double?) sum/count; + } + + /// + /// Computes the average of a sequence of values + /// that are obtained by invoking a transform function on each + /// element of the input sequence. + /// + + public static double? Average( + this IEnumerable source, + Func selector) + { + return source.Select(selector).Average(); + } + + /// + /// Returns the minimum value in a sequence of nullable + /// values. + /// + + public static int? Min( + this IEnumerable source) + { + CheckNotNull(source, "source"); + + return MinMaxImpl(source.Where(x => x != null), null, (min, x) => min < x); + } + + /// + /// Invokes a transform function on each element of a sequence and + /// returns the minimum nullable value. + /// + + public static int? Min( + this IEnumerable source, + Func selector) + { + return source.Select(selector).Min(); + } + + /// + /// Returns the maximum value in a sequence of nullable + /// values. + /// + + public static int? Max( + this IEnumerable source) + { + CheckNotNull(source, "source"); + + return MinMaxImpl(source.Where(x => x != null), + null, (max, x) => x == null || (max != null && x.Value < max.Value)); + } + + /// + /// Invokes a transform function on each element of a sequence and + /// returns the maximum nullable value. + /// + + public static int? Max( + this IEnumerable source, + Func selector) + { + return source.Select(selector).Max(); + } + + /// + /// Computes the sum of a sequence of nullable values. + /// + + public static long Sum( + this IEnumerable source) + { + CheckNotNull(source, "source"); + + long sum = 0; + foreach (var num in source) + sum = checked(sum + num); + + return sum; + } + + /// + /// Computes the sum of a sequence of nullable + /// values that are obtained by invoking a transform function on + /// each element of the input sequence. + /// + + public static long Sum( + this IEnumerable source, + Func selector) + { + return source.Select(selector).Sum(); + } + + /// + /// Computes the average of a sequence of nullable values. + /// + + public static double Average( + this IEnumerable source) + { + CheckNotNull(source, "source"); + + long sum = 0; + long count = 0; + + foreach (var num in source) + checked + { + sum += (long) num; + count++; + } + + if (count == 0) + throw new InvalidOperationException(); + + return (double) sum/count; + } + + /// + /// Computes the average of a sequence of nullable values + /// that are obtained by invoking a transform function on each + /// element of the input sequence. + /// + + public static double Average( + this IEnumerable source, + Func selector) + { + return source.Select(selector).Average(); + } + + + /// + /// Computes the sum of a sequence of values. + /// + + public static long? Sum( + this IEnumerable source) + { + CheckNotNull(source, "source"); + + long sum = 0; + foreach (var num in source) + sum = checked(sum + (num ?? 0)); + + return sum; + } + + /// + /// Computes the sum of a sequence of + /// values that are obtained by invoking a transform function on + /// each element of the input sequence. + /// + + public static long? Sum( + this IEnumerable source, + Func selector) + { + return source.Select(selector).Sum(); + } + + /// + /// Computes the average of a sequence of values. + /// + + public static double? Average( + this IEnumerable source) + { + CheckNotNull(source, "source"); + + long sum = 0; + long count = 0; + + foreach (var num in source.Where(n => n != null)) + checked + { + sum += (long) num; + count++; + } + + if (count == 0) + return null; + + return (double?) sum/count; + } + + /// + /// Computes the average of a sequence of values + /// that are obtained by invoking a transform function on each + /// element of the input sequence. + /// + + public static double? Average( + this IEnumerable source, + Func selector) + { + return source.Select(selector).Average(); + } + + /// + /// Returns the minimum value in a sequence of nullable + /// values. + /// + + public static long? Min( + this IEnumerable source) + { + CheckNotNull(source, "source"); + + return MinMaxImpl(source.Where(x => x != null), null, (min, x) => min < x); + } + + /// + /// Invokes a transform function on each element of a sequence and + /// returns the minimum nullable value. + /// + + public static long? Min( + this IEnumerable source, + Func selector) + { + return source.Select(selector).Min(); + } + + /// + /// Returns the maximum value in a sequence of nullable + /// values. + /// + + public static long? Max( + this IEnumerable source) + { + CheckNotNull(source, "source"); + + return MinMaxImpl(source.Where(x => x != null), + null, (max, x) => x == null || (max != null && x.Value < max.Value)); + } + + /// + /// Invokes a transform function on each element of a sequence and + /// returns the maximum nullable value. + /// + + public static long? Max( + this IEnumerable source, + Func selector) + { + return source.Select(selector).Max(); + } + + /// + /// Computes the sum of a sequence of nullable values. + /// + + public static float Sum( + this IEnumerable source) + { + CheckNotNull(source, "source"); + + float sum = 0; + foreach (var num in source) + sum = checked(sum + num); + + return sum; + } + + /// + /// Computes the sum of a sequence of nullable + /// values that are obtained by invoking a transform function on + /// each element of the input sequence. + /// + + public static float Sum( + this IEnumerable source, + Func selector) + { + return source.Select(selector).Sum(); + } + + /// + /// Computes the average of a sequence of nullable values. + /// + + public static float Average( + this IEnumerable source) + { + CheckNotNull(source, "source"); + + float sum = 0; + long count = 0; + + foreach (var num in source) + checked + { + sum += (float) num; + count++; + } + + if (count == 0) + throw new InvalidOperationException(); + + return (float) sum/count; + } + + /// + /// Computes the average of a sequence of nullable values + /// that are obtained by invoking a transform function on each + /// element of the input sequence. + /// + + public static float Average( + this IEnumerable source, + Func selector) + { + return source.Select(selector).Average(); + } + + + /// + /// Computes the sum of a sequence of values. + /// + + public static float? Sum( + this IEnumerable source) + { + CheckNotNull(source, "source"); + + float sum = 0; + foreach (var num in source) + sum = checked(sum + (num ?? 0)); + + return sum; + } + + /// + /// Computes the sum of a sequence of + /// values that are obtained by invoking a transform function on + /// each element of the input sequence. + /// + + public static float? Sum( + this IEnumerable source, + Func selector) + { + return source.Select(selector).Sum(); + } + + /// + /// Computes the average of a sequence of values. + /// + + public static float? Average( + this IEnumerable source) + { + CheckNotNull(source, "source"); + + float sum = 0; + long count = 0; + + foreach (var num in source.Where(n => n != null)) + checked + { + sum += (float) num; + count++; + } + + if (count == 0) + return null; + + return (float?) sum/count; + } + + /// + /// Computes the average of a sequence of values + /// that are obtained by invoking a transform function on each + /// element of the input sequence. + /// + + public static float? Average( + this IEnumerable source, + Func selector) + { + return source.Select(selector).Average(); + } + + /// + /// Returns the minimum value in a sequence of nullable + /// values. + /// + + public static float? Min( + this IEnumerable source) + { + CheckNotNull(source, "source"); + + return MinMaxImpl(source.Where(x => x != null), null, (min, x) => min < x); + } + + /// + /// Invokes a transform function on each element of a sequence and + /// returns the minimum nullable value. + /// + + public static float? Min( + this IEnumerable source, + Func selector) + { + return source.Select(selector).Min(); + } + + /// + /// Returns the maximum value in a sequence of nullable + /// values. + /// + + public static float? Max( + this IEnumerable source) + { + CheckNotNull(source, "source"); + + return MinMaxImpl(source.Where(x => x != null), + null, (max, x) => x == null || (max != null && x.Value < max.Value)); + } + + /// + /// Invokes a transform function on each element of a sequence and + /// returns the maximum nullable value. + /// + + public static float? Max( + this IEnumerable source, + Func selector) + { + return source.Select(selector).Max(); + } + + /// + /// Computes the sum of a sequence of nullable values. + /// + + public static double Sum( + this IEnumerable source) + { + CheckNotNull(source, "source"); + + double sum = 0; + foreach (var num in source) + sum = checked(sum + num); + + return sum; + } + + /// + /// Computes the sum of a sequence of nullable + /// values that are obtained by invoking a transform function on + /// each element of the input sequence. + /// + + public static double Sum( + this IEnumerable source, + Func selector) + { + return source.Select(selector).Sum(); + } + + /// + /// Computes the average of a sequence of nullable values. + /// + + public static double Average( + this IEnumerable source) + { + CheckNotNull(source, "source"); + + double sum = 0; + long count = 0; + + foreach (var num in source) + checked + { + sum += (double) num; + count++; + } + + if (count == 0) + throw new InvalidOperationException(); + + return (double) sum/count; + } + + /// + /// Computes the average of a sequence of nullable values + /// that are obtained by invoking a transform function on each + /// element of the input sequence. + /// + + public static double Average( + this IEnumerable source, + Func selector) + { + return source.Select(selector).Average(); + } + + + /// + /// Computes the sum of a sequence of values. + /// + + public static double? Sum( + this IEnumerable source) + { + CheckNotNull(source, "source"); + + double sum = 0; + foreach (var num in source) + sum = checked(sum + (num ?? 0)); + + return sum; + } + + /// + /// Computes the sum of a sequence of + /// values that are obtained by invoking a transform function on + /// each element of the input sequence. + /// + + public static double? Sum( + this IEnumerable source, + Func selector) + { + return source.Select(selector).Sum(); + } + + /// + /// Computes the average of a sequence of values. + /// + + public static double? Average( + this IEnumerable source) + { + CheckNotNull(source, "source"); + + double sum = 0; + long count = 0; + + foreach (var num in source.Where(n => n != null)) + checked + { + sum += (double) num; + count++; + } + + if (count == 0) + return null; + + return (double?) sum/count; + } + + /// + /// Computes the average of a sequence of values + /// that are obtained by invoking a transform function on each + /// element of the input sequence. + /// + + public static double? Average( + this IEnumerable source, + Func selector) + { + return source.Select(selector).Average(); + } + + /// + /// Returns the minimum value in a sequence of nullable + /// values. + /// + + public static double? Min( + this IEnumerable source) + { + CheckNotNull(source, "source"); + + return MinMaxImpl(source.Where(x => x != null), null, (min, x) => min < x); + } + + /// + /// Invokes a transform function on each element of a sequence and + /// returns the minimum nullable value. + /// + + public static double? Min( + this IEnumerable source, + Func selector) + { + return source.Select(selector).Min(); + } + + /// + /// Returns the maximum value in a sequence of nullable + /// values. + /// + + public static double? Max( + this IEnumerable source) + { + CheckNotNull(source, "source"); + + return MinMaxImpl(source.Where(x => x != null), + null, (max, x) => x == null || (max != null && x.Value < max.Value)); + } + + /// + /// Invokes a transform function on each element of a sequence and + /// returns the maximum nullable value. + /// + + public static double? Max( + this IEnumerable source, + Func selector) + { + return source.Select(selector).Max(); + } + + /// + /// Computes the sum of a sequence of nullable values. + /// + + public static decimal Sum( + this IEnumerable source) + { + CheckNotNull(source, "source"); + + decimal sum = 0; + foreach (var num in source) + sum = checked(sum + num); + + return sum; + } + + /// + /// Computes the sum of a sequence of nullable + /// values that are obtained by invoking a transform function on + /// each element of the input sequence. + /// + + public static decimal Sum( + this IEnumerable source, + Func selector) + { + return source.Select(selector).Sum(); + } + + /// + /// Computes the average of a sequence of nullable values. + /// + + public static decimal Average( + this IEnumerable source) + { + CheckNotNull(source, "source"); + + decimal sum = 0; + long count = 0; + + foreach (var num in source) + checked + { + sum += (decimal) num; + count++; + } + + if (count == 0) + throw new InvalidOperationException(); + + return (decimal) sum/count; + } + + /// + /// Computes the average of a sequence of nullable values + /// that are obtained by invoking a transform function on each + /// element of the input sequence. + /// + + public static decimal Average( + this IEnumerable source, + Func selector) + { + return source.Select(selector).Average(); + } + + + /// + /// Computes the sum of a sequence of values. + /// + + public static decimal? Sum( + this IEnumerable source) + { + CheckNotNull(source, "source"); + + decimal sum = 0; + foreach (var num in source) + sum = checked(sum + (num ?? 0)); + + return sum; + } + + /// + /// Computes the sum of a sequence of + /// values that are obtained by invoking a transform function on + /// each element of the input sequence. + /// + + public static decimal? Sum( + this IEnumerable source, + Func selector) + { + return source.Select(selector).Sum(); + } + + /// + /// Computes the average of a sequence of values. + /// + + public static decimal? Average( + this IEnumerable source) + { + CheckNotNull(source, "source"); + + decimal sum = 0; + long count = 0; + + foreach (var num in source.Where(n => n != null)) + checked + { + sum += (decimal) num; + count++; + } + + if (count == 0) + return null; + + return (decimal?) sum/count; + } + + /// + /// Computes the average of a sequence of values + /// that are obtained by invoking a transform function on each + /// element of the input sequence. + /// + + public static decimal? Average( + this IEnumerable source, + Func selector) + { + return source.Select(selector).Average(); + } + + /// + /// Returns the minimum value in a sequence of nullable + /// values. + /// + + public static decimal? Min( + this IEnumerable source) + { + CheckNotNull(source, "source"); + + return MinMaxImpl(source.Where(x => x != null), null, (min, x) => min < x); + } + + /// + /// Invokes a transform function on each element of a sequence and + /// returns the minimum nullable value. + /// + + public static decimal? Min( + this IEnumerable source, + Func selector) + { + return source.Select(selector).Min(); + } + + /// + /// Returns the maximum value in a sequence of nullable + /// values. + /// + + public static decimal? Max( + this IEnumerable source) + { + CheckNotNull(source, "source"); + + return MinMaxImpl(source.Where(x => x != null), + null, (max, x) => x == null || (max != null && x.Value < max.Value)); + } + + /// + /// Invokes a transform function on each element of a sequence and + /// returns the maximum nullable value. + /// + + public static decimal? Max( + this IEnumerable source, + Func selector) + { + return source.Select(selector).Max(); + } + } + + /// + /// Represents a collection of objects that have a common key. + /// + internal partial interface IGrouping : IEnumerable + { + /// + /// Gets the key of the . + /// + + TKey Key { get; } + } + + /// + /// Defines an indexer, size property, and Boolean search method for + /// data structures that map keys to + /// sequences of values. + /// + internal partial interface ILookup : IEnumerable> + { + bool Contains(TKey key); + int Count { get; } + IEnumerable this[TKey key] { get; } + } + + /// + /// Represents a sorted sequence. + /// + internal partial interface IOrderedEnumerable : IEnumerable + { + /// + /// Performs a subsequent ordering on the elements of an + /// according to a key. + /// + + IOrderedEnumerable CreateOrderedEnumerable( + Func keySelector, IComparer comparer, bool descending); + } + + /// + /// Represents a collection of keys each mapped to one or more values. + /// + internal sealed class Lookup : ILookup + { + private readonly Dictionary> _map; + + internal Lookup(IEqualityComparer comparer) + { + _map = new Dictionary>(comparer); + } + + internal void Add(IGrouping item) + { + _map.Add(item.Key, item); + } + + internal IEnumerable Find(TKey key) + { + IGrouping grouping; + return _map.TryGetValue(key, out grouping) ? grouping : null; + } + + /// + /// Gets the number of key/value collection pairs in the . + /// + + public int Count + { + get { return _map.Count; } + } + + /// + /// Gets the collection of values indexed by the specified key. + /// + + public IEnumerable this[TKey key] + { + get + { + IGrouping result; + return _map.TryGetValue(key, out result) ? result : Enumerable.Empty(); + } + } + + /// + /// Determines whether a specified key is in the . + /// + + public bool Contains(TKey key) + { + return _map.ContainsKey(key); + } + + /// + /// Applies a transform function to each key and its associated + /// values and returns the results. + /// + + public IEnumerable ApplyResultSelector( + Func, TResult> resultSelector) + { + if (resultSelector == null) + throw new ArgumentNullException("resultSelector"); + + foreach (var pair in _map) + yield return resultSelector(pair.Key, pair.Value); + } + + /// + /// Returns a generic enumerator that iterates through the . + /// + + public IEnumerator> GetEnumerator() + { + return _map.Values.GetEnumerator(); + } + + IEnumerator IEnumerable.GetEnumerator() + { + return GetEnumerator(); + } + } + + internal sealed class OrderedEnumerable : IOrderedEnumerable + { + private readonly IEnumerable _source; + private readonly List> _comparisons; + + public OrderedEnumerable(IEnumerable source, + Func keySelector, IComparer comparer, bool descending) : + this(source, null, keySelector, comparer, descending) + { + } + + private OrderedEnumerable(IEnumerable source, List> comparisons, + Func keySelector, IComparer comparer, bool descending) + { + if (source == null) throw new ArgumentNullException("source"); + if (keySelector == null) throw new ArgumentNullException("keySelector"); + + _source = source; + + comparer = comparer ?? Comparer.Default; + + if (comparisons == null) + comparisons = new List>( /* capacity */ 4); + + comparisons.Add((x, y) + => (descending ? -1 : 1)*comparer.Compare(keySelector(x), keySelector(y))); + + _comparisons = comparisons; + } + + public IOrderedEnumerable CreateOrderedEnumerable( + Func keySelector, IComparer comparer, bool descending) + { + return new OrderedEnumerable(_source, _comparisons, keySelector, comparer, descending); + } + + public IEnumerator GetEnumerator() + { + // + // We sort using List.Sort, but docs say that it performs an + // unstable sort. LINQ, on the other hand, says OrderBy performs + // a stable sort. So convert the source sequence into a sequence + // of tuples where the second element tags the position of the + // element from the source sequence (First). The position is + // then used as a tie breaker when all keys compare equal, + // thus making the sort stable. + // + + var list = _source.Select(new Func>(TagPosition)).ToList(); + + list.Sort((x, y) => + { + // + // Compare keys from left to right. + // + + var comparisons = _comparisons; + for (var i = 0; i < comparisons.Count; i++) + { + var result = comparisons[i](x.First, y.First); + if (result != 0) + return result; + } + + // + // All keys compared equal so now break the tie by their + // position in the original sequence, making the sort stable. + // + + return x.Second.CompareTo(y.Second); + }); + + return list.Select(new Func, T>(GetFirst)).GetEnumerator(); + + } + + /// + /// See issue #11 + /// for why this method is needed and cannot be expressed as a + /// lambda at the call site. + /// + + private static Tuple TagPosition(T e, int i) + { + return new Tuple(e, i); + } + + /// + /// See issue #11 + /// for why this method is needed and cannot be expressed as a + /// lambda at the call site. + /// + + private static T GetFirst(Tuple pv) + { + return pv.First; + } + + IEnumerator IEnumerable.GetEnumerator() + { + return GetEnumerator(); + } + } + + [Serializable] + internal struct Tuple : IEquatable> + { + public TFirst First { get; private set; } + public TSecond Second { get; private set; } + + public Tuple(TFirst first, TSecond second) + : this() + { + First = first; + Second = second; + } + + public override bool Equals(object obj) + { + return obj != null + && obj is Tuple + && base.Equals((Tuple) obj); + } + + public bool Equals(Tuple other) + { + return EqualityComparer.Default.Equals(other.First, First) + && EqualityComparer.Default.Equals(other.Second, Second); + } + + public override int GetHashCode() + { + var num = 0x7a2f0b42; + num = (-1521134295*num) + EqualityComparer.Default.GetHashCode(First); + return (-1521134295*num) + EqualityComparer.Default.GetHashCode(Second); + } + + public override string ToString() + { + return string.Format(@"{{ First = {0}, Second = {1} }}", First, Second); + } + } } -// $Id: Enumerable.g.cs 215 2009-10-03 13:31:49Z azizatif $ - -namespace System.Linq +namespace Newtonsoft.Json.Serialization { - #region Imports + public delegate TResult Func(); - using System; - using System.Collections.Generic; + public delegate TResult Func(T a); - #endregion - - // This partial implementation was template-generated: - // Sat, 03 Oct 2009 09:42:39 GMT + public delegate TResult Func(T1 arg1, T2 arg2); - partial class Enumerable - { - /// - /// Computes the sum of a sequence of nullable values. - /// + public delegate TResult Func(T1 arg1, T2 arg2, T3 arg3); - public static int Sum( - this IEnumerable source) - { - CheckNotNull(source, "source"); + public delegate TResult Func(T1 arg1, T2 arg2, T3 arg3, T4 arg4); - int sum = 0; - foreach (var num in source) - sum = checked(sum + num); + public delegate void Action(); - return sum; - } + public delegate void Action(T1 arg1, T2 arg2); - /// - /// Computes the sum of a sequence of nullable - /// values that are obtained by invoking a transform function on - /// each element of the input sequence. - /// + public delegate void Action(T1 arg1, T2 arg2, T3 arg3); - public static int Sum( - this IEnumerable source, - Func selector) - { - return source.Select(selector).Sum(); - } - - /// - /// Computes the average of a sequence of nullable values. - /// - - public static double Average( - this IEnumerable source) - { - CheckNotNull(source, "source"); - - long sum = 0; - long count = 0; - - foreach (var num in source) - checked - { - sum += (int) num; - count++; - } - - if (count == 0) - throw new InvalidOperationException(); - - return (double) sum / count; - } - - /// - /// Computes the average of a sequence of nullable values - /// that are obtained by invoking a transform function on each - /// element of the input sequence. - /// - - public static double Average( - this IEnumerable source, - Func selector) - { - return source.Select(selector).Average(); - } - - - /// - /// Computes the sum of a sequence of values. - /// - - public static int? Sum( - this IEnumerable source) - { - CheckNotNull(source, "source"); - - int sum = 0; - foreach (var num in source) - sum = checked(sum + (num ?? 0)); - - return sum; - } - - /// - /// Computes the sum of a sequence of - /// values that are obtained by invoking a transform function on - /// each element of the input sequence. - /// - - public static int? Sum( - this IEnumerable source, - Func selector) - { - return source.Select(selector).Sum(); - } - - /// - /// Computes the average of a sequence of values. - /// - - public static double? Average( - this IEnumerable source) - { - CheckNotNull(source, "source"); - - long sum = 0; - long count = 0; - - foreach (var num in source.Where(n => n != null)) - checked - { - sum += (int) num; - count++; - } - - if (count == 0) - return null; - - return (double?) sum / count; - } - - /// - /// Computes the average of a sequence of values - /// that are obtained by invoking a transform function on each - /// element of the input sequence. - /// - - public static double? Average( - this IEnumerable source, - Func selector) - { - return source.Select(selector).Average(); - } - - /// - /// Returns the minimum value in a sequence of nullable - /// values. - /// - - public static int? Min( - this IEnumerable source) - { - CheckNotNull(source, "source"); - - return MinMaxImpl(source.Where(x => x != null), null, (min, x) => min < x); - } - - /// - /// Invokes a transform function on each element of a sequence and - /// returns the minimum nullable value. - /// - - public static int? Min( - this IEnumerable source, - Func selector) - { - return source.Select(selector).Min(); - } - - /// - /// Returns the maximum value in a sequence of nullable - /// values. - /// - - public static int? Max( - this IEnumerable source) - { - CheckNotNull(source, "source"); - - return MinMaxImpl(source.Where(x => x != null), - null, (max, x) => x == null || (max != null && x.Value < max.Value)); - } - - /// - /// Invokes a transform function on each element of a sequence and - /// returns the maximum nullable value. - /// - - public static int? Max( - this IEnumerable source, - Func selector) - { - return source.Select(selector).Max(); - } - - /// - /// Computes the sum of a sequence of nullable values. - /// - - public static long Sum( - this IEnumerable source) - { - CheckNotNull(source, "source"); - - long sum = 0; - foreach (var num in source) - sum = checked(sum + num); - - return sum; - } - - /// - /// Computes the sum of a sequence of nullable - /// values that are obtained by invoking a transform function on - /// each element of the input sequence. - /// - - public static long Sum( - this IEnumerable source, - Func selector) - { - return source.Select(selector).Sum(); - } - - /// - /// Computes the average of a sequence of nullable values. - /// - - public static double Average( - this IEnumerable source) - { - CheckNotNull(source, "source"); - - long sum = 0; - long count = 0; - - foreach (var num in source) - checked - { - sum += (long) num; - count++; - } - - if (count == 0) - throw new InvalidOperationException(); - - return (double) sum / count; - } - - /// - /// Computes the average of a sequence of nullable values - /// that are obtained by invoking a transform function on each - /// element of the input sequence. - /// - - public static double Average( - this IEnumerable source, - Func selector) - { - return source.Select(selector).Average(); - } - - - /// - /// Computes the sum of a sequence of values. - /// - - public static long? Sum( - this IEnumerable source) - { - CheckNotNull(source, "source"); - - long sum = 0; - foreach (var num in source) - sum = checked(sum + (num ?? 0)); - - return sum; - } - - /// - /// Computes the sum of a sequence of - /// values that are obtained by invoking a transform function on - /// each element of the input sequence. - /// - - public static long? Sum( - this IEnumerable source, - Func selector) - { - return source.Select(selector).Sum(); - } - - /// - /// Computes the average of a sequence of values. - /// - - public static double? Average( - this IEnumerable source) - { - CheckNotNull(source, "source"); - - long sum = 0; - long count = 0; - - foreach (var num in source.Where(n => n != null)) - checked - { - sum += (long) num; - count++; - } - - if (count == 0) - return null; - - return (double?) sum / count; - } - - /// - /// Computes the average of a sequence of values - /// that are obtained by invoking a transform function on each - /// element of the input sequence. - /// - - public static double? Average( - this IEnumerable source, - Func selector) - { - return source.Select(selector).Average(); - } - - /// - /// Returns the minimum value in a sequence of nullable - /// values. - /// - - public static long? Min( - this IEnumerable source) - { - CheckNotNull(source, "source"); - - return MinMaxImpl(source.Where(x => x != null), null, (min, x) => min < x); - } - - /// - /// Invokes a transform function on each element of a sequence and - /// returns the minimum nullable value. - /// - - public static long? Min( - this IEnumerable source, - Func selector) - { - return source.Select(selector).Min(); - } - - /// - /// Returns the maximum value in a sequence of nullable - /// values. - /// - - public static long? Max( - this IEnumerable source) - { - CheckNotNull(source, "source"); - - return MinMaxImpl(source.Where(x => x != null), - null, (max, x) => x == null || (max != null && x.Value < max.Value)); - } - - /// - /// Invokes a transform function on each element of a sequence and - /// returns the maximum nullable value. - /// - - public static long? Max( - this IEnumerable source, - Func selector) - { - return source.Select(selector).Max(); - } - - /// - /// Computes the sum of a sequence of nullable values. - /// - - public static float Sum( - this IEnumerable source) - { - CheckNotNull(source, "source"); - - float sum = 0; - foreach (var num in source) - sum = checked(sum + num); - - return sum; - } - - /// - /// Computes the sum of a sequence of nullable - /// values that are obtained by invoking a transform function on - /// each element of the input sequence. - /// - - public static float Sum( - this IEnumerable source, - Func selector) - { - return source.Select(selector).Sum(); - } - - /// - /// Computes the average of a sequence of nullable values. - /// - - public static float Average( - this IEnumerable source) - { - CheckNotNull(source, "source"); - - float sum = 0; - long count = 0; - - foreach (var num in source) - checked - { - sum += (float) num; - count++; - } - - if (count == 0) - throw new InvalidOperationException(); - - return (float) sum / count; - } - - /// - /// Computes the average of a sequence of nullable values - /// that are obtained by invoking a transform function on each - /// element of the input sequence. - /// - - public static float Average( - this IEnumerable source, - Func selector) - { - return source.Select(selector).Average(); - } - - - /// - /// Computes the sum of a sequence of values. - /// - - public static float? Sum( - this IEnumerable source) - { - CheckNotNull(source, "source"); - - float sum = 0; - foreach (var num in source) - sum = checked(sum + (num ?? 0)); - - return sum; - } - - /// - /// Computes the sum of a sequence of - /// values that are obtained by invoking a transform function on - /// each element of the input sequence. - /// - - public static float? Sum( - this IEnumerable source, - Func selector) - { - return source.Select(selector).Sum(); - } - - /// - /// Computes the average of a sequence of values. - /// - - public static float? Average( - this IEnumerable source) - { - CheckNotNull(source, "source"); - - float sum = 0; - long count = 0; - - foreach (var num in source.Where(n => n != null)) - checked - { - sum += (float) num; - count++; - } - - if (count == 0) - return null; - - return (float?) sum / count; - } - - /// - /// Computes the average of a sequence of values - /// that are obtained by invoking a transform function on each - /// element of the input sequence. - /// - - public static float? Average( - this IEnumerable source, - Func selector) - { - return source.Select(selector).Average(); - } - - /// - /// Returns the minimum value in a sequence of nullable - /// values. - /// - - public static float? Min( - this IEnumerable source) - { - CheckNotNull(source, "source"); - - return MinMaxImpl(source.Where(x => x != null), null, (min, x) => min < x); - } - - /// - /// Invokes a transform function on each element of a sequence and - /// returns the minimum nullable value. - /// - - public static float? Min( - this IEnumerable source, - Func selector) - { - return source.Select(selector).Min(); - } - - /// - /// Returns the maximum value in a sequence of nullable - /// values. - /// - - public static float? Max( - this IEnumerable source) - { - CheckNotNull(source, "source"); - - return MinMaxImpl(source.Where(x => x != null), - null, (max, x) => x == null || (max != null && x.Value < max.Value)); - } - - /// - /// Invokes a transform function on each element of a sequence and - /// returns the maximum nullable value. - /// - - public static float? Max( - this IEnumerable source, - Func selector) - { - return source.Select(selector).Max(); - } - - /// - /// Computes the sum of a sequence of nullable values. - /// - - public static double Sum( - this IEnumerable source) - { - CheckNotNull(source, "source"); - - double sum = 0; - foreach (var num in source) - sum = checked(sum + num); - - return sum; - } - - /// - /// Computes the sum of a sequence of nullable - /// values that are obtained by invoking a transform function on - /// each element of the input sequence. - /// - - public static double Sum( - this IEnumerable source, - Func selector) - { - return source.Select(selector).Sum(); - } - - /// - /// Computes the average of a sequence of nullable values. - /// - - public static double Average( - this IEnumerable source) - { - CheckNotNull(source, "source"); - - double sum = 0; - long count = 0; - - foreach (var num in source) - checked - { - sum += (double) num; - count++; - } - - if (count == 0) - throw new InvalidOperationException(); - - return (double) sum / count; - } - - /// - /// Computes the average of a sequence of nullable values - /// that are obtained by invoking a transform function on each - /// element of the input sequence. - /// - - public static double Average( - this IEnumerable source, - Func selector) - { - return source.Select(selector).Average(); - } - - - /// - /// Computes the sum of a sequence of values. - /// - - public static double? Sum( - this IEnumerable source) - { - CheckNotNull(source, "source"); - - double sum = 0; - foreach (var num in source) - sum = checked(sum + (num ?? 0)); - - return sum; - } - - /// - /// Computes the sum of a sequence of - /// values that are obtained by invoking a transform function on - /// each element of the input sequence. - /// - - public static double? Sum( - this IEnumerable source, - Func selector) - { - return source.Select(selector).Sum(); - } - - /// - /// Computes the average of a sequence of values. - /// - - public static double? Average( - this IEnumerable source) - { - CheckNotNull(source, "source"); - - double sum = 0; - long count = 0; - - foreach (var num in source.Where(n => n != null)) - checked - { - sum += (double) num; - count++; - } - - if (count == 0) - return null; - - return (double?) sum / count; - } - - /// - /// Computes the average of a sequence of values - /// that are obtained by invoking a transform function on each - /// element of the input sequence. - /// - - public static double? Average( - this IEnumerable source, - Func selector) - { - return source.Select(selector).Average(); - } - - /// - /// Returns the minimum value in a sequence of nullable - /// values. - /// - - public static double? Min( - this IEnumerable source) - { - CheckNotNull(source, "source"); - - return MinMaxImpl(source.Where(x => x != null), null, (min, x) => min < x); - } - - /// - /// Invokes a transform function on each element of a sequence and - /// returns the minimum nullable value. - /// - - public static double? Min( - this IEnumerable source, - Func selector) - { - return source.Select(selector).Min(); - } - - /// - /// Returns the maximum value in a sequence of nullable - /// values. - /// - - public static double? Max( - this IEnumerable source) - { - CheckNotNull(source, "source"); - - return MinMaxImpl(source.Where(x => x != null), - null, (max, x) => x == null || (max != null && x.Value < max.Value)); - } - - /// - /// Invokes a transform function on each element of a sequence and - /// returns the maximum nullable value. - /// - - public static double? Max( - this IEnumerable source, - Func selector) - { - return source.Select(selector).Max(); - } - - /// - /// Computes the sum of a sequence of nullable values. - /// - - public static decimal Sum( - this IEnumerable source) - { - CheckNotNull(source, "source"); - - decimal sum = 0; - foreach (var num in source) - sum = checked(sum + num); - - return sum; - } - - /// - /// Computes the sum of a sequence of nullable - /// values that are obtained by invoking a transform function on - /// each element of the input sequence. - /// - - public static decimal Sum( - this IEnumerable source, - Func selector) - { - return source.Select(selector).Sum(); - } - - /// - /// Computes the average of a sequence of nullable values. - /// - - public static decimal Average( - this IEnumerable source) - { - CheckNotNull(source, "source"); - - decimal sum = 0; - long count = 0; - - foreach (var num in source) - checked - { - sum += (decimal) num; - count++; - } - - if (count == 0) - throw new InvalidOperationException(); - - return (decimal) sum / count; - } - - /// - /// Computes the average of a sequence of nullable values - /// that are obtained by invoking a transform function on each - /// element of the input sequence. - /// - - public static decimal Average( - this IEnumerable source, - Func selector) - { - return source.Select(selector).Average(); - } - - - /// - /// Computes the sum of a sequence of values. - /// - - public static decimal? Sum( - this IEnumerable source) - { - CheckNotNull(source, "source"); - - decimal sum = 0; - foreach (var num in source) - sum = checked(sum + (num ?? 0)); - - return sum; - } - - /// - /// Computes the sum of a sequence of - /// values that are obtained by invoking a transform function on - /// each element of the input sequence. - /// - - public static decimal? Sum( - this IEnumerable source, - Func selector) - { - return source.Select(selector).Sum(); - } - - /// - /// Computes the average of a sequence of values. - /// - - public static decimal? Average( - this IEnumerable source) - { - CheckNotNull(source, "source"); - - decimal sum = 0; - long count = 0; - - foreach (var num in source.Where(n => n != null)) - checked - { - sum += (decimal) num; - count++; - } - - if (count == 0) - return null; - - return (decimal?) sum / count; - } - - /// - /// Computes the average of a sequence of values - /// that are obtained by invoking a transform function on each - /// element of the input sequence. - /// - - public static decimal? Average( - this IEnumerable source, - Func selector) - { - return source.Select(selector).Average(); - } - - /// - /// Returns the minimum value in a sequence of nullable - /// values. - /// - - public static decimal? Min( - this IEnumerable source) - { - CheckNotNull(source, "source"); - - return MinMaxImpl(source.Where(x => x != null), null, (min, x) => min < x); - } - - /// - /// Invokes a transform function on each element of a sequence and - /// returns the minimum nullable value. - /// - - public static decimal? Min( - this IEnumerable source, - Func selector) - { - return source.Select(selector).Min(); - } - - /// - /// Returns the maximum value in a sequence of nullable - /// values. - /// - - public static decimal? Max( - this IEnumerable source) - { - CheckNotNull(source, "source"); - - return MinMaxImpl(source.Where(x => x != null), - null, (max, x) => x == null || (max != null && x.Value < max.Value)); - } - - /// - /// Invokes a transform function on each element of a sequence and - /// returns the maximum nullable value. - /// - - public static decimal? Max( - this IEnumerable source, - Func selector) - { - return source.Select(selector).Max(); - } - } + public delegate void Action(T1 arg1, T2 arg2, T3 arg3, T4 arg4); } namespace System.Runtime.CompilerServices { - /// - /// This attribute allows us to define extension methods without - /// requiring .NET Framework 3.5. For more information, see the section, - /// Extension Methods in .NET Framework 2.0 Apps, - /// of Basic Instincts: Extension Methods - /// column in MSDN Magazine, - /// issue Nov 2007. - /// - - [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class | AttributeTargets.Assembly)] - internal sealed class ExtensionAttribute : Attribute { } -} - -// $Id: Func.cs 224 2009-10-04 07:13:08Z azizatif $ - -namespace System -{ -#if LINQBRIDGE_LIB - public delegate TResult Func(); - public delegate TResult Func(T a); - public delegate TResult Func(T1 arg1, T2 arg2); - public delegate TResult Func(T1 arg1, T2 arg2, T3 arg3); - public delegate TResult Func(T1 arg1, T2 arg2, T3 arg3, T4 arg4); -#else - delegate TResult Func(); - delegate TResult Func(T a); - delegate TResult Func(T1 arg1, T2 arg2); - delegate TResult Func(T1 arg1, T2 arg2, T3 arg3); - delegate TResult Func(T1 arg1, T2 arg2, T3 arg3, T4 arg4); -#endif -} - -// $Id: IGrouping.cs 225 2009-10-04 07:16:14Z azizatif $ - -namespace System.Linq -{ - #region Imports - - using System.Collections.Generic; - - #endregion - - /// - /// Represents a collection of objects that have a common key. - /// - - partial interface IGrouping : IEnumerable - { - /// - /// Gets the key of the . - /// - - TKey Key { get; } - } -} - -// $Id: ILookup.cs 224 2009-10-04 07:13:08Z azizatif $ - -namespace System.Linq -{ - using System.Collections.Generic; - - /// - /// Defines an indexer, size property, and Boolean search method for - /// data structures that map keys to - /// sequences of values. - /// - - partial interface ILookup : IEnumerable> - { - bool Contains(TKey key); - int Count { get; } - IEnumerable this[TKey key] { get; } - } -} - -// $Id: IOrderedEnumerable.cs 224 2009-10-04 07:13:08Z azizatif $ - -namespace System.Linq -{ - using System.Collections.Generic; - - /// - /// Represents a sorted sequence. - /// - - partial interface IOrderedEnumerable : IEnumerable - { - /// - /// Performs a subsequent ordering on the elements of an - /// according to a key. - /// - - IOrderedEnumerable CreateOrderedEnumerable( - Func keySelector, IComparer comparer, bool descending); - } -} - -// $Id: Lookup.cs 224 2009-10-04 07:13:08Z azizatif $ - -namespace System.Linq -{ - #region Imports - - using System; - using System.Collections; - using System.Collections.Generic; - using IEnumerable=System.Collections.IEnumerable; - - #endregion - - /// - /// Represents a collection of keys each mapped to one or more values. - /// - - internal sealed class Lookup : ILookup - { - private readonly Dictionary> _map; - - internal Lookup(IEqualityComparer comparer) - { - _map = new Dictionary>(comparer); - } - - internal void Add(IGrouping item) - { - _map.Add(item.Key, item); - } - - internal IEnumerable Find(TKey key) - { - IGrouping grouping; - return _map.TryGetValue(key, out grouping) ? grouping : null; - } - - /// - /// Gets the number of key/value collection pairs in the . - /// - - public int Count - { - get { return _map.Count; } - } - - /// - /// Gets the collection of values indexed by the specified key. - /// - - public IEnumerable this[TKey key] - { - get - { - IGrouping result; - return _map.TryGetValue(key, out result) ? result : Enumerable.Empty(); - } - } - - /// - /// Determines whether a specified key is in the . - /// - - public bool Contains(TKey key) - { - return _map.ContainsKey(key); - } - - /// - /// Applies a transform function to each key and its associated - /// values and returns the results. - /// - - public IEnumerable ApplyResultSelector( - Func, TResult> resultSelector) - { - if (resultSelector == null) - throw new ArgumentNullException("resultSelector"); - - foreach (var pair in _map) - yield return resultSelector(pair.Key, pair.Value); - } - - /// - /// Returns a generic enumerator that iterates through the . - /// - - public IEnumerator> GetEnumerator() - { - return _map.Values.GetEnumerator(); - } - - IEnumerator IEnumerable.GetEnumerator() - { - return GetEnumerator(); - } - } -} - -// $Id: OrderedEnumerable.cs 237 2010-01-31 12:20:24Z azizatif $ - -namespace LinqBridge -{ - #region Imports - - using System; - using System.Collections; - using System.Collections.Generic; - using System.Linq; - - #endregion - - internal sealed class OrderedEnumerable : IOrderedEnumerable - { - private readonly IEnumerable _source; - private readonly List> _comparisons; - - public OrderedEnumerable(IEnumerable source, - Func keySelector, IComparer comparer, bool descending) : - this(source, null, keySelector, comparer, descending) {} - - private OrderedEnumerable(IEnumerable source, List> comparisons, - Func keySelector, IComparer comparer, bool descending) - { - if (source == null) throw new ArgumentNullException("source"); - if (keySelector == null) throw new ArgumentNullException("keySelector"); - - _source = source; - - comparer = comparer ?? Comparer.Default; - - if (comparisons == null) - comparisons = new List>(/* capacity */ 4); - - comparisons.Add((x, y) - => (descending ? -1 : 1) * comparer.Compare(keySelector(x), keySelector(y))); - - _comparisons = comparisons; - } - - public IOrderedEnumerable CreateOrderedEnumerable( - Func keySelector, IComparer comparer, bool descending) - { - return new OrderedEnumerable(_source, _comparisons, keySelector, comparer, descending); - } - - public IEnumerator GetEnumerator() - { - // - // We sort using List.Sort, but docs say that it performs an - // unstable sort. LINQ, on the other hand, says OrderBy performs - // a stable sort. So convert the source sequence into a sequence - // of tuples where the second element tags the position of the - // element from the source sequence (First). The position is - // then used as a tie breaker when all keys compare equal, - // thus making the sort stable. - // - - var list = _source.Select(new Func>(TagPosition)).ToList(); - - list.Sort((x, y) => - { - // - // Compare keys from left to right. - // - - var comparisons = _comparisons; - for (var i = 0; i < comparisons.Count; i++) - { - var result = comparisons[i](x.First, y.First); - if (result != 0) - return result; - } - - // - // All keys compared equal so now break the tie by their - // position in the original sequence, making the sort stable. - // - - return x.Second.CompareTo(y.Second); - }); - - return list.Select(new Func, T>(GetFirst)).GetEnumerator(); - - } - - /// - /// See issue #11 - /// for why this method is needed and cannot be expressed as a - /// lambda at the call site. - /// - - private static Tuple TagPosition(T e, int i) - { - return new Tuple(e, i); - } - - /// - /// See issue #11 - /// for why this method is needed and cannot be expressed as a - /// lambda at the call site. - /// - - private static T GetFirst(Tuple pv) - { - return pv.First; - } - - IEnumerator IEnumerable.GetEnumerator() - { - return GetEnumerator(); - } - } -} - -// $Id: Tuple.cs 215 2009-10-03 13:31:49Z azizatif $ - -namespace LinqBridge -{ - #region Imports - - using System; - using System.Collections.Generic; - using System.Text; - - #endregion - - [ Serializable ] - internal struct Tuple : IEquatable> - { - public TFirst First { get; private set; } - public TSecond Second { get; private set; } - - public Tuple(TFirst first, TSecond second) : this() - { - First = first; - Second = second; - } - - public override bool Equals(object obj) - { - return obj != null - && obj is Tuple - && base.Equals((Tuple) obj); - } - - public bool Equals(Tuple other) - { - return EqualityComparer.Default.Equals(other.First, First) - && EqualityComparer.Default.Equals(other.Second, Second); - } - - public override int GetHashCode() - { - var num = 0x7a2f0b42; - num = (-1521134295 * num) + EqualityComparer.Default.GetHashCode(First); - return (-1521134295 * num) + EqualityComparer.Default.GetHashCode(Second); - } - - public override string ToString() - { - return string.Format(@"{{ First = {0}, Second = {1} }}", First, Second); - } - } -} - -// $Id: Action.cs 239 2010-02-05 23:26:23Z azizatif $ - -namespace System -{ -#if LINQBRIDGE_LIB - public delegate void Action(); - public delegate void Action(T1 arg1, T2 arg2); - public delegate void Action(T1 arg1, T2 arg2, T3 arg3); - public delegate void Action(T1 arg1, T2 arg2, T3 arg3, T4 arg4); -#else - delegate void Action(); - delegate void Action(T1 arg1, T2 arg2); - delegate void Action(T1 arg1, T2 arg2, T3 arg3); - delegate void Action(T1 arg1, T2 arg2, T3 arg3, T4 arg4); -#endif + /// + /// This attribute allows us to define extension methods without + /// requiring .NET Framework 3.5. For more information, see the section, + /// Extension Methods in .NET Framework 2.0 Apps, + /// of Basic Instincts: Extension Methods + /// column in MSDN Magazine, + /// issue Nov 2007. + /// + + [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class | AttributeTargets.Assembly)] + internal sealed class ExtensionAttribute : Attribute { } } #endif \ No newline at end of file diff --git a/Src/Newtonsoft.Json/Utilities/ListWrapper.cs b/Src/Newtonsoft.Json/Utilities/ListWrapper.cs index 79faaf34..2bf04f05 100644 --- a/Src/Newtonsoft.Json/Utilities/ListWrapper.cs +++ b/Src/Newtonsoft.Json/Utilities/ListWrapper.cs @@ -23,13 +23,8 @@ // OTHER DEALINGS IN THE SOFTWARE. #endregion -using System; using System.Collections; using System.Collections.Generic; -using System.Threading; -using Newtonsoft.Json.Utilities; -using System.Linq; -using System.Globalization; namespace Newtonsoft.Json.Utilities { diff --git a/Src/Newtonsoft.Json/Utilities/ReflectionDelegateFactory.cs b/Src/Newtonsoft.Json/Utilities/ReflectionDelegateFactory.cs index 1fcb762e..7093a4a0 100644 --- a/Src/Newtonsoft.Json/Utilities/ReflectionDelegateFactory.cs +++ b/Src/Newtonsoft.Json/Utilities/ReflectionDelegateFactory.cs @@ -26,6 +26,10 @@ using System; using System.Globalization; using System.Reflection; +using Newtonsoft.Json.Serialization; +#if NET20 +using Newtonsoft.Json.Utilities.LinqBridge; +#endif namespace Newtonsoft.Json.Utilities { diff --git a/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs b/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs index 2764cab6..fbffd121 100644 --- a/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs +++ b/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs @@ -27,16 +27,20 @@ using System; using System.Collections.Generic; using System.Reflection; using System.Collections; -using System.Linq; using System.Globalization; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters; using System.Text; -using System.Text.RegularExpressions; #if NETFX_CORE using IConvertible = Newtonsoft.Json.Utilities.Convertible; using ICustomAttributeProvider = Newtonsoft.Json.Utilities.CustomAttributeProvider; #endif +#if NET20 +using Newtonsoft.Json.Utilities.LinqBridge; +#else +using System.Linq; +#endif +using Newtonsoft.Json.Serialization; namespace Newtonsoft.Json.Utilities { diff --git a/Src/Newtonsoft.Json/Utilities/StringReference.cs b/Src/Newtonsoft.Json/Utilities/StringReference.cs index ddcdff21..3a1b62e1 100644 --- a/Src/Newtonsoft.Json/Utilities/StringReference.cs +++ b/Src/Newtonsoft.Json/Utilities/StringReference.cs @@ -1,9 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace Newtonsoft.Json.Utilities +namespace Newtonsoft.Json.Utilities { internal struct StringReference { diff --git a/Src/Newtonsoft.Json/Utilities/StringUtils.cs b/Src/Newtonsoft.Json/Utilities/StringUtils.cs index 91cd3c83..9ebf389a 100644 --- a/Src/Newtonsoft.Json/Utilities/StringUtils.cs +++ b/Src/Newtonsoft.Json/Utilities/StringUtils.cs @@ -27,9 +27,13 @@ using System; using System.Collections.Generic; using System.IO; using System.Text; -using System.Text.RegularExpressions; -using System.Linq; using System.Globalization; +#if NET20 +using Newtonsoft.Json.Utilities.LinqBridge; +#else +using System.Linq; +#endif +using Newtonsoft.Json.Serialization; namespace Newtonsoft.Json.Utilities { diff --git a/Src/Newtonsoft.Json/Utilities/ThreadSafeStore.cs b/Src/Newtonsoft.Json/Utilities/ThreadSafeStore.cs index 8d4921a6..85f2b473 100644 --- a/Src/Newtonsoft.Json/Utilities/ThreadSafeStore.cs +++ b/Src/Newtonsoft.Json/Utilities/ThreadSafeStore.cs @@ -1,5 +1,9 @@ using System; using System.Collections.Generic; +#if NET20 +using Newtonsoft.Json.Utilities.LinqBridge; +#endif +using Newtonsoft.Json.Serialization; namespace Newtonsoft.Json.Utilities { diff --git a/Src/Newtonsoft.Json/Utilities/TypeExtensions.cs b/Src/Newtonsoft.Json/Utilities/TypeExtensions.cs index b059a398..f3cb74b6 100644 --- a/Src/Newtonsoft.Json/Utilities/TypeExtensions.cs +++ b/Src/Newtonsoft.Json/Utilities/TypeExtensions.cs @@ -1,8 +1,11 @@ using System; using System.Collections.Generic; -using System.Linq; using System.Reflection; -using System.Text; +#if NET20 +using Newtonsoft.Json.Utilities.LinqBridge; +#else +using System.Linq; +#endif namespace Newtonsoft.Json.Utilities {