From 8471c4ffe1cb800a4afd747b71dbb5ba31d5ca90 Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Sun, 18 Mar 2012 16:24:30 +1300 Subject: -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 --- Src/Newtonsoft.Json.Tests/Bson/BsonReaderTests.cs | 2 - Src/Newtonsoft.Json.Tests/Bson/BsonWriterTests.cs | 6 +- .../Converters/BinaryConverterTests.cs | 2 - .../Converters/CustomCreationConverterTests.cs | 2 - .../Converters/DataSetConverterTests.cs | 3 - .../Converters/DataTableConverterTests.cs | 5 - .../Converters/IsoDateTimeConverterTests.cs | 4 - .../Converters/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 - .../ComponentModel/JPropertyDescriptorTests.cs | 4 - Src/Newtonsoft.Json.Tests/Linq/JArrayTests.cs | 7 +- .../Linq/JConstructorTests.cs | 7 +- Src/Newtonsoft.Json.Tests/Linq/JObjectTests.cs | 23 +- Src/Newtonsoft.Json.Tests/Linq/JPathTests.cs | 12 +- Src/Newtonsoft.Json.Tests/Linq/JPropertyTests.cs | 2 - Src/Newtonsoft.Json.Tests/Linq/JRawTests.cs | 6 +- .../Linq/JTokenEqualityComparerTests.cs | 3 - Src/Newtonsoft.Json.Tests/Linq/JTokenTests.cs | 6 +- Src/Newtonsoft.Json.Tests/Linq/JTokenWriterTest.cs | 4 + Src/Newtonsoft.Json.Tests/Linq/JValueTests.cs | 6 +- Src/Newtonsoft.Json.Tests/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 - .../CamelCasePropertyNamesContractResolverTests.cs | 2 - .../Serialization/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 - .../TestObjects/CircularReferenceWithIdClass.cs | 5 - .../TestObjects/ContentBaseClass.cs | 7 +- .../TestObjects/ContentSubClass.cs | 7 +- .../TestObjects/EmployeeReference.cs | 5 - .../GenericListAndDictionaryInterfaceProperties.cs | 5 +- .../TestObjects/HolderClass.cs | 5 +- .../TestObjects/JaggedArray.cs | 7 +- Src/Newtonsoft.Json.Tests/TestObjects/Movie.cs | 2 - .../TestObjects/PersonError.cs | 2 - .../TestObjects/PersonPropertyClass.cs | 7 +- .../TestObjects/PrivateMembersClass.cs | 5 - ...etizedConstructorRequiringConverterTestClass.cs | 3 - .../PublicParametizedConstructorTestClass.cs | 7 +- ...ConstructorWithNonPropertyParameterTestClass.cs | 7 +- ...rametizedConstructorWithPropertyNameConflict.cs | 3 - .../TestObjects/RequiredMembersClass.cs | 3 - .../TestObjects/SerializationEventTestObject.cs | 2 - Src/Newtonsoft.Json.Tests/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 - Src/Newtonsoft.Json/Converters/RegexConverter.cs | 3 - .../Converters/StringEnumConverter.cs | 6 +- Src/Newtonsoft.Json/Converters/VersionConverter.cs | 3 - Src/Newtonsoft.Json/Converters/XmlNodeConverter.cs | 4 + Src/Newtonsoft.Json/DefaultValueHandling.cs | 3 - Src/Newtonsoft.Json/IJsonLineInfo.cs | 7 +- Src/Newtonsoft.Json/JsonArrayAttribute.cs | 3 - Src/Newtonsoft.Json/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 +- Src/Newtonsoft.Json/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 - Src/Newtonsoft.Json/Newtonsoft.Json.Net20.csproj | 1 + Src/Newtonsoft.Json/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 - Src/Newtonsoft.Json/Properties/AssemblyInfo.cs | 2 +- Src/Newtonsoft.Json/Schema/Extensions.cs | 3 - Src/Newtonsoft.Json/Schema/JsonSchema.cs | 3 - Src/Newtonsoft.Json/Schema/JsonSchemaBuilder.cs | 7 +- Src/Newtonsoft.Json/Schema/JsonSchemaConstants.cs | 5 +- Src/Newtonsoft.Json/Schema/JsonSchemaGenerator.cs | 7 +- Src/Newtonsoft.Json/Schema/JsonSchemaModel.cs | 2 - .../Schema/JsonSchemaModelBuilder.cs | 6 +- Src/Newtonsoft.Json/Schema/JsonSchemaNode.cs | 4 + Src/Newtonsoft.Json/Schema/JsonSchemaResolver.cs | 9 +- Src/Newtonsoft.Json/Schema/JsonSchemaType.cs | 3 - Src/Newtonsoft.Json/Schema/JsonSchemaWriter.cs | 8 +- .../Serialization/CachedAttributeGetter.cs | 3 - .../Serialization/DefaultContractResolver.cs | 18 +- .../Serialization/DefaultReferenceResolver.cs | 4 - .../Serialization/DynamicValueProvider.cs | 4 +- Src/Newtonsoft.Json/Serialization/ErrorContext.cs | 3 - .../Serialization/ErrorEventArgs.cs | 3 - .../Serialization/IContractResolver.cs | 3 - .../Serialization/JsonArrayContract.cs | 6 +- .../Serialization/JsonDictionaryContract.cs | 3 + .../Serialization/JsonFormatterConverter.cs | 3 - .../Serialization/JsonISerializableContract.cs | 4 - .../Serialization/JsonLinqContract.cs | 3 - Src/Newtonsoft.Json/Serialization/JsonProperty.cs | 3 + .../Serialization/JsonSerializerInternalReader.cs | 12 +- .../Serialization/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 - Src/Newtonsoft.Json/Utilities/CollectionUtils.cs | 5 + Src/Newtonsoft.Json/Utilities/CollectionWrapper.cs | 7 +- Src/Newtonsoft.Json/Utilities/ConvertUtils.cs | 7 +- Src/Newtonsoft.Json/Utilities/DateTimeUtils.cs | 3 - Src/Newtonsoft.Json/Utilities/DictionaryWrapper.cs | 7 +- .../Utilities/DynamicReflectionDelegateFactory.cs | 6 +- Src/Newtonsoft.Json/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 | 4964 ++++++++++---------- Src/Newtonsoft.Json/Utilities/ListWrapper.cs | 5 - .../Utilities/ReflectionDelegateFactory.cs | 4 + Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs | 8 +- Src/Newtonsoft.Json/Utilities/StringReference.cs | 7 +- Src/Newtonsoft.Json/Utilities/StringUtils.cs | 8 +- Src/Newtonsoft.Json/Utilities/ThreadSafeStore.cs | 4 + Src/Newtonsoft.Json/Utilities/TypeExtensions.cs | 7 +- 162 files changed, 3027 insertions(+), 3043 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 d99ceca..ad21afc 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 d5c1d99..3018a12 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 86d4dce..ae1d9ed 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 eae51bb..a38e516 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 ba1dad3..7e759ae 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 3b39280..d59db29 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 634ca03..bbd6b32 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 f011126..850d342 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 40d99f8..292437b 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 f5ed3f6..03a486d 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 4a7226c..a7fc126 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 48f0858..146045a 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 2fb8212..261f181 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 fa45495..bf2af7d 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 fabfa6b..4ec070f 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 dc54d4d..2ac3342 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 df79047..1d677ec 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 6ebef28..bb8141d 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 7c43332..e4ad638 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 5790feb..9e8c69a 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 108df02..b4de0d3 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 a188196..d210526 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 d52d4ab..8d84906 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 feb8c6e..e58d930 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 1cf6906..79821c7 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 08f2854..943fef3 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 b637375..f472a52 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 7975b6d..fbf09f3 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 c6bc056..e55896b 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 586f793..fa10be1 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 9791286..50721d0 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 1e29588..7f91c86 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 accc5ed..9806c52 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 35542bc..92ccde9 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 89f26e7..15c010d 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 d3b0d50..26985cc 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 6ab0df5..5f927db 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 e8f1a89..2f038a0 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 b79f137..8ed6121 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 e6012bf..97e82a6 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 5387a78..fda4508 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 27a0aa4..16b2b63 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 be2aeff..c5b2fef 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 90162e9..c40987a 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 0e27200..47c8150 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 bdb4012..acfe149 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 4d4c867..8ceaab0 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 6e505eb..f3090f0 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 b88e2c8..609784d 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 97c8b7a..4dff08c 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 5514778..59c619f 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 14bcd5e..01e10a4 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 9f52e98..2efe77f 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 40dbba2..cf18dfc 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 7a9483b..06e04bf 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 a8d9a85..0ad339a 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 c552d44..88ff087 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 0b94146..194d1ae 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 6cace1f..8273dde 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 57dbb64..694b7a0 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 1baaa7e..76d0c52 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 ce2f5b0..1778a61 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 9289b14..3853ca4 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 f1691ec..754b553 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 d8c267e..2b63f24 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 6da97cb..384f71a 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 a4b0838..d0e3cfa 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 7b5f8ce..de2a3a9 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 7fcf223..b2fd4ee 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 123977b..f561e01 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 e5697c2..ffa9f52 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 8c2f068..e153ffc 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 7ea57ea..7beb3cf 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 02ef524..ac5aa7a 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 137bc64..6697b28 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 67a9b66..ae55156 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 300e5e8..faa3bdf 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 72c265f..42123b4 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 29c38f0..18cc788 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 64a5d02..7720fd5 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 192a506..ef3e07c 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 a83c828..f65193a 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 ae3a8d5..92a48d5 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 98ab579..3036383 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 729d58a..b60596d 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 2bf2af6..cb26d10 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 9882f4f..b0d87d9 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 250bbf9..80c4de9 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 6291e99..d90afac 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 da7d2a0..a42fafe 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 3a9d26d..87a36b6 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 05f54da..41d593f 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 799e411..5bee8b2 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 7d8dd29..d24a746 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 4456818..4c139c3 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 0000000..e72344f --- /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 7e24713..339b7dd 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 35d94ab..47c9c94 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 cedb573..9b0b164 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 32c6bf1..d2dcc36 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 6e5bcf8..1297e46 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 11d7674..19a6859 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 606ae81..59bd227 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 36b4d2b..ec54083 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 4d76b44..bfd00cb 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 3dae2a8..323e2f0 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 e538022..fd8a131 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 5fb317e..cb672da 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 8a5d966..c7a9589 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 c2e9d6e..540fdc8 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 b9f40df..47b1f42 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 a93e8f2..c2534ae 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 5eeb88b..0f34780 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 7a140d6..7b9cc8c 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 0e9eda1..79fee1e 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 6f623e5..181871b 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 0d438b4..17b65db 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 f126b9a..636e8be 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 5f1a23c..30b576d 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 d7c514f..40952ad 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 bf7c1fb..e80aef5 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 eeaa0db..38d0b82 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 255f748..6aca84f 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 ce4ea62..6199e99 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 b1de3f7..3d9b95b 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 ec302bb..1e464d3 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 75cc87b..c7bfff0 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 f4c7c5b..5886a03 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 7545922..4c20192 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 234cca3..8582a3b 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 bf36008..762cbdb 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 49f19b4..a2fee70 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 b8b9a11..403968a 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 1f10088..e44aba1 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 f2240ec..4f0952a 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 7684298..e9f451e 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 03705d7..77e6142 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 b76925e..370828b 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 12e8ea5..180ce08 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 4013cf3..5b2b1de 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 2a80c9c..147af15 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 1a949fe..f681c25 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 197f03a..772937d 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 e667637..53bca72 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 5f5a347..7cd140e 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 52345e4..3a922c2 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 6d43f65..93acf1b 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 e6b9270..7e84040 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 5882bd6..80c837f 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 57e034f..d5e82e2 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 40355f2..1c0f1c5 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 ae385c2..b4252ed 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 1739d12..9806ecf 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 f83e316..653db4b 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 21a3400..25810c1 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 + /// + /// Provides a set of static (Shared in Visual Basic) methods for + /// querying objects that implement . + /// + internal static partial class Enumerable + { + /// + /// Returns the input typed as . + /// + + public static IEnumerable AsEnumerable(IEnumerable source) + { + return source; + } - using System; - using System.Collections; - using System.Collections.Generic; - using System.Diagnostics; - using LinqBridge; + /// + /// Returns an empty that has the + /// specified type argument. + /// - #endregion + public static IEnumerable Empty() + { + return Sequence.Empty; + } /// - /// Provides a set of static (Shared in Visual Basic) methods for - /// querying objects that implement . + /// Converts the elements of an to the + /// specified type. /// - static partial class Enumerable + public static IEnumerable Cast( + this IEnumerable source) { - /// - /// Returns the input typed as . - /// + CheckNotNull(source, "source"); - public static IEnumerable AsEnumerable(IEnumerable source) - { - return source; - } + return CastYield(source); + } - /// - /// Returns an empty that has the - /// specified type argument. - /// + private static IEnumerable CastYield( + IEnumerable source) + { + foreach (var item in source) + yield return (TResult) item; + } - public static IEnumerable Empty() - { - return Sequence.Empty; - } + /// + /// Filters the elements of an based on a specified type. + /// - /// - /// Converts the elements of an to the - /// specified type. - /// + public static IEnumerable OfType( + this IEnumerable source) + { + CheckNotNull(source, "source"); - public static IEnumerable Cast( - this IEnumerable source) - { - CheckNotNull(source, "source"); + return OfTypeYield(source); + } - return CastYield(source); - } + private static IEnumerable OfTypeYield( + IEnumerable source) + { + foreach (var item in source) + if (item is TResult) + yield return (TResult) item; + } - private static IEnumerable CastYield( - IEnumerable source) - { - foreach (var item in source) - 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. - /// - /// Filters the elements of an based on a specified type. - /// + public static IEnumerable Range(int start, int count) + { + if (count < 0) + throw new ArgumentOutOfRangeException("count", count, null); - public static IEnumerable OfType( - this IEnumerable source) - { - CheckNotNull(source, "source"); + var end = (long) start + count; + if (end - 1 >= int.MaxValue) + throw new ArgumentOutOfRangeException("count", count, null); - return OfTypeYield(source); - } + return RangeYield(start, end); + } - private static IEnumerable OfTypeYield( - IEnumerable source) - { - foreach (var item in source) - if (item is TResult) - yield return (TResult) item; - } + private static IEnumerable RangeYield(int start, long end) + { + for (var i = start; i < end; i++) + yield return i; + } - /// - /// 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. + /// + /// Generates a sequence that contains one repeated value. + /// - public static IEnumerable Range(int start, int count) - { - if (count < 0) - throw new ArgumentOutOfRangeException("count", count, null); + public static IEnumerable Repeat(TResult element, 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 RepeatYield(element, count); + } - return RangeYield(start, end); - } + private static IEnumerable RepeatYield(TResult element, int count) + { + for (var i = 0; i < count; i++) + yield return element; + } - private static IEnumerable RangeYield(int start, long end) - { - for (var i = start; i < end; i++) - yield return i; - } + /// + /// Filters a sequence of values based on a predicate. + /// - /// - /// Generates a sequence that contains one repeated value. - /// + public static IEnumerable Where( + this IEnumerable source, + Func predicate) + { + CheckNotNull(predicate, "predicate"); - public static IEnumerable Repeat(TResult element, int count) - { - if (count < 0) throw new ArgumentOutOfRangeException("count", count, null); + return source.Where((item, i) => predicate(item)); + } - return RepeatYield(element, count); - } + /// + /// Filters a sequence of values based on a predicate. + /// Each element's index is used in the logic of the predicate function. + /// - private static IEnumerable RepeatYield(TResult element, int count) - { - for (var i = 0; i < count; i++) - yield return element; - } + public static IEnumerable Where( + this IEnumerable source, + Func predicate) + { + CheckNotNull(source, "source"); + CheckNotNull(predicate, "predicate"); - /// - /// Filters a sequence of values based on a predicate. - /// + return WhereYield(source, predicate); + } - public static IEnumerable Where( - this IEnumerable source, - Func predicate) - { - CheckNotNull(predicate, "predicate"); + private static IEnumerable WhereYield( + IEnumerable source, + Func predicate) + { + var i = 0; + foreach (var item in source) + if (predicate(item, i++)) + yield return item; + } - return source.Where((item, i) => predicate(item)); - } + /// + /// Projects each element of a sequence into a new form. + /// - /// - /// 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 Select( + this IEnumerable source, + Func selector) + { + CheckNotNull(selector, "selector"); - public static IEnumerable Where( - this IEnumerable source, - Func predicate) - { - CheckNotNull(source, "source"); - CheckNotNull(predicate, "predicate"); + return source.Select((item, i) => selector(item)); + } - return WhereYield(source, predicate); - } + /// + /// Projects each element of a sequence into a new form by + /// incorporating the element's index. + /// - private static IEnumerable WhereYield( - IEnumerable source, - Func predicate) - { - var i = 0; - foreach (var item in source) - if (predicate(item, i++)) - yield return item; - } + public static IEnumerable Select( + this IEnumerable source, + Func selector) + { + CheckNotNull(source, "source"); + CheckNotNull(selector, "selector"); - /// - /// Projects each element of a sequence into a new form. - /// + return SelectYield(source, selector); + } - public static IEnumerable Select( - this IEnumerable source, - Func selector) - { - CheckNotNull(selector, "selector"); + private static IEnumerable SelectYield( + IEnumerable source, + Func selector) + { + var i = 0; + foreach (var item in source) + yield return selector(item, i++); + } - return source.Select((item, i) => selector(item)); - } + /// + /// Projects each element of a sequence to an + /// and flattens the resulting sequences into one sequence. + /// - /// - /// Projects each element of a sequence into a new form by - /// incorporating the element's index. - /// + public static IEnumerable SelectMany( + this IEnumerable source, + Func> selector) + { + CheckNotNull(selector, "selector"); - public static IEnumerable Select( - this IEnumerable source, - Func selector) - { - CheckNotNull(source, "source"); - CheckNotNull(selector, "selector"); + return source.SelectMany((item, i) => selector(item)); + } - return SelectYield(source, selector); - } + /// + /// 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. + /// - private static IEnumerable SelectYield( - IEnumerable source, - Func selector) - { - var i = 0; - foreach (var item in source) - yield return selector(item, i++); - } + public static IEnumerable SelectMany( + this IEnumerable source, + Func> selector) + { + CheckNotNull(selector, "selector"); - /// - /// Projects each element of a sequence to an - /// and flattens the resulting sequences into one sequence. - /// + return source.SelectMany(selector, (item, subitem) => subitem); + } - public static IEnumerable SelectMany( - this IEnumerable source, - Func> selector) - { - CheckNotNull(selector, "selector"); + /// + /// 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. + /// - return source.SelectMany((item, i) => selector(item)); - } + public static IEnumerable SelectMany( + this IEnumerable source, + Func> collectionSelector, + Func resultSelector) + { + CheckNotNull(collectionSelector, "collectionSelector"); - /// - /// 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. - /// + return source.SelectMany((item, i) => collectionSelector(item), resultSelector); + } - public static IEnumerable SelectMany( - this IEnumerable source, - Func> selector) - { - CheckNotNull(selector, "selector"); + /// + /// 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. + /// - return source.SelectMany(selector, (item, subitem) => subitem); - } + public static IEnumerable SelectMany( + this IEnumerable source, + Func> collectionSelector, + Func resultSelector) + { + CheckNotNull(source, "source"); + CheckNotNull(collectionSelector, "collectionSelector"); + CheckNotNull(resultSelector, "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. - /// + return SelectManyYield(source, collectionSelector, resultSelector); + } - public static IEnumerable SelectMany( - this IEnumerable source, - Func> collectionSelector, - Func resultSelector) - { - CheckNotNull(collectionSelector, "collectionSelector"); + 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); + } - return source.SelectMany((item, i) => collectionSelector(item), resultSelector); - } + /// + /// Returns elements from a sequence as long as a specified condition is true. + /// - /// - /// 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"); + public static IEnumerable TakeWhile( + this IEnumerable source, + Func predicate) + { + CheckNotNull(predicate, "predicate"); - return SelectManyYield(source, collectionSelector, resultSelector); - } + return source.TakeWhile((item, i) => predicate(item)); + } - 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. + /// The element's index is used in the logic of the predicate function. + /// - /// - /// Returns elements from a sequence as long as a specified condition is true. - /// + public static IEnumerable TakeWhile( + this IEnumerable source, + Func predicate) + { + CheckNotNull(source, "source"); + CheckNotNull(predicate, "predicate"); - public static IEnumerable TakeWhile( - this IEnumerable source, - Func predicate) - { - CheckNotNull(predicate, "predicate"); + return TakeWhileYield(source, predicate); + } - return source.TakeWhile((item, i) => predicate(item)); - } + 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; + } - /// - /// 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. - /// + private static class Futures + { + public static readonly Func Default = () => default(T); + public static readonly Func Undefined = () => { throw new InvalidOperationException(); }; + } - public static IEnumerable TakeWhile( - this IEnumerable source, - Func predicate) - { - CheckNotNull(source, "source"); - CheckNotNull(predicate, "predicate"); + /// + /// Base implementation of First operator. + /// - return TakeWhileYield(source, predicate); - } + private static TSource FirstImpl( + this IEnumerable source, + Func empty) + { + CheckNotNull(source, "source"); + Debug.Assert(empty != null); - 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; - } + var list = source as IList; // optimized case for lists + if (list != null) + return list.Count > 0 ? list[0] : empty(); - private static class Futures - { - public static readonly Func Default = () => default(T); - public static readonly Func Undefined = () => { throw new InvalidOperationException(); }; - } + using (var e = source.GetEnumerator()) // fallback for enumeration + return e.MoveNext() ? e.Current : empty(); + } - /// - /// Base implementation of First operator. - /// - - private static TSource FirstImpl( - this IEnumerable source, - Func empty) - { - CheckNotNull(source, "source"); - Debug.Assert(empty != null); + /// + /// Returns the first element of a sequence. + /// - var list = source as IList; // optimized case for lists - if (list != null) - return list.Count > 0 ? list[0] : empty(); + public static TSource First( + this IEnumerable source) + { + return source.FirstImpl(Futures.Undefined); + } - using (var e = source.GetEnumerator()) // fallback for enumeration - return e.MoveNext() ? e.Current : empty(); - } + /// + /// Returns the first element in a sequence that satisfies a specified condition. + /// - /// - /// Returns the first element of a sequence. - /// + public static TSource First( + this IEnumerable source, + Func predicate) + { + return First(source.Where(predicate)); + } - public static TSource First( - this IEnumerable source) - { - return source.FirstImpl(Futures.Undefined); - } + /// + /// Returns the first element of a sequence, or a default value if + /// the sequence contains no elements. + /// - /// - /// Returns the first element in a sequence that satisfies a specified condition. - /// + public static TSource FirstOrDefault( + this IEnumerable source) + { + return source.FirstImpl(Futures.Default); + } - public static TSource First( - this IEnumerable source, - Func predicate) - { - return First(source.Where(predicate)); - } + /// + /// Returns the first element of the sequence that satisfies a + /// condition or a default value if no such element is found. + /// - /// - /// Returns the first element of a sequence, or a default value if - /// the sequence contains no elements. - /// + public static TSource FirstOrDefault( + this IEnumerable source, + Func predicate) + { + return FirstOrDefault(source.Where(predicate)); + } - public static TSource FirstOrDefault( - this IEnumerable source) - { - return source.FirstImpl(Futures.Default); - } + /// + /// Base implementation of Last operator. + /// - /// - /// Returns the first element of the sequence that satisfies a - /// condition or a default value if no such element is found. - /// + private static TSource LastImpl( + this IEnumerable source, + Func empty) + { + CheckNotNull(source, "source"); - public static TSource FirstOrDefault( - this IEnumerable source, - Func predicate) - { - return FirstOrDefault(source.Where(predicate)); - } + var list = source as IList; // optimized case for lists + if (list != null) + return list.Count > 0 ? list[list.Count - 1] : empty(); - /// - /// Base implementation of Last operator. - /// + using (var e = source.GetEnumerator()) + { + if (!e.MoveNext()) + return empty(); - private static TSource LastImpl( - this IEnumerable source, - Func empty) - { - CheckNotNull(source, "source"); + var last = e.Current; + while (e.MoveNext()) + last = e.Current; - var list = source as IList; // optimized case for lists - if (list != null) - return list.Count > 0 ? list[list.Count - 1] : empty(); + return last; + } + } - using (var e = source.GetEnumerator()) - { - if (!e.MoveNext()) - return empty(); + /// + /// Returns the last element of a sequence. + /// + public static TSource Last( + this IEnumerable source) + { + return source.LastImpl(Futures.Undefined); + } - var last = e.Current; - while (e.MoveNext()) - last = e.Current; + /// + /// Returns the last element of a sequence that satisfies a + /// specified condition. + /// - return last; - } - } + public static TSource Last( + this IEnumerable source, + Func predicate) + { + return Last(source.Where(predicate)); + } - /// - /// 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, or a default value if + /// the sequence contains no elements. + /// - /// - /// Returns the last element of a sequence that satisfies a - /// specified condition. - /// + public static TSource LastOrDefault( + this IEnumerable source) + { + return source.LastImpl(Futures.Default); + } - public static TSource Last( - this IEnumerable source, - Func predicate) - { - return Last(source.Where(predicate)); - } + /// + /// Returns the last element of a sequence that satisfies a + /// condition or a default value if no such element is found. + /// - /// - /// Returns the last element of a sequence, or a default value if - /// the sequence contains no elements. - /// + public static TSource LastOrDefault( + this IEnumerable source, + Func predicate) + { + return LastOrDefault(source.Where(predicate)); + } - public static TSource LastOrDefault( - this IEnumerable source) - { - return source.LastImpl(Futures.Default); - } + /// + /// Base implementation of Single operator. + /// - /// - /// Returns the last element of a sequence that satisfies a - /// condition or a default value if no such element is found. - /// + private static TSource SingleImpl( + this IEnumerable source, + Func empty) + { + CheckNotNull(source, "source"); - public static TSource LastOrDefault( - this IEnumerable source, - Func predicate) + using (var e = source.GetEnumerator()) + { + if (e.MoveNext()) { - return LastOrDefault(source.Where(predicate)); + var single = e.Current; + if (!e.MoveNext()) + return single; + + throw new InvalidOperationException(); } - /// - /// Base implementation of Single operator. - /// - - private static TSource SingleImpl( - this IEnumerable source, - Func empty) - { - CheckNotNull(source, "source"); + return empty(); + } + } - using (var e = source.GetEnumerator()) - { - if (e.MoveNext()) - { - var single = e.Current; - if (!e.MoveNext()) - return single; + /// + /// Returns the only element of a sequence, and throws an exception + /// if there is not exactly one element in the sequence. + /// - throw new InvalidOperationException(); - } + public static TSource Single( + this IEnumerable source) + { + return source.SingleImpl(Futures.Undefined); + } - return empty(); - } - } + /// + /// Returns the only element of a sequence that satisfies a + /// specified condition, and throws an exception if more than one + /// such element exists. + /// - /// - /// 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, + Func predicate) + { + return Single(source.Where(predicate)); + } - public static TSource Single( - this IEnumerable source) - { - return source.SingleImpl(Futures.Undefined); - } + /// + /// 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. + /// - /// - /// 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 SingleOrDefault( + this IEnumerable source) + { + return source.SingleImpl(Futures.Default); + } - public static TSource Single( - this IEnumerable source, - Func predicate) - { - return Single(source.Where(predicate)); - } + /// + /// 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. + /// - /// - /// 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, + Func predicate) + { + return SingleOrDefault(source.Where(predicate)); + } - public static TSource SingleOrDefault( - this IEnumerable source) - { - return source.SingleImpl(Futures.Default); - } + /// + /// Returns the element at a specified index in a sequence. + /// - /// - /// 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 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); + } + } - public static TSource SingleOrDefault( - this IEnumerable source, - Func predicate) - { - return SingleOrDefault(source.Where(predicate)); - } + /// + /// Returns the element at a specified index in a sequence or a + /// default value if the index is out of range. + /// - /// - /// Returns the element at a specified index in a sequence. - /// + public static TSource ElementAtOrDefault( + this IEnumerable source, + int index) + { + CheckNotNull(source, "source"); - 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); - } - } + if (index < 0) + return default(TSource); - /// - /// Returns the element at a specified index in a sequence or a - /// default value if the index is out of range. - /// + var list = source as IList; + if (list != null) + return index < list.Count ? list[index] : default(TSource); - public static TSource ElementAtOrDefault( - this IEnumerable source, - int index) - { - CheckNotNull(source, "source"); + return source.SkipWhile((item, i) => i < index).FirstOrDefault(); + } - if (index < 0) - return default(TSource); + /// + /// Inverts the order of the elements in a sequence. + /// - var list = source as IList; - if (list != null) - return index < list.Count ? list[index] : default(TSource); + public static IEnumerable Reverse( + this IEnumerable source) + { + CheckNotNull(source, "source"); - return source.SkipWhile((item, i) => i < index).FirstOrDefault(); - } + return ReverseYield(source); + } - /// - /// Inverts the order of the elements in a sequence. - /// - - public static IEnumerable Reverse( - this IEnumerable source) - { - CheckNotNull(source, "source"); + private static IEnumerable ReverseYield(IEnumerable source) + { + var stack = new Stack(); + foreach (var item in source) + stack.Push(item); - return ReverseYield(source); - } + foreach (var item in stack) + yield return item; + } - private static IEnumerable ReverseYield(IEnumerable source) - { - var stack = new Stack(); - foreach (var item in source) - stack.Push(item); + /// + /// Returns a specified number of contiguous elements from the start + /// of a sequence. + /// - foreach (var item in stack) - yield return item; - } + public static IEnumerable Take( + this IEnumerable source, + int count) + { + return source.Where((item, i) => i < count); + } - /// - /// Returns a specified number of contiguous elements from the start - /// of a sequence. - /// + /// + /// Bypasses a specified number of elements in a sequence and then + /// returns the remaining elements. + /// - public static IEnumerable Take( - this IEnumerable source, - int count) - { - return source.Where((item, i) => i < count); - } + public static IEnumerable Skip( + 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. - /// + /// + /// Bypasses elements in a sequence as long as a specified condition + /// is true and then returns the remaining elements. + /// - public static IEnumerable Skip( - this IEnumerable source, - int count) - { - return source.Where((item, i) => i >= count); - } + public static IEnumerable SkipWhile( + this IEnumerable source, + Func predicate) + { + CheckNotNull(predicate, "predicate"); - /// - /// Bypasses elements in a sequence as long as a specified condition - /// is true and then returns the remaining elements. - /// + return source.SkipWhile((item, i) => predicate(item)); + } - public static IEnumerable SkipWhile( - this IEnumerable source, - Func predicate) - { - CheckNotNull(predicate, "predicate"); + /// + /// 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. + /// - return source.SkipWhile((item, i) => predicate(item)); - } + public static IEnumerable SkipWhile( + this IEnumerable source, + Func predicate) + { + CheckNotNull(source, "source"); + CheckNotNull(predicate, "predicate"); - /// - /// 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. - /// + return SkipWhileYield(source, predicate); + } - public static IEnumerable SkipWhile( - this IEnumerable source, - Func predicate) + private static IEnumerable SkipWhileYield( + IEnumerable source, + Func predicate) + { + using (var e = source.GetEnumerator()) + { + for (var i = 0;; i++) { - CheckNotNull(source, "source"); - CheckNotNull(predicate, "predicate"); + if (!e.MoveNext()) + yield break; - return SkipWhileYield(source, predicate); + if (!predicate(e.Current, i)) + break; } - private static IEnumerable SkipWhileYield( - IEnumerable source, - Func predicate) + do { - 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()); - } - } + yield return e.Current; + } while (e.MoveNext()); + } + } - /// - /// Returns the number of elements in a sequence. - /// + /// + /// Returns the number of elements in a sequence. + /// - public static int Count( - this IEnumerable source) - { - CheckNotNull(source, "source"); + 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)); - } + 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. - /// + /// + /// 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)); - } + 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. - /// + /// + /// Returns an that represents the total number + /// of elements in a sequence. + /// - public static long LongCount( - this IEnumerable source) - { - CheckNotNull(source, "source"); + 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); - } + 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. - /// + /// + /// 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)); - } + public static long LongCount( + this IEnumerable source, + Func predicate) + { + return LongCount(source.Where(predicate)); + } - /// - /// Concatenates two sequences. - /// + /// + /// Concatenates two sequences. + /// - public static IEnumerable Concat( - this IEnumerable first, - IEnumerable second) - { - CheckNotNull(first, "first"); - CheckNotNull(second, "second"); + public static IEnumerable Concat( + this IEnumerable first, + IEnumerable second) + { + CheckNotNull(first, "first"); + CheckNotNull(second, "second"); - return ConcatYield(first, second); - } + return ConcatYield(first, second); + } - private static IEnumerable ConcatYield( - IEnumerable first, - IEnumerable second) - { - foreach (var item in first) - yield return item; + private static IEnumerable ConcatYield( + IEnumerable first, + IEnumerable second) + { + foreach (var item in first) + yield return item; - foreach (var item in second) - yield return item; - } + foreach (var item in second) + yield return item; + } - /// - /// Creates a from an . - /// + /// + /// Creates a from an . + /// - public static List ToList( - this IEnumerable source) - { - CheckNotNull(source, "source"); + public static List ToList( + this IEnumerable source) + { + CheckNotNull(source, "source"); - return new List(source); - } + return new List(source); + } + + /// + /// Creates an array from an . + /// - /// - /// Creates an array from an . - /// + public static TSource[] ToArray( + this IEnumerable source) + { + return source.ToList().ToArray(); + } - 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. + /// - /// - /// 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); + } - 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. + /// - /// - /// 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"); - public static IEnumerable Distinct( - this IEnumerable source, - IEqualityComparer comparer) - { - CheckNotNull(source, "source"); + return DistinctYield(source, comparer); + } - return DistinctYield(source, comparer); - } + private static IEnumerable DistinctYield( + IEnumerable source, + IEqualityComparer comparer) + { + var set = new Dictionary(comparer); + var gotNull = false; - private static IEnumerable DistinctYield( - IEnumerable source, - IEqualityComparer comparer) + foreach (var item in source) + { + if (item == null) { - 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; - } + if (gotNull) + continue; + gotNull = true; } - - /// - /// Creates a from an - /// according to a specified key - /// selector function. - /// - - public static ILookup ToLookup( - this IEnumerable source, - Func keySelector) + else { - return ToLookup(source, keySelector, e => e, /* comparer */ null); + if (set.ContainsKey(item)) + continue; + set.Add(item, null); } - /// - /// Creates a from an - /// according to a specified key - /// selector function and a key comparer. - /// + yield return item; + } + } - public static ILookup ToLookup( - this IEnumerable source, - Func keySelector, - IEqualityComparer comparer) - { - return ToLookup(source, keySelector, e => e, comparer); - } + /// + /// Creates a from an + /// according to a specified key + /// selector function. + /// - /// - /// Creates a from an - /// according to specified key - /// and element selector functions. - /// + public static ILookup ToLookup( + this IEnumerable source, + Func keySelector) + { + return ToLookup(source, keySelector, e => e, /* comparer */ null); + } - 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 and a key comparer. + /// - /// - /// 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; - } + public static ILookup ToLookup( + this IEnumerable source, + Func keySelector, + IEqualityComparer comparer) + { + return ToLookup(source, keySelector, e => e, comparer); + } - /// - /// Groups the elements of a sequence according to a specified key - /// selector function. - /// + /// + /// Creates a from an + /// according to specified key + /// and element selector functions. + /// - public static IEnumerable> GroupBy( - this IEnumerable source, - Func keySelector) - { - return GroupBy(source, keySelector, /* comparer */ null); - } + public static ILookup ToLookup( + this IEnumerable source, + Func keySelector, + Func elementSelector) + { + return ToLookup(source, keySelector, elementSelector, /* comparer */ null); + } - /// - /// Groups the elements of a sequence according to a specified key - /// selector function and compares the keys by using a specified - /// comparer. - /// + /// + /// Creates a from an + /// according to a specified key + /// selector function, a comparer and an element selector function. + /// - public static IEnumerable> GroupBy( - this IEnumerable source, - Func keySelector, - IEqualityComparer comparer) - { - return GroupBy(source, keySelector, e => e, comparer); - } + 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); - /// - /// 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. - /// + foreach (var item in source) + { + var key = keySelector(item); - public static IEnumerable> GroupBy( - this IEnumerable source, - Func keySelector, - Func elementSelector) + var grouping = (Grouping) lookup.Find(key); + if (grouping == null) { - return GroupBy(source, keySelector, elementSelector, /* comparer */ null); + grouping = new Grouping(key); + lookup.Add(grouping); } - /// - /// 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"); + grouping.Add(elementSelector(item)); + } - return ToLookup(source, keySelector, elementSelector, comparer); - } + return lookup; + } - /// - /// 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. - /// + /// + /// Groups the elements of a sequence according to a specified key + /// selector function. + /// - public static IEnumerable GroupBy( - this IEnumerable source, - Func keySelector, - Func, TResult> resultSelector) - { - return GroupBy(source, keySelector, resultSelector, /* comparer */ null); - } + 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 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"); + /// + /// Groups the elements of a sequence according to a specified key + /// selector function and compares the keys by using a specified + /// comparer. + /// - return ToLookup(source, keySelector, comparer).Select(g => resultSelector(g.Key, g)); - } + 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 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 projects the elements for each group by + /// using a specified function. + /// - /// - /// 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"); + public static IEnumerable> GroupBy( + this IEnumerable source, + Func keySelector, + Func elementSelector) + { + return GroupBy(source, keySelector, elementSelector, /* comparer */ null); + } - return ToLookup(source, keySelector, elementSelector, 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. + /// - /// - /// Applies an accumulator function over a sequence. - /// + public static IEnumerable> GroupBy( + this IEnumerable source, + Func keySelector, + Func elementSelector, + IEqualityComparer comparer) + { + CheckNotNull(source, "source"); + CheckNotNull(keySelector, "keySelector"); + CheckNotNull(elementSelector, "elementSelector"); - public static TSource Aggregate( - this IEnumerable source, - Func func) - { - CheckNotNull(source, "source"); - CheckNotNull(func, "func"); + return ToLookup(source, keySelector, elementSelector, comparer); + } - using (var e = source.GetEnumerator()) - { - if (!e.MoveNext()) - throw new InvalidOperationException(); + /// + /// 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. + /// - return e.Renumerable().Skip(1).Aggregate(e.Current, func); - } - } + public static IEnumerable GroupBy( + this IEnumerable source, + Func keySelector, + Func, TResult> resultSelector) + { + return GroupBy(source, keySelector, resultSelector, /* comparer */ null); + } - /// - /// Applies an accumulator function over a sequence. The specified - /// seed value is used as the initial accumulator value. - /// + /// + /// 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 TAccumulate Aggregate( - this IEnumerable source, - TAccumulate seed, - Func func) - { - return Aggregate(source, seed, func, r => r); - } + public static IEnumerable GroupBy( + this IEnumerable source, + Func keySelector, + Func, TResult> resultSelector, + IEqualityComparer comparer) + { + CheckNotNull(source, "source"); + CheckNotNull(keySelector, "keySelector"); + CheckNotNull(resultSelector, "resultSelector"); - /// - /// 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"); + return ToLookup(source, keySelector, comparer).Select(g => resultSelector(g.Key, g)); + } - var result = seed; + /// + /// 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. + /// - foreach (var item in source) - result = func(result, item); + public static IEnumerable GroupBy( + this IEnumerable source, + Func keySelector, + Func elementSelector, + Func, TResult> resultSelector) + { + return GroupBy(source, keySelector, elementSelector, resultSelector, /* comparer */ null); + } - return resultSelector(result); - } + /// + /// 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. + /// - /// - /// Produces the set union of two sequences by using the default - /// equality comparer. - /// + 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"); - public static IEnumerable Union( - this IEnumerable first, - IEnumerable second) - { - return Union(first, second, /* comparer */ null); - } + return ToLookup(source, keySelector, elementSelector, comparer) + .Select(g => resultSelector(g.Key, g)); + } - /// - /// Produces the set union of two sequences by using a specified - /// . - /// + /// + /// Applies an accumulator function over a sequence. + /// - public static IEnumerable Union( - this IEnumerable first, - IEnumerable second, - IEqualityComparer comparer) - { - return first.Concat(second).Distinct(comparer); - } + public static TSource Aggregate( + this IEnumerable source, + Func func) + { + CheckNotNull(source, "source"); + CheckNotNull(func, "func"); - /// - /// Returns the elements of the specified sequence or the type - /// parameter's default value in a singleton collection if the - /// sequence is empty. - /// + using (var e = source.GetEnumerator()) + { + if (!e.MoveNext()) + throw new InvalidOperationException(); - public static IEnumerable DefaultIfEmpty( - this IEnumerable source) - { - return source.DefaultIfEmpty(default(TSource)); - } + return e.Renumerable().Skip(1).Aggregate(e.Current, func); + } + } - /// - /// 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"); + /// + /// Applies an accumulator function over a sequence. The specified + /// seed value is used as the initial accumulator value. + /// - return DefaultIfEmptyYield(source, defaultValue); - } + public static TAccumulate Aggregate( + this IEnumerable source, + TAccumulate seed, + Func func) + { + return Aggregate(source, seed, func, r => r); + } - 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()); - } - } + /// + /// 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. + /// - /// - /// Determines whether all elements of a sequence satisfy a condition. - /// + public static TResult Aggregate( + this IEnumerable source, + TAccumulate seed, + Func func, + Func resultSelector) + { + CheckNotNull(source, "source"); + CheckNotNull(func, "func"); + CheckNotNull(resultSelector, "resultSelector"); - public static bool All( - this IEnumerable source, - Func predicate) - { - CheckNotNull(source, "source"); - CheckNotNull(predicate, "predicate"); + var result = seed; - foreach (var item in source) - if (!predicate(item)) - return false; + foreach (var item in source) + result = func(result, item); - return true; - } + return resultSelector(result); + } - /// - /// Determines whether a sequence contains any elements. - /// + /// + /// Produces the set union of two sequences by using the default + /// equality comparer. + /// - public static bool Any( - this IEnumerable source) - { - CheckNotNull(source, "source"); + public static IEnumerable Union( + this IEnumerable first, + IEnumerable second) + { + return Union(first, second, /* comparer */ null); + } - using (var e = source.GetEnumerator()) - return e.MoveNext(); - } + /// + /// Produces the set union of two sequences by using a specified + /// . + /// - /// - /// Determines whether any element of a sequence satisfies a - /// condition. - /// + public static IEnumerable Union( + this IEnumerable first, + IEnumerable second, + IEqualityComparer comparer) + { + return first.Concat(second).Distinct(comparer); + } - public static bool Any( - this IEnumerable source, - Func predicate) - { - return source.Where(predicate).Any(); - } + /// + /// Returns the elements of the specified sequence or the type + /// parameter's default value in a singleton collection if the + /// sequence is empty. + /// - /// - /// Determines whether a sequence contains a specified element by - /// using the default equality comparer. - /// + public static IEnumerable DefaultIfEmpty( + this IEnumerable source) + { + return source.DefaultIfEmpty(default(TSource)); + } - public static bool Contains( - this IEnumerable source, - TSource value) - { - return source.Contains(value, /* comparer */ null); - } + /// + /// Returns the elements of the specified sequence or the specified + /// value in a singleton collection if the sequence is empty. + /// - /// - /// Determines whether a sequence contains a specified element by - /// using a specified . - /// + public static IEnumerable DefaultIfEmpty( + this IEnumerable source, + TSource defaultValue) + { + CheckNotNull(source, "source"); - public static bool Contains( - this IEnumerable source, - TSource value, - IEqualityComparer comparer) - { - CheckNotNull(source, "source"); + return DefaultIfEmptyYield(source, defaultValue); + } - if (comparer == null) - { - var collection = source as ICollection; - if (collection != null) - return collection.Contains(value); - } + 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()); + } + } - comparer = comparer ?? EqualityComparer.Default; - return source.Any(item => comparer.Equals(item, value)); - } + /// + /// Determines whether all elements of a sequence satisfy a condition. + /// - /// - /// Determines whether two sequences are equal by comparing the - /// elements by using the default equality comparer for their type. - /// + public static bool All( + this IEnumerable source, + Func predicate) + { + CheckNotNull(source, "source"); + CheckNotNull(predicate, "predicate"); - public static bool SequenceEqual( - this IEnumerable first, - IEnumerable second) - { - return first.SequenceEqual(second, /* comparer */ null); - } + foreach (var item in source) + if (!predicate(item)) + return false; - /// - /// Determines whether two sequences are equal by comparing their - /// elements by using a specified . - /// + return true; + } - public static bool SequenceEqual( - this IEnumerable first, - IEnumerable second, - IEqualityComparer comparer) - { - CheckNotNull(first, "frist"); - CheckNotNull(second, "second"); + /// + /// Determines whether a sequence contains any elements. + /// - comparer = comparer ?? EqualityComparer.Default; + public static bool Any( + this IEnumerable source) + { + CheckNotNull(source, "source"); - using (IEnumerator lhs = first.GetEnumerator(), - rhs = second.GetEnumerator()) - { - do - { - if (!lhs.MoveNext()) - return !rhs.MoveNext(); + using (var e = source.GetEnumerator()) + return e.MoveNext(); + } - if (!rhs.MoveNext()) - return false; - } - while (comparer.Equals(lhs.Current, rhs.Current)); - } + /// + /// Determines whether any element of a sequence satisfies a + /// condition. + /// - return false; - } + public static bool Any( + this IEnumerable source, + Func predicate) + { + return source.Where(predicate).Any(); + } - /// - /// Base implementation for Min/Max operator. - /// + /// + /// Determines whether a sequence contains a specified element by + /// using the default equality comparer. + /// - private static TSource MinMaxImpl( - this IEnumerable source, - Func lesser) - { - CheckNotNull(source, "source"); - Debug.Assert(lesser != null); + public static bool Contains( + this IEnumerable source, + TSource value) + { + return source.Contains(value, /* comparer */ null); + } - return source.Aggregate((a, item) => lesser(a, item) ? a : item); - } + /// + /// Determines whether a sequence contains a specified element by + /// using a specified . + /// - /// - /// Base implementation for Min/Max operator for nullable types. - /// + public static bool Contains( + this IEnumerable source, + TSource value, + IEqualityComparer comparer) + { + CheckNotNull(source, "source"); - private static TSource? MinMaxImpl( - this IEnumerable source, - TSource? seed, Func lesser) where TSource : struct - { - CheckNotNull(source, "source"); - Debug.Assert(lesser != null); + if (comparer == null) + { + var collection = source as ICollection; + if (collection != null) + return collection.Contains(value); + } - return source.Aggregate(seed, (a, item) => lesser(a, item) ? a : item); - // == MinMaxImpl(Repeat(null, 1).Concat(source), lesser); - } + comparer = comparer ?? EqualityComparer.Default; + return source.Any(item => comparer.Equals(item, value)); + } - /// - /// Returns the minimum value in a generic sequence. - /// + /// + /// Determines whether two sequences are equal by comparing the + /// elements by using the default equality comparer for their type. + /// - public static TSource Min( - this IEnumerable source) - { - var comparer = Comparer.Default; - return source.MinMaxImpl((x, y) => comparer.Compare(x, y) < 0); - } + public static bool SequenceEqual( + this IEnumerable first, + IEnumerable second) + { + return first.SequenceEqual(second, /* comparer */ null); + } - /// - /// Invokes a transform function on each element of a generic - /// sequence and returns the minimum resulting value. - /// + /// + /// Determines whether two sequences are equal by comparing their + /// elements by using a specified . + /// - public static TResult Min( - this IEnumerable source, - Func selector) - { - return source.Select(selector).Min(); - } + public static bool SequenceEqual( + this IEnumerable first, + IEnumerable second, + IEqualityComparer comparer) + { + CheckNotNull(first, "frist"); + CheckNotNull(second, "second"); - /// - /// Returns the maximum value in a generic sequence. - /// + comparer = comparer ?? EqualityComparer.Default; - public static TSource Max( - this IEnumerable source) + using (IEnumerator lhs = first.GetEnumerator(), + rhs = second.GetEnumerator()) + { + do { - var comparer = Comparer.Default; - return source.MinMaxImpl((x, y) => comparer.Compare(x, y) > 0); - } + if (!lhs.MoveNext()) + return !rhs.MoveNext(); - /// - /// Invokes a transform function on each element of a generic - /// sequence and returns the maximum resulting value. - /// + if (!rhs.MoveNext()) + return false; + } while (comparer.Equals(lhs.Current, rhs.Current)); + } - public static TResult Max( - this IEnumerable source, - Func selector) - { - return source.Select(selector).Max(); - } + return false; + } - /// - /// 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); + /// + /// Base implementation for Min/Max operator. + /// - do { yield return e.Current; } while (e.MoveNext()); - } + private static TSource MinMaxImpl( + this IEnumerable source, + Func lesser) + { + CheckNotNull(source, "source"); + Debug.Assert(lesser != null); - /// - /// Sorts the elements of a sequence in ascending order according to a key. - /// + return source.Aggregate((a, item) => lesser(a, item) ? a : item); + } - public static IOrderedEnumerable OrderBy( - this IEnumerable source, - Func keySelector) - { - return source.OrderBy(keySelector, /* comparer */ null); - } + /// + /// Base implementation for Min/Max operator for nullable types. + /// - /// - /// Sorts the elements of a sequence in ascending order by using a - /// specified comparer. - /// + private static TSource? MinMaxImpl( + this IEnumerable source, + TSource? seed, Func lesser) where TSource : struct + { + CheckNotNull(source, "source"); + Debug.Assert(lesser != null); - public static IOrderedEnumerable OrderBy( - this IEnumerable source, - Func keySelector, - IComparer comparer) - { - CheckNotNull(source, "source"); - CheckNotNull(keySelector, "keySelector"); + return source.Aggregate(seed, (a, item) => lesser(a, item) ? a : item); + // == MinMaxImpl(Repeat(null, 1).Concat(source), lesser); + } - return new OrderedEnumerable(source, keySelector, comparer, /* descending */ false); - } + /// + /// Returns the minimum value in a generic sequence. + /// - /// - /// Sorts the elements of a sequence in descending order according to a key. - /// + public static TSource Min( + this IEnumerable source) + { + var comparer = Comparer.Default; + return source.MinMaxImpl((x, y) => comparer.Compare(x, y) < 0); + } - public static IOrderedEnumerable OrderByDescending( - this IEnumerable source, - Func keySelector) - { - return source.OrderByDescending(keySelector, /* comparer */ null); - } + /// + /// Invokes a transform function on each element of a generic + /// sequence and returns the minimum resulting value. + /// - /// - /// Sorts the elements of a sequence in descending order by using a - /// specified comparer. - /// + public static TResult Min( + this IEnumerable source, + Func selector) + { + return source.Select(selector).Min(); + } - public static IOrderedEnumerable OrderByDescending( - this IEnumerable source, - Func keySelector, - IComparer comparer) - { - CheckNotNull(source, "source"); - CheckNotNull(source, "keySelector"); + /// + /// Returns the maximum value in a generic sequence. + /// - return new OrderedEnumerable(source, keySelector, comparer, /* descending */ true); - } + public static TSource Max( + this IEnumerable source) + { + var comparer = Comparer.Default; + return source.MinMaxImpl((x, y) => comparer.Compare(x, y) > 0); + } - /// - /// Performs a subsequent ordering of the elements in a sequence in - /// ascending order according to a key. - /// + /// + /// Invokes a transform function on each element of a generic + /// sequence and returns the maximum resulting value. + /// - public static IOrderedEnumerable ThenBy( - this IOrderedEnumerable source, - Func keySelector) - { - return source.ThenBy(keySelector, /* comparer */ null); - } + public static TResult Max( + this IEnumerable source, + Func selector) + { + return source.Select(selector).Max(); + } - /// - /// Performs a subsequent ordering of the elements in a sequence in - /// ascending order by using a specified comparer. - /// + /// + /// 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. + /// - public static IOrderedEnumerable ThenBy( - this IOrderedEnumerable source, - Func keySelector, - IComparer comparer) - { - CheckNotNull(source, "source"); + private static IEnumerable Renumerable(this IEnumerator e) + { + Debug.Assert(e != null); - return source.CreateOrderedEnumerable(keySelector, comparer, /* descending */ false); - } + 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); + } - /// - /// Performs a subsequent ordering of the elements in a sequence in - /// descending order, according to a key. - /// + /// + /// 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 IOrderedEnumerable ThenByDescending( - this IOrderedEnumerable source, - Func keySelector) - { - return source.ThenByDescending(keySelector, /* comparer */ null); - } + 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)])); + } - /// - /// Performs a subsequent ordering of the elements in a sequence in - /// descending order by using a specified comparer. - /// + [DebuggerStepThrough] + private static void CheckNotNull(T value, string name) where T : class + { + if (value == null) + throw new ArgumentNullException(name); + } - public static IOrderedEnumerable ThenByDescending( - this IOrderedEnumerable source, - Func keySelector, - IComparer comparer) - { - CheckNotNull(source, "source"); + private static class Sequence + { + public static readonly IEnumerable Empty = new T[0]; + } - return source.CreateOrderedEnumerable(keySelector, comparer, /* descending */ true); - } + private sealed class Grouping : List, IGrouping + { + internal Grouping(K key) + { + Key = key; + } - /// - /// Base implementation for Intersect and Except operators. - /// + public K Key { get; private set; } + } + } - private static IEnumerable IntersectExceptImpl( - this IEnumerable first, - IEnumerable second, - IEqualityComparer comparer, - bool flag) - { - CheckNotNull(first, "first"); - CheckNotNull(second, "second"); + internal partial class Enumerable + { + /// + /// Computes the sum of a sequence of nullable values. + /// - var keys = new List(); - var flags = new Dictionary(comparer); + public static int Sum( + this IEnumerable source) + { + CheckNotNull(source, "source"); - foreach (var item in first.Where(k => !flags.ContainsKey(k))) - { - flags.Add(item, !flag); - keys.Add(item); - } + int sum = 0; + foreach (var num in source) + sum = checked(sum + num); - foreach (var item in second.Where(flags.ContainsKey)) - flags[item] = flag; + return sum; + } - // - // As per docs, "the marked elements are yielded in the order in - // which they were collected. - // + /// + /// Computes the sum of a sequence of nullable + /// values that are obtained by invoking a transform function on + /// each element of the input sequence. + /// - return keys.Where(item => flags[item]); - } + public static int Sum( + this IEnumerable source, + Func selector) + { + return source.Select(selector).Sum(); + } - /// - /// Produces the set intersection of two sequences by using the - /// default equality comparer to compare values. - /// + /// + /// Computes the average of a sequence of nullable values. + /// - public static IEnumerable Intersect( - this IEnumerable first, - IEnumerable second) - { - return first.Intersect(second, /* comparer */ null); - } + public static double Average( + this IEnumerable source) + { + CheckNotNull(source, "source"); - /// - /// Produces the set intersection of two sequences by using the - /// specified to compare values. - /// + long sum = 0; + long count = 0; - public static IEnumerable Intersect( - this IEnumerable first, - IEnumerable second, - IEqualityComparer comparer) + foreach (var num in source) + checked { - return IntersectExceptImpl(first, second, comparer, /* flag */ true); + sum += (int) num; + count++; } - /// - /// Produces the set difference of two sequences by using the - /// default equality comparer to compare values. - /// + if (count == 0) + throw new InvalidOperationException(); - public static IEnumerable Except( - this IEnumerable first, - IEnumerable second) - { - return first.Except(second, /* comparer */ null); - } + return (double) sum/count; + } - /// - /// Produces the set difference of two sequences by using the - /// specified to compare values. - /// + /// + /// 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 IEnumerable Except( - this IEnumerable first, - IEnumerable second, - IEqualityComparer comparer) - { - return IntersectExceptImpl(first, second, comparer, /* flag */ false); - } + public static double Average( + this IEnumerable source, + Func selector) + { + return source.Select(selector).Average(); + } - /// - /// 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); - } + /// + /// Computes the sum of a sequence of values. + /// - /// - /// Creates a from an - /// according to a specified key - /// selector function and key comparer. - /// + public static int? Sum( + this IEnumerable source) + { + CheckNotNull(source, "source"); - public static Dictionary ToDictionary( - this IEnumerable source, - Func keySelector, - IEqualityComparer comparer) - { - return source.ToDictionary(keySelector, e => e); - } + int sum = 0; + foreach (var num in source) + sum = checked(sum + (num ?? 0)); - /// - /// 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); - } + return sum; + } - /// - /// 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; - } + /// + /// Computes the sum of a sequence of + /// values that are obtained by invoking a transform function on + /// each element of the input sequence. + /// - /// - /// 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); - } + public static int? Sum( + this IEnumerable source, + Func selector) + { + return source.Select(selector).Sum(); + } - /// - /// 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); - } + /// + /// Computes the average of a sequence of values. + /// - /// - /// 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); - } + public static double? Average( + this IEnumerable source) + { + CheckNotNull(source, "source"); - /// - /// 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); - } + long sum = 0; + long count = 0; - private static class Sequence + foreach (var num in source.Where(n => n != null)) + checked { - public static readonly IEnumerable Empty = new T[0]; + sum += (int) num; + count++; } - private sealed class Grouping : List, IGrouping - { - internal Grouping(K key) - { - Key = key; - } + if (count == 0) + return null; - public K Key { get; private set; } - } + return (double?) sum/count; } -} - -// $Id: Enumerable.g.cs 215 2009-10-03 13:31:49Z azizatif $ -namespace System.Linq -{ - #region Imports + /// + /// Computes the average of a sequence of values + /// that are obtained by invoking a transform function on each + /// element of the input sequence. + /// - using System; - using System.Collections.Generic; + public static double? Average( + this IEnumerable source, + Func selector) + { + return source.Select(selector).Average(); + } - #endregion - - // This partial implementation was template-generated: - // Sat, 03 Oct 2009 09:42:39 GMT + /// + /// Returns the minimum value in a sequence of nullable + /// values. + /// - partial class Enumerable + public static int? Min( + this IEnumerable source) { - /// - /// Computes the sum of a sequence of nullable values. - /// + CheckNotNull(source, "source"); - public static int Sum( - this IEnumerable source) - { - CheckNotNull(source, "source"); + return MinMaxImpl(source.Where(x => x != null), null, (min, x) => min < x); + } - int sum = 0; - foreach (var num in source) - sum = checked(sum + num); + /// + /// Invokes a transform function on each element of a sequence and + /// returns the minimum nullable value. + /// - return sum; - } + public static int? Min( + this IEnumerable source, + Func selector) + { + return source.Select(selector).Min(); + } - /// - /// Computes the sum of a sequence of nullable - /// values that are obtained by invoking a transform function on - /// each element of the input sequence. - /// + /// + /// Returns the maximum value in a sequence of nullable + /// values. + /// - 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 int? Max( + this IEnumerable source) + { + CheckNotNull(source, "source"); - public static double Average( - this IEnumerable source) - { - CheckNotNull(source, "source"); + return MinMaxImpl(source.Where(x => x != null), + null, (max, x) => x == null || (max != null && x.Value < max.Value)); + } - long sum = 0; - long count = 0; + /// + /// Invokes a transform function on each element of a sequence and + /// returns the maximum nullable value. + /// - foreach (var num in source) - checked - { - sum += (int) num; - count++; - } + public static int? Max( + this IEnumerable source, + Func selector) + { + return source.Select(selector).Max(); + } - if (count == 0) - throw new InvalidOperationException(); + /// + /// Computes the sum of a sequence of nullable values. + /// - return (double) sum / count; - } + public static long Sum( + this IEnumerable source) + { + CheckNotNull(source, "source"); - /// - /// Computes the average of a sequence of nullable values - /// that are obtained by invoking a transform function on each - /// element of the input sequence. - /// + long sum = 0; + foreach (var num in source) + sum = checked(sum + num); - public static double Average( - this IEnumerable source, - Func selector) - { - return source.Select(selector).Average(); - } - + return sum; + } - /// - /// Computes the sum of a sequence of values. - /// + /// + /// 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) - { - CheckNotNull(source, "source"); + public static long Sum( + this IEnumerable source, + Func selector) + { + return source.Select(selector).Sum(); + } - int sum = 0; - foreach (var num in source) - sum = checked(sum + (num ?? 0)); + /// + /// Computes the average of a sequence of nullable values. + /// - return sum; - } + public static double Average( + this IEnumerable source) + { + CheckNotNull(source, "source"); - /// - /// Computes the sum of a sequence of - /// values that are obtained by invoking a transform function on - /// each element of the input sequence. - /// + long sum = 0; + long count = 0; - public static int? Sum( - this IEnumerable source, - Func selector) + foreach (var num in source) + checked { - return source.Select(selector).Sum(); + sum += (long) num; + count++; } - - /// - /// 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) + throw new InvalidOperationException(); - 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. - /// + return (double) sum/count; + } - 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); - } + /// + /// Computes the average of a sequence of nullable values + /// that are obtained by invoking a transform function on each + /// element of the input sequence. + /// - /// - /// Invokes a transform function on each element of a sequence and - /// returns the minimum nullable value. - /// + public static double Average( + this IEnumerable source, + Func selector) + { + return source.Select(selector).Average(); + } - public static int? Min( - this IEnumerable source, - Func selector) - { - return source.Select(selector).Min(); - } - /// - /// Returns the maximum value in a sequence of nullable - /// values. - /// + /// + /// Computes the sum of a sequence of 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)); - } + public static long? Sum( + this IEnumerable source) + { + CheckNotNull(source, "source"); - /// - /// Invokes a transform function on each element of a sequence and - /// returns the maximum nullable value. - /// + long sum = 0; + foreach (var num in source) + sum = checked(sum + (num ?? 0)); - public static int? Max( - this IEnumerable source, - Func selector) - { - return source.Select(selector).Max(); - } + return sum; + } - /// - /// Computes the sum of a sequence of nullable values. - /// + /// + /// 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) - { - CheckNotNull(source, "source"); + public static long? Sum( + this IEnumerable source, + Func selector) + { + return source.Select(selector).Sum(); + } - long sum = 0; - foreach (var num in source) - sum = checked(sum + num); + /// + /// Computes the average of a sequence of values. + /// - return sum; - } + public static double? Average( + this IEnumerable source) + { + CheckNotNull(source, "source"); - /// - /// Computes the sum of a sequence of nullable - /// values that are obtained by invoking a transform function on - /// each element of the input sequence. - /// + long sum = 0; + long count = 0; - public static long Sum( - this IEnumerable source, - Func selector) + foreach (var num in source.Where(n => n != null)) + checked { - return source.Select(selector).Sum(); + sum += (long) num; + count++; } - - /// - /// Computes the average of a sequence of nullable values. - /// - public static double Average( - this IEnumerable source) - { - CheckNotNull(source, "source"); + if (count == 0) + return null; - long sum = 0; - long count = 0; + return (double?) sum/count; + } - foreach (var num in source) - checked - { - sum += (long) num; - count++; - } + /// + /// Computes the average of a sequence of values + /// that are obtained by invoking a transform function on each + /// element of the input sequence. + /// - if (count == 0) - throw new InvalidOperationException(); + public static double? Average( + this IEnumerable source, + Func selector) + { + return source.Select(selector).Average(); + } - return (double) sum / count; - } + /// + /// Returns the minimum value in a sequence of nullable + /// values. + /// - /// - /// 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 long? Min( + this IEnumerable source) + { + CheckNotNull(source, "source"); - public static double Average( - this IEnumerable source, - Func selector) - { - return source.Select(selector).Average(); - } - + return MinMaxImpl(source.Where(x => x != null), null, (min, x) => min < x); + } - /// - /// Computes the sum of a sequence of values. - /// + /// + /// Invokes a transform function on each element of a sequence and + /// returns the minimum nullable value. + /// - public static long? Sum( - this IEnumerable source) - { - CheckNotNull(source, "source"); + public static long? Min( + this IEnumerable source, + Func selector) + { + return source.Select(selector).Min(); + } - long sum = 0; - foreach (var num in source) - sum = checked(sum + (num ?? 0)); + /// + /// Returns the maximum value in a sequence of nullable + /// values. + /// - return sum; - } + public static long? Max( + this IEnumerable source) + { + CheckNotNull(source, "source"); - /// - /// Computes the sum of a sequence of - /// values that are obtained by invoking a transform function on - /// each element of the input sequence. - /// + return MinMaxImpl(source.Where(x => x != null), + null, (max, x) => x == null || (max != null && x.Value < max.Value)); + } - public static long? Sum( - this IEnumerable source, - Func selector) - { - return source.Select(selector).Sum(); - } - - /// - /// Computes the average of a sequence of values. - /// + /// + /// Invokes a transform function on each element of a sequence and + /// returns the maximum nullable value. + /// - public static double? Average( - this IEnumerable source) - { - CheckNotNull(source, "source"); + public static long? Max( + this IEnumerable source, + Func selector) + { + return source.Select(selector).Max(); + } - long sum = 0; - long count = 0; + /// + /// Computes the sum of a sequence of nullable values. + /// - foreach (var num in source.Where(n => n != null)) - checked - { - sum += (long) num; - count++; - } + public static float Sum( + this IEnumerable source) + { + CheckNotNull(source, "source"); - if (count == 0) - return null; + float sum = 0; + foreach (var num in source) + sum = checked(sum + num); - return (double?) sum / count; - } + return sum; + } - /// - /// Computes the average of a sequence of values - /// that are obtained by invoking a transform function on each - /// element of the input sequence. - /// + /// + /// 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? 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); - } + public static float Sum( + this IEnumerable source, + Func selector) + { + return source.Select(selector).Sum(); + } - /// - /// Invokes a transform function on each element of a sequence and - /// returns the minimum nullable value. - /// + /// + /// Computes the average of a sequence of nullable values. + /// - public static long? Min( - this IEnumerable source, - Func selector) - { - return source.Select(selector).Min(); - } + public static float Average( + this IEnumerable source) + { + CheckNotNull(source, "source"); - /// - /// Returns the maximum value in a sequence of nullable - /// values. - /// + float sum = 0; + long count = 0; - public static long? Max( - this IEnumerable source) + foreach (var num in source) + checked { - CheckNotNull(source, "source"); - - return MinMaxImpl(source.Where(x => x != null), - null, (max, x) => x == null || (max != null && x.Value < max.Value)); + sum += (float) num; + count++; } - /// - /// Invokes a transform function on each element of a sequence and - /// returns the maximum nullable value. - /// + if (count == 0) + throw new InvalidOperationException(); - public static long? Max( - this IEnumerable source, - Func selector) - { - return source.Select(selector).Max(); - } + return (float) sum/count; + } - /// - /// Computes the sum of a sequence of nullable values. - /// + /// + /// 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 Sum( - this IEnumerable source) - { - CheckNotNull(source, "source"); + public static float Average( + this IEnumerable source, + Func selector) + { + return source.Select(selector).Average(); + } - float sum = 0; - foreach (var num in source) - sum = checked(sum + num); - return sum; - } + /// + /// Computes the sum of a sequence of values. + /// - /// - /// 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) + { + CheckNotNull(source, "source"); - public static float Sum( - this IEnumerable source, - Func selector) - { - return source.Select(selector).Sum(); - } - - /// - /// Computes the average of a sequence of nullable values. - /// + float sum = 0; + foreach (var num in source) + sum = checked(sum + (num ?? 0)); - public static float Average( - this IEnumerable source) - { - CheckNotNull(source, "source"); + return sum; + } - float sum = 0; - long count = 0; + /// + /// Computes the sum of a sequence of + /// values that are obtained by invoking a transform function on + /// each element of the input sequence. + /// - foreach (var num in source) - checked - { - sum += (float) num; - count++; - } + public static float? Sum( + this IEnumerable source, + Func selector) + { + return source.Select(selector).Sum(); + } - if (count == 0) - throw new InvalidOperationException(); + /// + /// Computes the average of a sequence of values. + /// - return (float) sum / count; - } + public static float? Average( + this IEnumerable source) + { + CheckNotNull(source, "source"); - /// - /// Computes the average of a sequence of nullable values - /// that are obtained by invoking a transform function on each - /// element of the input sequence. - /// + float sum = 0; + long count = 0; - public static float Average( - this IEnumerable source, - Func selector) + foreach (var num in source.Where(n => n != null)) + checked { - return source.Select(selector).Average(); + sum += (float) num; + count++; } - - /// - /// Computes the sum of a sequence of values. - /// + if (count == 0) + return null; - public static float? Sum( - this IEnumerable source) - { - CheckNotNull(source, "source"); + return (float?) sum/count; + } - float sum = 0; - foreach (var num in source) - sum = checked(sum + (num ?? 0)); + /// + /// Computes the average of a sequence of values + /// that are obtained by invoking a transform function on each + /// element of the input sequence. + /// - return sum; - } + public static float? Average( + this IEnumerable source, + Func selector) + { + return source.Select(selector).Average(); + } - /// - /// Computes the sum of a sequence of - /// values that are obtained by invoking a transform function on - /// each element of the input sequence. - /// + /// + /// Returns the minimum value in a sequence of nullable + /// values. + /// - 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? Min( + this IEnumerable source) + { + CheckNotNull(source, "source"); - public static float? Average( - this IEnumerable source) - { - CheckNotNull(source, "source"); + return MinMaxImpl(source.Where(x => x != null), null, (min, x) => min < x); + } - float sum = 0; - long count = 0; + /// + /// Invokes a transform function on each element of a sequence and + /// returns the minimum nullable value. + /// - foreach (var num in source.Where(n => n != null)) - checked - { - sum += (float) num; - count++; - } + public static float? Min( + this IEnumerable source, + Func selector) + { + return source.Select(selector).Min(); + } - if (count == 0) - return null; + /// + /// Returns the maximum value in a sequence of nullable + /// values. + /// - return (float?) sum / count; - } + public static float? Max( + this IEnumerable source) + { + CheckNotNull(source, "source"); - /// - /// Computes the average of a sequence of values - /// that are obtained by invoking a transform function on each - /// element of the input sequence. - /// + return MinMaxImpl(source.Where(x => x != null), + null, (max, x) => x == null || (max != null && x.Value < max.Value)); + } - 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 maximum nullable value. + /// - /// - /// Invokes a transform function on each element of a sequence and - /// returns the minimum nullable value. - /// + public static float? Max( + this IEnumerable source, + Func selector) + { + return source.Select(selector).Max(); + } - public static float? Min( - this IEnumerable source, - Func selector) - { - return source.Select(selector).Min(); - } + /// + /// Computes the sum of a sequence of nullable values. + /// + + public static double Sum( + this IEnumerable source) + { + CheckNotNull(source, "source"); - /// - /// Returns the maximum value in a sequence of nullable - /// values. - /// + double sum = 0; + foreach (var num in source) + sum = checked(sum + num); - 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)); - } + return sum; + } - /// - /// Invokes a transform function on each element of a sequence and - /// returns the maximum nullable value. - /// + /// + /// 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? Max( - this IEnumerable source, - Func selector) - { - return source.Select(selector).Max(); - } + public static double Sum( + this IEnumerable source, + Func selector) + { + return source.Select(selector).Sum(); + } - /// - /// Computes the sum of a sequence of nullable values. - /// + /// + /// Computes the average of a sequence of nullable values. + /// - public static double Sum( - this IEnumerable source) - { - CheckNotNull(source, "source"); + public static double Average( + this IEnumerable source) + { + CheckNotNull(source, "source"); - double sum = 0; - foreach (var num in source) - sum = checked(sum + num); + double sum = 0; + long count = 0; - return sum; + foreach (var num in source) + checked + { + sum += (double) num; + count++; } - /// - /// Computes the sum of a sequence of nullable - /// values that are obtained by invoking a transform function on - /// each element of the input sequence. - /// + if (count == 0) + throw new InvalidOperationException(); - public static double Sum( - this IEnumerable source, - Func selector) - { - return source.Select(selector).Sum(); - } - - /// - /// Computes the average of a sequence of nullable values. - /// + return (double) sum/count; + } - public static double Average( - this IEnumerable source) - { - CheckNotNull(source, "source"); + /// + /// Computes the average of a sequence of nullable values + /// that are obtained by invoking a transform function on each + /// element of the input sequence. + /// - double sum = 0; - long count = 0; + public static double Average( + this IEnumerable source, + Func selector) + { + return source.Select(selector).Average(); + } - foreach (var num in source) - checked - { - sum += (double) num; - count++; - } - if (count == 0) - throw new InvalidOperationException(); + /// + /// Computes the sum of a sequence of values. + /// - return (double) sum / count; - } + public static double? Sum( + this IEnumerable source) + { + CheckNotNull(source, "source"); - /// - /// Computes the average of a sequence of nullable values - /// that are obtained by invoking a transform function on each - /// element of the input sequence. - /// + double sum = 0; + foreach (var num in source) + sum = checked(sum + (num ?? 0)); - public static double Average( - this IEnumerable source, - Func selector) - { - return source.Select(selector).Average(); - } - + return sum; + } - /// - /// Computes the sum of a sequence of values. - /// + /// + /// 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) - { - CheckNotNull(source, "source"); + public static double? Sum( + this IEnumerable source, + Func selector) + { + return source.Select(selector).Sum(); + } - double sum = 0; - foreach (var num in source) - sum = checked(sum + (num ?? 0)); + /// + /// Computes the average of a sequence of values. + /// - return sum; - } + public static double? Average( + this IEnumerable source) + { + CheckNotNull(source, "source"); - /// - /// Computes the sum of a sequence of - /// values that are obtained by invoking a transform function on - /// each element of the input sequence. - /// + double sum = 0; + long count = 0; - public static double? Sum( - this IEnumerable source, - Func selector) + foreach (var num in source.Where(n => n != null)) + checked { - return source.Select(selector).Sum(); + sum += (double) num; + count++; } - - /// - /// Computes the average of a sequence of values. - /// - - public static double? Average( - this IEnumerable source) - { - CheckNotNull(source, "source"); - double sum = 0; - long count = 0; + if (count == 0) + return null; - foreach (var num in source.Where(n => n != null)) - checked - { - sum += (double) num; - count++; - } - - if (count == 0) - return null; + return (double?) sum/count; + } - 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. + /// - /// - /// 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(); + } - 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); - } + /// + /// Returns the minimum value in a sequence of nullable + /// values. + /// - /// - /// Invokes a transform function on each element of a sequence and - /// returns the minimum nullable value. - /// + public static double? Min( + this IEnumerable source) + { + CheckNotNull(source, "source"); - public static double? Min( - this IEnumerable source, - Func selector) - { - return source.Select(selector).Min(); - } + return MinMaxImpl(source.Where(x => x != null), null, (min, x) => min < x); + } - /// - /// Returns the maximum value in a sequence of nullable - /// values. - /// + /// + /// Invokes a transform function on each element of a sequence and + /// returns the minimum nullable value. + /// - 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)); - } + public static double? Min( + this IEnumerable source, + Func selector) + { + return source.Select(selector).Min(); + } - /// - /// Invokes a transform function on each element of a sequence and - /// returns the maximum nullable value. - /// + /// + /// Returns the maximum value in a sequence of nullable + /// values. + /// - public static double? Max( - this IEnumerable source, - Func selector) - { - return source.Select(selector).Max(); - } + public static double? Max( + this IEnumerable source) + { + CheckNotNull(source, "source"); - /// - /// Computes the sum of a sequence of nullable values. - /// + return MinMaxImpl(source.Where(x => x != null), + null, (max, x) => x == null || (max != null && x.Value < max.Value)); + } - public static decimal Sum( - this IEnumerable source) - { - CheckNotNull(source, "source"); + /// + /// Invokes a transform function on each element of a sequence and + /// returns the maximum nullable value. + /// - decimal sum = 0; - foreach (var num in source) - sum = checked(sum + num); + public static double? Max( + this IEnumerable source, + Func selector) + { + return source.Select(selector).Max(); + } - return sum; - } + /// + /// Computes the sum of a sequence of nullable values. + /// - /// - /// 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) + { + CheckNotNull(source, "source"); - public static decimal Sum( - this IEnumerable source, - Func selector) - { - return source.Select(selector).Sum(); - } - - /// - /// Computes the average of a sequence of nullable values. - /// + decimal sum = 0; + foreach (var num in source) + sum = checked(sum + num); - public static decimal Average( - this IEnumerable source) - { - CheckNotNull(source, "source"); + return sum; + } - decimal sum = 0; - long count = 0; + /// + /// Computes the sum of a sequence of nullable + /// values that are obtained by invoking a transform function on + /// each element of the input sequence. + /// - foreach (var num in source) - checked - { - sum += (decimal) num; - count++; - } + public static decimal Sum( + this IEnumerable source, + Func selector) + { + return source.Select(selector).Sum(); + } - if (count == 0) - throw new InvalidOperationException(); + /// + /// Computes the average of a sequence of nullable values. + /// - return (decimal) sum / count; - } + public static decimal Average( + this IEnumerable source) + { + CheckNotNull(source, "source"); - /// - /// Computes the average of a sequence of nullable values - /// that are obtained by invoking a transform function on each - /// element of the input sequence. - /// + decimal sum = 0; + long count = 0; - public static decimal Average( - this IEnumerable source, - Func selector) + foreach (var num in source) + checked { - return source.Select(selector).Average(); + sum += (decimal) num; + count++; } - - /// - /// Computes the sum of a sequence of values. - /// + if (count == 0) + throw new InvalidOperationException(); - public static decimal? Sum( - this IEnumerable source) - { - CheckNotNull(source, "source"); + return (decimal) sum/count; + } - decimal sum = 0; - foreach (var num in source) - sum = checked(sum + (num ?? 0)); + /// + /// Computes the average of a sequence of nullable values + /// that are obtained by invoking a transform function on each + /// element of the input sequence. + /// - return sum; - } + public static decimal Average( + this IEnumerable source, + Func selector) + { + return source.Select(selector).Average(); + } - /// - /// 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. - /// + /// + /// Computes the sum of a sequence of values. + /// - public static decimal? Average( - this IEnumerable source) - { - CheckNotNull(source, "source"); + public static decimal? Sum( + this IEnumerable source) + { + CheckNotNull(source, "source"); - decimal sum = 0; - long count = 0; + decimal sum = 0; + foreach (var num in source) + sum = checked(sum + (num ?? 0)); - foreach (var num in source.Where(n => n != null)) - checked - { - sum += (decimal) num; - count++; - } + return sum; + } - if (count == 0) - return null; + /// + /// Computes the sum of a sequence of + /// values that are obtained by invoking a transform function on + /// each element of the input sequence. + /// - return (decimal?) sum / count; - } + public static decimal? Sum( + this IEnumerable source, + Func selector) + { + return source.Select(selector).Sum(); + } - /// - /// Computes the average of a sequence of values - /// that are obtained by invoking a transform function on each - /// element of the input sequence. - /// + /// + /// Computes the average of a sequence of values. + /// - 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); - } + public static decimal? Average( + this IEnumerable source) + { + CheckNotNull(source, "source"); - /// - /// Invokes a transform function on each element of a sequence and - /// returns the minimum nullable value. - /// + decimal sum = 0; + long count = 0; - public static decimal? Min( - this IEnumerable source, - Func selector) + foreach (var num in source.Where(n => n != null)) + checked { - return source.Select(selector).Min(); + sum += (decimal) num; + count++; } - /// - /// Returns the maximum value in a sequence of nullable - /// values. - /// + if (count == 0) + return null; - 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)); - } + return (decimal?) sum/count; + } - /// - /// Invokes a transform function on each element of a sequence and - /// returns the maximum nullable value. - /// + /// + /// 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? Max( - this IEnumerable source, - Func selector) - { - return source.Select(selector).Max(); - } + public static decimal? Average( + this IEnumerable source, + Func selector) + { + return source.Select(selector).Average(); } -} -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. - /// + /// + /// Returns the minimum value in a sequence of nullable + /// values. + /// - [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class | AttributeTargets.Assembly)] - internal sealed class ExtensionAttribute : Attribute { } -} + public static decimal? Min( + this IEnumerable source) + { + CheckNotNull(source, "source"); -// $Id: Func.cs 224 2009-10-04 07:13:08Z azizatif $ + return MinMaxImpl(source.Where(x => x != null), null, (min, x) => min < x); + } -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 -} + /// + /// Invokes a transform function on each element of a sequence and + /// returns the minimum nullable value. + /// -// $Id: IGrouping.cs 225 2009-10-04 07:16:14Z azizatif $ + public static decimal? Min( + this IEnumerable source, + Func selector) + { + return source.Select(selector).Min(); + } -namespace System.Linq -{ - #region Imports + /// + /// Returns the maximum value in a sequence of nullable + /// values. + /// - using System.Collections.Generic; + public static decimal? Max( + this IEnumerable source) + { + CheckNotNull(source, "source"); - #endregion + return MinMaxImpl(source.Where(x => x != null), + null, (max, x) => x == null || (max != null && x.Value < max.Value)); + } /// - /// Represents a collection of objects that have a common key. + /// Invokes a transform function on each element of a sequence and + /// returns the maximum nullable value. /// - partial interface IGrouping : IEnumerable + public static decimal? Max( + this IEnumerable source, + Func selector) { - /// - /// Gets the key of the . - /// - - TKey Key { get; } + return source.Select(selector).Max(); } -} - -// $Id: ILookup.cs 224 2009-10-04 07:13:08Z azizatif $ + } -namespace System.Linq -{ - using System.Collections.Generic; + /// + /// 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 + { /// - /// Defines an indexer, size property, and Boolean search method for - /// data structures that map keys to - /// sequences of values. + /// Performs a subsequent ordering on the elements of an + /// according to a key. /// - partial interface ILookup : IEnumerable> + 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) { - bool Contains(TKey key); - int Count { get; } - IEnumerable this[TKey key] { get; } + _map = new Dictionary>(comparer); } -} -// $Id: IOrderedEnumerable.cs 224 2009-10-04 07:13:08Z azizatif $ + internal void Add(IGrouping item) + { + _map.Add(item.Key, item); + } -namespace System.Linq -{ - using System.Collections.Generic; + internal IEnumerable Find(TKey key) + { + IGrouping grouping; + return _map.TryGetValue(key, out grouping) ? grouping : null; + } /// - /// Represents a sorted sequence. + /// Gets the number of key/value collection pairs in the . /// - partial interface IOrderedEnumerable : IEnumerable + public int Count { - /// - /// Performs a subsequent ordering on the elements of an - /// according to a key. - /// - - IOrderedEnumerable CreateOrderedEnumerable( - Func keySelector, IComparer comparer, bool descending); + get { return _map.Count; } } -} - -// $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; + /// + /// Gets the collection of values indexed by the specified key. + /// - #endregion + public IEnumerable this[TKey key] + { + get + { + IGrouping result; + return _map.TryGetValue(key, out result) ? result : Enumerable.Empty(); + } + } /// - /// Represents a collection of keys each mapped to one or more values. + /// Determines whether a specified key is in the . /// - internal sealed class Lookup : ILookup + public bool Contains(TKey key) { - private readonly Dictionary> _map; + return _map.ContainsKey(key); + } - internal Lookup(IEqualityComparer comparer) - { - _map = new Dictionary>(comparer); - } + /// + /// Applies a transform function to each key and its associated + /// values and returns the results. + /// - internal void Add(IGrouping item) - { - _map.Add(item.Key, item); - } + public IEnumerable ApplyResultSelector( + Func, TResult> resultSelector) + { + if (resultSelector == null) + throw new ArgumentNullException("resultSelector"); - internal IEnumerable Find(TKey key) - { - IGrouping grouping; - return _map.TryGetValue(key, out grouping) ? grouping : null; - } + foreach (var pair in _map) + yield return resultSelector(pair.Key, pair.Value); + } - /// - /// Gets the number of key/value collection pairs in the . - /// + /// + /// Returns a generic enumerator that iterates through the . + /// - public int Count - { - get { return _map.Count; } - } + public IEnumerator> GetEnumerator() + { + return _map.Values.GetEnumerator(); + } - /// - /// Gets the collection of values indexed by the specified key. - /// + IEnumerator IEnumerable.GetEnumerator() + { + return GetEnumerator(); + } + } - public IEnumerable this[TKey key] - { - get - { - IGrouping result; - return _map.TryGetValue(key, out result) ? result : Enumerable.Empty(); - } - } + internal sealed class OrderedEnumerable : IOrderedEnumerable + { + private readonly IEnumerable _source; + private readonly List> _comparisons; - /// - /// Determines whether a specified key is in the . - /// + public OrderedEnumerable(IEnumerable source, + Func keySelector, IComparer comparer, bool descending) : + this(source, null, keySelector, comparer, descending) + { + } - public bool Contains(TKey key) - { - return _map.ContainsKey(key); - } + 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"); - /// - /// Applies a transform function to each key and its associated - /// values and returns the results. - /// + _source = source; - 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); - } + comparer = comparer ?? Comparer.Default; - /// - /// Returns a generic enumerator that iterates through the . - /// + if (comparisons == null) + comparisons = new List>( /* capacity */ 4); - public IEnumerator> GetEnumerator() - { - return _map.Values.GetEnumerator(); - } + comparisons.Add((x, y) + => (descending ? -1 : 1)*comparer.Compare(keySelector(x), keySelector(y))); - IEnumerator IEnumerable.GetEnumerator() - { - return GetEnumerator(); - } + _comparisons = comparisons; } -} - -// $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 + public IOrderedEnumerable CreateOrderedEnumerable( + Func keySelector, IComparer comparer, bool descending) { - private readonly IEnumerable _source; - private readonly List> _comparisons; - - public OrderedEnumerable(IEnumerable source, - Func keySelector, IComparer comparer, bool descending) : - this(source, null, keySelector, comparer, descending) {} + return new OrderedEnumerable(_source, _comparisons, 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"); + 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(); - _source = source; - - comparer = comparer ?? Comparer.Default; + } - if (comparisons == null) - comparisons = new List>(/* capacity */ 4); + /// + /// See issue #11 + /// for why this method is needed and cannot be expressed as a + /// lambda at the call site. + /// - comparisons.Add((x, y) - => (descending ? -1 : 1) * comparer.Compare(keySelector(x), keySelector(y))); + private static Tuple TagPosition(T e, int i) + { + return new Tuple(e, i); + } - _comparisons = comparisons; - } + /// + /// See issue #11 + /// for why this method is needed and cannot be expressed as a + /// lambda at the call site. + /// - public IOrderedEnumerable CreateOrderedEnumerable( - Func keySelector, IComparer comparer, bool descending) - { - return new OrderedEnumerable(_source, _comparisons, keySelector, comparer, descending); - } + private static T GetFirst(Tuple pv) + { + return pv.First; + } - 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(); + IEnumerator IEnumerable.GetEnumerator() + { + return GetEnumerator(); + } + } - } + [Serializable] + internal struct Tuple : IEquatable> + { + public TFirst First { get; private set; } + public TSecond Second { get; private set; } - /// - /// See issue #11 - /// for why this method is needed and cannot be expressed as a - /// lambda at the call site. - /// + public Tuple(TFirst first, TSecond second) + : this() + { + First = first; + Second = second; + } - private static Tuple TagPosition(T e, int i) - { - return new Tuple(e, i); - } + public override bool Equals(object obj) + { + return obj != null + && obj is Tuple + && base.Equals((Tuple) obj); + } - /// - /// See issue #11 - /// for why this method is needed and cannot be expressed as a - /// lambda at the call site. - /// + public bool Equals(Tuple other) + { + return EqualityComparer.Default.Equals(other.First, First) + && EqualityComparer.Default.Equals(other.Second, Second); + } - private static T GetFirst(Tuple pv) - { - return pv.First; - } + public override int GetHashCode() + { + var num = 0x7a2f0b42; + num = (-1521134295*num) + EqualityComparer.Default.GetHashCode(First); + return (-1521134295*num) + EqualityComparer.Default.GetHashCode(Second); + } - IEnumerator IEnumerable.GetEnumerator() - { - return GetEnumerator(); - } + public override string ToString() + { + return string.Format(@"{{ First = {0}, Second = {1} }}", First, Second); } + } } -// $Id: Tuple.cs 215 2009-10-03 13:31:49Z azizatif $ - -namespace LinqBridge +namespace Newtonsoft.Json.Serialization { - #region Imports + public delegate TResult Func(); - using System; - using System.Collections.Generic; - using System.Text; + public delegate TResult Func(T a); - #endregion + public delegate TResult Func(T1 arg1, T2 arg2); - [ Serializable ] - internal struct Tuple : IEquatable> - { - public TFirst First { get; private set; } - public TSecond Second { get; private set; } + public delegate TResult Func(T1 arg1, T2 arg2, T3 arg3); - public Tuple(TFirst first, TSecond second) : this() - { - First = first; - Second = second; - } + public delegate TResult Func(T1 arg1, T2 arg2, T3 arg3, T4 arg4); - public override bool Equals(object obj) - { - return obj != null - && obj is Tuple - && base.Equals((Tuple) obj); - } + public delegate void Action(); - public bool Equals(Tuple other) - { - return EqualityComparer.Default.Equals(other.First, First) - && EqualityComparer.Default.Equals(other.Second, Second); - } + public delegate void Action(T1 arg1, T2 arg2); - public override int GetHashCode() - { - var num = 0x7a2f0b42; - num = (-1521134295 * num) + EqualityComparer.Default.GetHashCode(First); - return (-1521134295 * num) + EqualityComparer.Default.GetHashCode(Second); - } + public delegate void Action(T1 arg1, T2 arg2, T3 arg3); - public override string ToString() - { - return string.Format(@"{{ First = {0}, Second = {1} }}", First, Second); - } - } + public delegate void Action(T1 arg1, T2 arg2, T3 arg3, T4 arg4); } -// $Id: Action.cs 239 2010-02-05 23:26:23Z azizatif $ - -namespace System +namespace System.Runtime.CompilerServices { -#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 79faaf3..2bf04f0 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 1fcb762..7093a4a 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 2764cab..fbffd12 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 ddcdff2..3a1b62e 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 91cd3c8..9ebf389 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 8d4921a..85f2b47 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 b059a39..f3cb74b 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 { -- cgit v1.2.3