Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mono/Newtonsoft.Json.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJames Newton-King <james@newtonking.com>2012-03-18 07:24:30 +0400
committerJames Newton-King <james@newtonking.com>2012-03-18 07:24:30 +0400
commit8471c4ffe1cb800a4afd747b71dbb5ba31d5ca90 (patch)
treec497bfaf1258541fd9fc0a0dd0fc358fa4d71c69
parent9c7fb730f2774d116398f0dcc045b0c386b19a6b (diff)
-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
-rw-r--r--Src/Newtonsoft.Json.Tests/Bson/BsonReaderTests.cs2
-rw-r--r--Src/Newtonsoft.Json.Tests/Bson/BsonWriterTests.cs6
-rw-r--r--Src/Newtonsoft.Json.Tests/Converters/BinaryConverterTests.cs2
-rw-r--r--Src/Newtonsoft.Json.Tests/Converters/CustomCreationConverterTests.cs2
-rw-r--r--Src/Newtonsoft.Json.Tests/Converters/DataSetConverterTests.cs3
-rw-r--r--Src/Newtonsoft.Json.Tests/Converters/DataTableConverterTests.cs5
-rw-r--r--Src/Newtonsoft.Json.Tests/Converters/IsoDateTimeConverterTests.cs4
-rw-r--r--Src/Newtonsoft.Json.Tests/Converters/JavaScriptDateTimeConverterTests.cs5
-rw-r--r--Src/Newtonsoft.Json.Tests/Converters/ObjectIdConverterTests.cs7
-rw-r--r--Src/Newtonsoft.Json.Tests/Converters/RegexConverterTests.cs2
-rw-r--r--Src/Newtonsoft.Json.Tests/Converters/StringEnumConverterTests.cs3
-rw-r--r--Src/Newtonsoft.Json.Tests/Converters/VersionConverterTests.cs4
-rw-r--r--Src/Newtonsoft.Json.Tests/ExceptionTests.cs3
-rw-r--r--Src/Newtonsoft.Json.Tests/JsonArrayAttributeTests.cs4
-rw-r--r--Src/Newtonsoft.Json.Tests/JsonValidatingReaderTests.cs4
-rw-r--r--Src/Newtonsoft.Json.Tests/Linq/ComponentModel/BindingTests.cs4
-rw-r--r--Src/Newtonsoft.Json.Tests/Linq/ComponentModel/JPropertyDescriptorTests.cs4
-rw-r--r--Src/Newtonsoft.Json.Tests/Linq/JArrayTests.cs7
-rw-r--r--Src/Newtonsoft.Json.Tests/Linq/JConstructorTests.cs7
-rw-r--r--Src/Newtonsoft.Json.Tests/Linq/JObjectTests.cs23
-rw-r--r--Src/Newtonsoft.Json.Tests/Linq/JPathTests.cs12
-rw-r--r--Src/Newtonsoft.Json.Tests/Linq/JPropertyTests.cs2
-rw-r--r--Src/Newtonsoft.Json.Tests/Linq/JRawTests.cs6
-rw-r--r--Src/Newtonsoft.Json.Tests/Linq/JTokenEqualityComparerTests.cs3
-rw-r--r--Src/Newtonsoft.Json.Tests/Linq/JTokenTests.cs6
-rw-r--r--Src/Newtonsoft.Json.Tests/Linq/JTokenWriterTest.cs4
-rw-r--r--Src/Newtonsoft.Json.Tests/Linq/JValueTests.cs6
-rw-r--r--Src/Newtonsoft.Json.Tests/Linq/LinqToJsonTest.cs12
-rw-r--r--Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs2
-rw-r--r--Src/Newtonsoft.Json.Tests/Schema/ExtensionsTests.cs2
-rw-r--r--Src/Newtonsoft.Json.Tests/Schema/JsonSchemaBuilderTests.cs3
-rw-r--r--Src/Newtonsoft.Json.Tests/Schema/JsonSchemaGeneratorTests.cs6
-rw-r--r--Src/Newtonsoft.Json.Tests/Schema/JsonSchemaModelBuilderTests.cs5
-rw-r--r--Src/Newtonsoft.Json.Tests/Schema/JsonSchemaNodeTests.cs4
-rw-r--r--Src/Newtonsoft.Json.Tests/Schema/JsonSchemaTests.cs3
-rw-r--r--Src/Newtonsoft.Json.Tests/Serialization/CamelCasePropertyNamesContractResolverTests.cs2
-rw-r--r--Src/Newtonsoft.Json.Tests/Serialization/JsonPropertyCollectionTests.cs4
-rw-r--r--Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs87
-rw-r--r--Src/Newtonsoft.Json.Tests/Serialization/PopulateTests.cs3
-rw-r--r--Src/Newtonsoft.Json.Tests/Serialization/PreserveReferencesHandlingTests.cs1
-rw-r--r--Src/Newtonsoft.Json.Tests/Serialization/SerializationErrorHandlingTests.cs3
-rw-r--r--Src/Newtonsoft.Json.Tests/Serialization/SerializationEventAttributeTests.cs5
-rw-r--r--Src/Newtonsoft.Json.Tests/Serialization/TypeNameHandlingTests.cs2
-rw-r--r--Src/Newtonsoft.Json.Tests/TestFixtureBase.cs7
-rw-r--r--Src/Newtonsoft.Json.Tests/TestObjects/Car.cs2
-rw-r--r--Src/Newtonsoft.Json.Tests/TestObjects/CircularReferenceClass.cs5
-rw-r--r--Src/Newtonsoft.Json.Tests/TestObjects/CircularReferenceWithIdClass.cs5
-rw-r--r--Src/Newtonsoft.Json.Tests/TestObjects/ContentBaseClass.cs7
-rw-r--r--Src/Newtonsoft.Json.Tests/TestObjects/ContentSubClass.cs7
-rw-r--r--Src/Newtonsoft.Json.Tests/TestObjects/EmployeeReference.cs5
-rw-r--r--Src/Newtonsoft.Json.Tests/TestObjects/GenericListAndDictionaryInterfaceProperties.cs5
-rw-r--r--Src/Newtonsoft.Json.Tests/TestObjects/HolderClass.cs5
-rw-r--r--Src/Newtonsoft.Json.Tests/TestObjects/JaggedArray.cs7
-rw-r--r--Src/Newtonsoft.Json.Tests/TestObjects/Movie.cs2
-rw-r--r--Src/Newtonsoft.Json.Tests/TestObjects/PersonError.cs2
-rw-r--r--Src/Newtonsoft.Json.Tests/TestObjects/PersonPropertyClass.cs7
-rw-r--r--Src/Newtonsoft.Json.Tests/TestObjects/PrivateMembersClass.cs5
-rw-r--r--Src/Newtonsoft.Json.Tests/TestObjects/PublicParametizedConstructorRequiringConverterTestClass.cs3
-rw-r--r--Src/Newtonsoft.Json.Tests/TestObjects/PublicParametizedConstructorTestClass.cs7
-rw-r--r--Src/Newtonsoft.Json.Tests/TestObjects/PublicParametizedConstructorWithNonPropertyParameterTestClass.cs7
-rw-r--r--Src/Newtonsoft.Json.Tests/TestObjects/PublicParametizedConstructorWithPropertyNameConflict.cs3
-rw-r--r--Src/Newtonsoft.Json.Tests/TestObjects/RequiredMembersClass.cs3
-rw-r--r--Src/Newtonsoft.Json.Tests/TestObjects/SerializationEventTestObject.cs2
-rw-r--r--Src/Newtonsoft.Json.Tests/TestObjects/Shortie.cs8
-rw-r--r--Src/Newtonsoft.Json.Tests/TestObjects/WagePerson.cs7
-rw-r--r--Src/Newtonsoft.Json.Tests/Utilities/DynamicReflectionDelegateFactoryTests.cs6
-rw-r--r--Src/Newtonsoft.Json.Tests/Utilities/ReflectionUtilsTests.cs2
-rw-r--r--Src/Newtonsoft.Json/Bson/BsonBinaryWriter.cs2
-rw-r--r--Src/Newtonsoft.Json/Bson/BsonObjectId.cs3
-rw-r--r--Src/Newtonsoft.Json/ConstructorHandling.cs5
-rw-r--r--Src/Newtonsoft.Json/Converters/BsonObjectIdConverter.cs3
-rw-r--r--Src/Newtonsoft.Json/Converters/DateTimeConverterBase.cs3
-rw-r--r--Src/Newtonsoft.Json/Converters/KeyValuePairConverter.cs2
-rw-r--r--Src/Newtonsoft.Json/Converters/RegexConverter.cs3
-rw-r--r--Src/Newtonsoft.Json/Converters/StringEnumConverter.cs6
-rw-r--r--Src/Newtonsoft.Json/Converters/VersionConverter.cs3
-rw-r--r--Src/Newtonsoft.Json/Converters/XmlNodeConverter.cs4
-rw-r--r--Src/Newtonsoft.Json/DefaultValueHandling.cs3
-rw-r--r--Src/Newtonsoft.Json/IJsonLineInfo.cs7
-rw-r--r--Src/Newtonsoft.Json/JsonArrayAttribute.cs3
-rw-r--r--Src/Newtonsoft.Json/JsonConstructorAttribute.cs3
-rw-r--r--Src/Newtonsoft.Json/JsonContainerAttribute.cs3
-rw-r--r--Src/Newtonsoft.Json/JsonConverterAttribute.cs3
-rw-r--r--Src/Newtonsoft.Json/JsonReader.cs7
-rw-r--r--Src/Newtonsoft.Json/JsonSerializerSettings.cs3
-rw-r--r--Src/Newtonsoft.Json/JsonValidatingReader.cs7
-rw-r--r--Src/Newtonsoft.Json/JsonWriter.cs9
-rw-r--r--Src/Newtonsoft.Json/Linq/Extensions.cs8
-rw-r--r--Src/Newtonsoft.Json/Linq/IJEnumerable.cs5
-rw-r--r--Src/Newtonsoft.Json/Linq/JArray.cs3
-rw-r--r--Src/Newtonsoft.Json/Linq/JConstructor.cs2
-rw-r--r--Src/Newtonsoft.Json/Linq/JContainer.cs9
-rw-r--r--Src/Newtonsoft.Json/Linq/JEnumerable.cs8
-rw-r--r--Src/Newtonsoft.Json/Linq/JObject.cs61
-rw-r--r--Src/Newtonsoft.Json/Linq/JProperty.cs4
-rw-r--r--Src/Newtonsoft.Json/Linq/JPropertyKeyedCollection.cs168
-rw-r--r--Src/Newtonsoft.Json/Linq/JRaw.cs6
-rw-r--r--Src/Newtonsoft.Json/Linq/JToken.cs7
-rw-r--r--Src/Newtonsoft.Json/Linq/JTokenEqualityComparer.cs5
-rw-r--r--Src/Newtonsoft.Json/Linq/JTokenReader.cs4
-rw-r--r--Src/Newtonsoft.Json/Linq/JTokenWriter.cs3
-rw-r--r--Src/Newtonsoft.Json/Linq/JValue.cs3
-rw-r--r--Src/Newtonsoft.Json/MemberSerialization.cs5
-rw-r--r--Src/Newtonsoft.Json/Newtonsoft.Json.Net20.csproj1
-rw-r--r--Src/Newtonsoft.Json/Newtonsoft.Json.Net35.csproj1
-rw-r--r--Src/Newtonsoft.Json/Newtonsoft.Json.Silverlight.csproj1
-rw-r--r--Src/Newtonsoft.Json/Newtonsoft.Json.WindowsPhone.csproj1
-rw-r--r--Src/Newtonsoft.Json/Newtonsoft.Json.csproj1
-rw-r--r--Src/Newtonsoft.Json/NullValueHandling.cs5
-rw-r--r--Src/Newtonsoft.Json/ObjectCreationHandling.cs5
-rw-r--r--Src/Newtonsoft.Json/Properties/AssemblyInfo.cs2
-rw-r--r--Src/Newtonsoft.Json/Schema/Extensions.cs3
-rw-r--r--Src/Newtonsoft.Json/Schema/JsonSchema.cs3
-rw-r--r--Src/Newtonsoft.Json/Schema/JsonSchemaBuilder.cs7
-rw-r--r--Src/Newtonsoft.Json/Schema/JsonSchemaConstants.cs5
-rw-r--r--Src/Newtonsoft.Json/Schema/JsonSchemaGenerator.cs7
-rw-r--r--Src/Newtonsoft.Json/Schema/JsonSchemaModel.cs2
-rw-r--r--Src/Newtonsoft.Json/Schema/JsonSchemaModelBuilder.cs6
-rw-r--r--Src/Newtonsoft.Json/Schema/JsonSchemaNode.cs4
-rw-r--r--Src/Newtonsoft.Json/Schema/JsonSchemaResolver.cs9
-rw-r--r--Src/Newtonsoft.Json/Schema/JsonSchemaType.cs3
-rw-r--r--Src/Newtonsoft.Json/Schema/JsonSchemaWriter.cs8
-rw-r--r--Src/Newtonsoft.Json/Serialization/CachedAttributeGetter.cs3
-rw-r--r--Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs18
-rw-r--r--Src/Newtonsoft.Json/Serialization/DefaultReferenceResolver.cs4
-rw-r--r--Src/Newtonsoft.Json/Serialization/DynamicValueProvider.cs4
-rw-r--r--Src/Newtonsoft.Json/Serialization/ErrorContext.cs3
-rw-r--r--Src/Newtonsoft.Json/Serialization/ErrorEventArgs.cs3
-rw-r--r--Src/Newtonsoft.Json/Serialization/IContractResolver.cs3
-rw-r--r--Src/Newtonsoft.Json/Serialization/JsonArrayContract.cs6
-rw-r--r--Src/Newtonsoft.Json/Serialization/JsonDictionaryContract.cs3
-rw-r--r--Src/Newtonsoft.Json/Serialization/JsonFormatterConverter.cs3
-rw-r--r--Src/Newtonsoft.Json/Serialization/JsonISerializableContract.cs4
-rw-r--r--Src/Newtonsoft.Json/Serialization/JsonLinqContract.cs3
-rw-r--r--Src/Newtonsoft.Json/Serialization/JsonProperty.cs3
-rw-r--r--Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs12
-rw-r--r--Src/Newtonsoft.Json/Serialization/JsonSerializerInternalWriter.cs16
-rw-r--r--Src/Newtonsoft.Json/Serialization/JsonStringContract.cs3
-rw-r--r--Src/Newtonsoft.Json/Serialization/JsonTypeReflector.cs40
-rw-r--r--Src/Newtonsoft.Json/Serialization/ReflectionValueProvider.cs3
-rw-r--r--Src/Newtonsoft.Json/TypeNameHandling.cs3
-rw-r--r--Src/Newtonsoft.Json/Utilities/BidirectionalDictionary.cs2
-rw-r--r--Src/Newtonsoft.Json/Utilities/CollectionUtils.cs5
-rw-r--r--Src/Newtonsoft.Json/Utilities/CollectionWrapper.cs7
-rw-r--r--Src/Newtonsoft.Json/Utilities/ConvertUtils.cs7
-rw-r--r--Src/Newtonsoft.Json/Utilities/DateTimeUtils.cs3
-rw-r--r--Src/Newtonsoft.Json/Utilities/DictionaryWrapper.cs7
-rw-r--r--Src/Newtonsoft.Json/Utilities/DynamicReflectionDelegateFactory.cs6
-rw-r--r--Src/Newtonsoft.Json/Utilities/DynamicWrapper.cs8
-rw-r--r--Src/Newtonsoft.Json/Utilities/EnumUtils.cs4
-rw-r--r--Src/Newtonsoft.Json/Utilities/EnumValue.cs5
-rw-r--r--Src/Newtonsoft.Json/Utilities/EnumValues.cs4
-rw-r--r--Src/Newtonsoft.Json/Utilities/ILGeneratorExtensions.cs3
-rw-r--r--Src/Newtonsoft.Json/Utilities/LateBoundReflectionDelegateFactory.cs5
-rw-r--r--Src/Newtonsoft.Json/Utilities/LinqBridge.cs4936
-rw-r--r--Src/Newtonsoft.Json/Utilities/ListWrapper.cs5
-rw-r--r--Src/Newtonsoft.Json/Utilities/ReflectionDelegateFactory.cs4
-rw-r--r--Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs8
-rw-r--r--Src/Newtonsoft.Json/Utilities/StringReference.cs7
-rw-r--r--Src/Newtonsoft.Json/Utilities/StringUtils.cs8
-rw-r--r--Src/Newtonsoft.Json/Utilities/ThreadSafeStore.cs4
-rw-r--r--Src/Newtonsoft.Json/Utilities/TypeExtensions.cs7
162 files changed, 3013 insertions, 3029 deletions
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,13 +11,16 @@ 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
{
@@ -26,6 +28,19 @@ namespace Newtonsoft.Json.Tests.Linq
public class JObjectTests : TestFixtureBase
{
[Test]
+ public void Keys()
+ {
+ var o = new JObject();
+ var d = (IDictionary<string, JToken>) o;
+
+ Assert.AreEqual(0, d.Keys.Count);
+
+ o["value"] = true;
+
+ Assert.AreEqual(1, d.Keys.Count);
+ }
+
+ [Test]
public void TryGetValue()
{
JObject o = new JObject();
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<ISerializableTestObject>("{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<JsonSerializationException>(
+ @"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<ISerializableTestObject>("{booleanValue:true}");
+ });
+ }
+ finally
+ {
+ JsonTypeReflector.SetFullyTrusted(true);
+ }
+ }
+
+ [Test]
+ public void DeserializeISerializableInPartialTrust()
+ {
+ try
+ {
+ ExceptionAssert.Throws<JsonSerializationException>(
+ @"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
{
/// <summary>
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
{
/// <summary>
/// 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<string, JToken>
- {
- public JPropertKeyedCollection(IEqualityComparer<string> 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<string, JToken> Dictionary
- {
- get { return base.Dictionary; }
- }
- }
-
- private readonly JPropertKeyedCollection _properties = new JPropertKeyedCollection(StringComparer.Ordinal);
+ private readonly JPropertyKeyedCollection _properties = new JPropertyKeyedCollection();
/// <summary>
/// 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
/// <returns>A <see cref="JProperty"/> with the specified name or null.</returns>
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<string, JToken>.ContainsKey(string key)
{
- if (_properties.Dictionary == null)
- return false;
-
- return _properties.Dictionary.ContainsKey(key);
+ return _properties.Contains(key);
}
ICollection<string> IDictionary<string, JToken>.Keys
{
- get { return _properties.Dictionary.Keys; }
+ // todo: make order the collection returned match JObject order
+ get { return _properties.Keys; }
}
/// <summary>
@@ -453,7 +420,11 @@ namespace Newtonsoft.Json.Linq
ICollection<JToken> IDictionary<string, JToken>.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<JToken>
+ {
+ private static readonly IEqualityComparer<string> Comparer = StringComparer.Ordinal;
+
+ private Dictionary<string, JToken> _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<string, JToken>(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<string> Keys
+ {
+ get
+ {
+ EnsureDictionary();
+ return _dictionary.Keys;
+ }
+ }
+
+ public ICollection<JToken> 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
{
/// <summary>
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 @@
<Compile Include="Formatting.cs" />
<Compile Include="JsonConstructorAttribute.cs" />
<Compile Include="JsonPosition.cs" />
+ <Compile Include="Linq\JPropertyKeyedCollection.cs" />
<Compile Include="SerializationBinder.cs" />
<Compile Include="StreamingContext.cs" />
<Compile Include="Utilities\LinqBridge.cs" />
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 @@
<Compile Include="JsonConstructorAttribute.cs" />
<Compile Include="JsonPosition.cs" />
<Compile Include="Linq\JPropertyDescriptor.cs" />
+ <Compile Include="Linq\JPropertyKeyedCollection.cs" />
<Compile Include="SerializationBinder.cs" />
<Compile Include="StreamingContext.cs" />
<Compile Include="Utilities\DynamicProxy.cs" />
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 @@
<Compile Include="Linq\JPath.cs" />
<Compile Include="Linq\JProperty.cs" />
<Compile Include="Linq\JPropertyDescriptor.cs" />
+ <Compile Include="Linq\JPropertyKeyedCollection.cs" />
<Compile Include="Linq\JRaw.cs" />
<Compile Include="Linq\JToken.cs" />
<Compile Include="Linq\JTokenEqualityComparer.cs" />
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 @@
<Compile Include="Linq\JPath.cs" />
<Compile Include="Linq\JProperty.cs" />
<Compile Include="Linq\JPropertyDescriptor.cs" />
+ <Compile Include="Linq\JPropertyKeyedCollection.cs" />
<Compile Include="Linq\JRaw.cs" />
<Compile Include="Linq\JToken.cs" />
<Compile Include="Linq\JTokenEqualityComparer.cs" />
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 @@
<Compile Include="Formatting.cs" />
<Compile Include="JsonConstructorAttribute.cs" />
<Compile Include="JsonPosition.cs" />
+ <Compile Include="Linq\JPropertyKeyedCollection.cs" />
<Compile Include="Utilities\DynamicProxy.cs" />
<Compile Include="Linq\JPath.cs" />
<Compile Include="Linq\JRaw.cs" />
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
{
/// <summary>
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
{
/// <summary>
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<string, JsonSchemaType> JsonSchemaTypeMapping = new Dictionary<string, JsonSchemaType>()
+ public static readonly IDictionary<string, JsonSchemaType> JsonSchemaTypeMapping = new Dictionary<string, JsonSchemaType>
{
{"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
/// </value>
public bool SerializeCompilerGeneratedMembers { get; set; }
+#if !SILVERLIGHT && !PocketPC && !NETFX_CORE
+ /// <summary>
+ /// Gets or sets a value indicating whether to ignore the ISerializable interface when serializing and deserializing types.
+ /// </summary>
+ /// <value>
+ /// <c>true</c> if the ISerializable interface will be ignored when serializing and deserializing types; otherwise, <c>false</c>.
+ /// </value>
+ public bool IgnoreSerializableInterface { get; set; }
+#endif
+
/// <summary>
/// Initializes a new instance of the <see cref="DefaultContractResolver"/> class.
/// </summary>
@@ -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<T> 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
+ /// <summary>
+ /// Provides a set of static (Shared in Visual Basic) methods for
+ /// querying objects that implement <see cref="IEnumerable{T}" />.
+ /// </summary>
+ internal static partial class Enumerable
+ {
+ /// <summary>
+ /// Returns the input typed as <see cref="IEnumerable{T}"/>.
+ /// </summary>
+
+ public static IEnumerable<TSource> AsEnumerable<TSource>(IEnumerable<TSource> source)
+ {
+ return source;
+ }
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Diagnostics;
- using LinqBridge;
+ /// <summary>
+ /// Returns an empty <see cref="IEnumerable{T}"/> that has the
+ /// specified type argument.
+ /// </summary>
- #endregion
+ public static IEnumerable<TResult> Empty<TResult>()
+ {
+ return Sequence<TResult>.Empty;
+ }
/// <summary>
- /// Provides a set of static (Shared in Visual Basic) methods for
- /// querying objects that implement <see cref="IEnumerable{T}" />.
+ /// Converts the elements of an <see cref="IEnumerable"/> to the
+ /// specified type.
/// </summary>
- static partial class Enumerable
+ public static IEnumerable<TResult> Cast<TResult>(
+ this IEnumerable source)
{
- /// <summary>
- /// Returns the input typed as <see cref="IEnumerable{T}"/>.
- /// </summary>
+ CheckNotNull(source, "source");
- public static IEnumerable<TSource> AsEnumerable<TSource>(IEnumerable<TSource> source)
- {
- return source;
- }
+ return CastYield<TResult>(source);
+ }
- /// <summary>
- /// Returns an empty <see cref="IEnumerable{T}"/> that has the
- /// specified type argument.
- /// </summary>
+ private static IEnumerable<TResult> CastYield<TResult>(
+ IEnumerable source)
+ {
+ foreach (var item in source)
+ yield return (TResult) item;
+ }
- public static IEnumerable<TResult> Empty<TResult>()
- {
- return Sequence<TResult>.Empty;
- }
+ /// <summary>
+ /// Filters the elements of an <see cref="IEnumerable"/> based on a specified type.
+ /// </summary>
- /// <summary>
- /// Converts the elements of an <see cref="IEnumerable"/> to the
- /// specified type.
- /// </summary>
+ public static IEnumerable<TResult> OfType<TResult>(
+ this IEnumerable source)
+ {
+ CheckNotNull(source, "source");
- public static IEnumerable<TResult> Cast<TResult>(
- this IEnumerable source)
- {
- CheckNotNull(source, "source");
+ return OfTypeYield<TResult>(source);
+ }
- return CastYield<TResult>(source);
- }
+ private static IEnumerable<TResult> OfTypeYield<TResult>(
+ IEnumerable source)
+ {
+ foreach (var item in source)
+ if (item is TResult)
+ yield return (TResult) item;
+ }
- private static IEnumerable<TResult> CastYield<TResult>(
- IEnumerable source)
- {
- foreach (var item in source)
- yield return (TResult) item;
- }
+ /// <summary>
+ /// Generates a sequence of integral numbers within a specified range.
+ /// </summary>
+ /// <param name="start">The value of the first integer in the sequence.</param>
+ /// <param name="count">The number of sequential integers to generate.</param>
- /// <summary>
- /// Filters the elements of an <see cref="IEnumerable"/> based on a specified type.
- /// </summary>
+ public static IEnumerable<int> Range(int start, int count)
+ {
+ if (count < 0)
+ throw new ArgumentOutOfRangeException("count", count, null);
- public static IEnumerable<TResult> OfType<TResult>(
- this IEnumerable source)
- {
- CheckNotNull(source, "source");
+ var end = (long) start + count;
+ if (end - 1 >= int.MaxValue)
+ throw new ArgumentOutOfRangeException("count", count, null);
- return OfTypeYield<TResult>(source);
- }
+ return RangeYield(start, end);
+ }
- private static IEnumerable<TResult> OfTypeYield<TResult>(
- IEnumerable source)
- {
- foreach (var item in source)
- if (item is TResult)
- yield return (TResult) item;
- }
+ private static IEnumerable<int> RangeYield(int start, long end)
+ {
+ for (var i = start; i < end; i++)
+ yield return i;
+ }
- /// <summary>
- /// Generates a sequence of integral numbers within a specified range.
- /// </summary>
- /// <param name="start">The value of the first integer in the sequence.</param>
- /// <param name="count">The number of sequential integers to generate.</param>
+ /// <summary>
+ /// Generates a sequence that contains one repeated value.
+ /// </summary>
- public static IEnumerable<int> Range(int start, int count)
- {
- if (count < 0)
- throw new ArgumentOutOfRangeException("count", count, null);
+ public static IEnumerable<TResult> Repeat<TResult>(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<TResult> RepeatYield<TResult>(TResult element, int count)
+ {
+ for (var i = 0; i < count; i++)
+ yield return element;
+ }
- private static IEnumerable<int> RangeYield(int start, long end)
- {
- for (var i = start; i < end; i++)
- yield return i;
- }
+ /// <summary>
+ /// Filters a sequence of values based on a predicate.
+ /// </summary>
- /// <summary>
- /// Generates a sequence that contains one repeated value.
- /// </summary>
+ public static IEnumerable<TSource> Where<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, bool> predicate)
+ {
+ CheckNotNull(predicate, "predicate");
- public static IEnumerable<TResult> Repeat<TResult>(TResult element, int count)
- {
- if (count < 0) throw new ArgumentOutOfRangeException("count", count, null);
+ return source.Where((item, i) => predicate(item));
+ }
- return RepeatYield(element, count);
- }
+ /// <summary>
+ /// Filters a sequence of values based on a predicate.
+ /// Each element's index is used in the logic of the predicate function.
+ /// </summary>
- private static IEnumerable<TResult> RepeatYield<TResult>(TResult element, int count)
- {
- for (var i = 0; i < count; i++)
- yield return element;
- }
+ public static IEnumerable<TSource> Where<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, int, bool> predicate)
+ {
+ CheckNotNull(source, "source");
+ CheckNotNull(predicate, "predicate");
- /// <summary>
- /// Filters a sequence of values based on a predicate.
- /// </summary>
+ return WhereYield(source, predicate);
+ }
- public static IEnumerable<TSource> Where<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, bool> predicate)
- {
- CheckNotNull(predicate, "predicate");
+ private static IEnumerable<TSource> WhereYield<TSource>(
+ IEnumerable<TSource> source,
+ Func<TSource, int, bool> predicate)
+ {
+ var i = 0;
+ foreach (var item in source)
+ if (predicate(item, i++))
+ yield return item;
+ }
- return source.Where((item, i) => predicate(item));
- }
+ /// <summary>
+ /// Projects each element of a sequence into a new form.
+ /// </summary>
- /// <summary>
- /// Filters a sequence of values based on a predicate.
- /// Each element's index is used in the logic of the predicate function.
- /// </summary>
+ public static IEnumerable<TResult> Select<TSource, TResult>(
+ this IEnumerable<TSource> source,
+ Func<TSource, TResult> selector)
+ {
+ CheckNotNull(selector, "selector");
- public static IEnumerable<TSource> Where<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, int, bool> predicate)
- {
- CheckNotNull(source, "source");
- CheckNotNull(predicate, "predicate");
+ return source.Select((item, i) => selector(item));
+ }
- return WhereYield(source, predicate);
- }
+ /// <summary>
+ /// Projects each element of a sequence into a new form by
+ /// incorporating the element's index.
+ /// </summary>
- private static IEnumerable<TSource> WhereYield<TSource>(
- IEnumerable<TSource> source,
- Func<TSource, int, bool> predicate)
- {
- var i = 0;
- foreach (var item in source)
- if (predicate(item, i++))
- yield return item;
- }
+ public static IEnumerable<TResult> Select<TSource, TResult>(
+ this IEnumerable<TSource> source,
+ Func<TSource, int, TResult> selector)
+ {
+ CheckNotNull(source, "source");
+ CheckNotNull(selector, "selector");
- /// <summary>
- /// Projects each element of a sequence into a new form.
- /// </summary>
+ return SelectYield(source, selector);
+ }
- public static IEnumerable<TResult> Select<TSource, TResult>(
- this IEnumerable<TSource> source,
- Func<TSource, TResult> selector)
- {
- CheckNotNull(selector, "selector");
+ private static IEnumerable<TResult> SelectYield<TSource, TResult>(
+ IEnumerable<TSource> source,
+ Func<TSource, int, TResult> selector)
+ {
+ var i = 0;
+ foreach (var item in source)
+ yield return selector(item, i++);
+ }
- return source.Select((item, i) => selector(item));
- }
+ /// <summary>
+ /// Projects each element of a sequence to an <see cref="IEnumerable{T}" />
+ /// and flattens the resulting sequences into one sequence.
+ /// </summary>
- /// <summary>
- /// Projects each element of a sequence into a new form by
- /// incorporating the element's index.
- /// </summary>
+ public static IEnumerable<TResult> SelectMany<TSource, TResult>(
+ this IEnumerable<TSource> source,
+ Func<TSource, IEnumerable<TResult>> selector)
+ {
+ CheckNotNull(selector, "selector");
- public static IEnumerable<TResult> Select<TSource, TResult>(
- this IEnumerable<TSource> source,
- Func<TSource, int, TResult> selector)
- {
- CheckNotNull(source, "source");
- CheckNotNull(selector, "selector");
+ return source.SelectMany((item, i) => selector(item));
+ }
- return SelectYield(source, selector);
- }
+ /// <summary>
+ /// Projects each element of a sequence to an <see cref="IEnumerable{T}" />,
+ /// and flattens the resulting sequences into one sequence. The
+ /// index of each source element is used in the projected form of
+ /// that element.
+ /// </summary>
- private static IEnumerable<TResult> SelectYield<TSource, TResult>(
- IEnumerable<TSource> source,
- Func<TSource, int, TResult> selector)
- {
- var i = 0;
- foreach (var item in source)
- yield return selector(item, i++);
- }
+ public static IEnumerable<TResult> SelectMany<TSource, TResult>(
+ this IEnumerable<TSource> source,
+ Func<TSource, int, IEnumerable<TResult>> selector)
+ {
+ CheckNotNull(selector, "selector");
- /// <summary>
- /// Projects each element of a sequence to an <see cref="IEnumerable{T}" />
- /// and flattens the resulting sequences into one sequence.
- /// </summary>
+ return source.SelectMany(selector, (item, subitem) => subitem);
+ }
- public static IEnumerable<TResult> SelectMany<TSource, TResult>(
- this IEnumerable<TSource> source,
- Func<TSource, IEnumerable<TResult>> selector)
- {
- CheckNotNull(selector, "selector");
+ /// <summary>
+ /// Projects each element of a sequence to an <see cref="IEnumerable{T}" />,
+ /// flattens the resulting sequences into one sequence, and invokes
+ /// a result selector function on each element therein.
+ /// </summary>
- return source.SelectMany((item, i) => selector(item));
- }
+ public static IEnumerable<TResult> SelectMany<TSource, TCollection, TResult>(
+ this IEnumerable<TSource> source,
+ Func<TSource, IEnumerable<TCollection>> collectionSelector,
+ Func<TSource, TCollection, TResult> resultSelector)
+ {
+ CheckNotNull(collectionSelector, "collectionSelector");
- /// <summary>
- /// Projects each element of a sequence to an <see cref="IEnumerable{T}" />,
- /// and flattens the resulting sequences into one sequence. The
- /// index of each source element is used in the projected form of
- /// that element.
- /// </summary>
+ return source.SelectMany((item, i) => collectionSelector(item), resultSelector);
+ }
- public static IEnumerable<TResult> SelectMany<TSource, TResult>(
- this IEnumerable<TSource> source,
- Func<TSource, int, IEnumerable<TResult>> selector)
- {
- CheckNotNull(selector, "selector");
+ /// <summary>
+ /// Projects each element of a sequence to an <see cref="IEnumerable{T}" />,
+ /// 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.
+ /// </summary>
- return source.SelectMany(selector, (item, subitem) => subitem);
- }
+ public static IEnumerable<TResult> SelectMany<TSource, TCollection, TResult>(
+ this IEnumerable<TSource> source,
+ Func<TSource, int, IEnumerable<TCollection>> collectionSelector,
+ Func<TSource, TCollection, TResult> resultSelector)
+ {
+ CheckNotNull(source, "source");
+ CheckNotNull(collectionSelector, "collectionSelector");
+ CheckNotNull(resultSelector, "resultSelector");
- /// <summary>
- /// Projects each element of a sequence to an <see cref="IEnumerable{T}" />,
- /// flattens the resulting sequences into one sequence, and invokes
- /// a result selector function on each element therein.
- /// </summary>
+ return SelectManyYield(source, collectionSelector, resultSelector);
+ }
- public static IEnumerable<TResult> SelectMany<TSource, TCollection, TResult>(
- this IEnumerable<TSource> source,
- Func<TSource, IEnumerable<TCollection>> collectionSelector,
- Func<TSource, TCollection, TResult> resultSelector)
- {
- CheckNotNull(collectionSelector, "collectionSelector");
+ private static IEnumerable<TResult> SelectManyYield<TSource, TCollection, TResult>(
+ this IEnumerable<TSource> source,
+ Func<TSource, int, IEnumerable<TCollection>> collectionSelector,
+ Func<TSource, TCollection, TResult> 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);
- }
+ /// <summary>
+ /// Returns elements from a sequence as long as a specified condition is true.
+ /// </summary>
- /// <summary>
- /// Projects each element of a sequence to an <see cref="IEnumerable{T}" />,
- /// 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.
- /// </summary>
-
- public static IEnumerable<TResult> SelectMany<TSource, TCollection, TResult>(
- this IEnumerable<TSource> source,
- Func<TSource, int, IEnumerable<TCollection>> collectionSelector,
- Func<TSource, TCollection, TResult> resultSelector)
- {
- CheckNotNull(source, "source");
- CheckNotNull(collectionSelector, "collectionSelector");
- CheckNotNull(resultSelector, "resultSelector");
+ public static IEnumerable<TSource> TakeWhile<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, bool> predicate)
+ {
+ CheckNotNull(predicate, "predicate");
- return SelectManyYield(source, collectionSelector, resultSelector);
- }
+ return source.TakeWhile((item, i) => predicate(item));
+ }
- private static IEnumerable<TResult> SelectManyYield<TSource, TCollection, TResult>(
- this IEnumerable<TSource> source,
- Func<TSource, int, IEnumerable<TCollection>> collectionSelector,
- Func<TSource, TCollection, TResult> resultSelector)
- {
- var i = 0;
- foreach (var item in source)
- foreach (var subitem in collectionSelector(item, i++))
- yield return resultSelector(item, subitem);
- }
+ /// <summary>
+ /// 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.
+ /// </summary>
- /// <summary>
- /// Returns elements from a sequence as long as a specified condition is true.
- /// </summary>
+ public static IEnumerable<TSource> TakeWhile<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, int, bool> predicate)
+ {
+ CheckNotNull(source, "source");
+ CheckNotNull(predicate, "predicate");
- public static IEnumerable<TSource> TakeWhile<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, bool> predicate)
- {
- CheckNotNull(predicate, "predicate");
+ return TakeWhileYield(source, predicate);
+ }
- return source.TakeWhile((item, i) => predicate(item));
- }
+ private static IEnumerable<TSource> TakeWhileYield<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, int, bool> predicate)
+ {
+ var i = 0;
+ foreach (var item in source)
+ if (predicate(item, i++))
+ yield return item;
+ else
+ break;
+ }
- /// <summary>
- /// 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.
- /// </summary>
+ private static class Futures<T>
+ {
+ public static readonly Func<T> Default = () => default(T);
+ public static readonly Func<T> Undefined = () => { throw new InvalidOperationException(); };
+ }
- public static IEnumerable<TSource> TakeWhile<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, int, bool> predicate)
- {
- CheckNotNull(source, "source");
- CheckNotNull(predicate, "predicate");
+ /// <summary>
+ /// Base implementation of First operator.
+ /// </summary>
- return TakeWhileYield(source, predicate);
- }
+ private static TSource FirstImpl<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource> empty)
+ {
+ CheckNotNull(source, "source");
+ Debug.Assert(empty != null);
- private static IEnumerable<TSource> TakeWhileYield<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, int, bool> predicate)
- {
- var i = 0;
- foreach (var item in source)
- if (predicate(item, i++))
- yield return item;
- else
- break;
- }
+ var list = source as IList<TSource>; // optimized case for lists
+ if (list != null)
+ return list.Count > 0 ? list[0] : empty();
- private static class Futures<T>
- {
- public static readonly Func<T> Default = () => default(T);
- public static readonly Func<T> Undefined = () => { throw new InvalidOperationException(); };
- }
+ using (var e = source.GetEnumerator()) // fallback for enumeration
+ return e.MoveNext() ? e.Current : empty();
+ }
- /// <summary>
- /// Base implementation of First operator.
- /// </summary>
-
- private static TSource FirstImpl<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource> empty)
- {
- CheckNotNull(source, "source");
- Debug.Assert(empty != null);
+ /// <summary>
+ /// Returns the first element of a sequence.
+ /// </summary>
- var list = source as IList<TSource>; // optimized case for lists
- if (list != null)
- return list.Count > 0 ? list[0] : empty();
+ public static TSource First<TSource>(
+ this IEnumerable<TSource> source)
+ {
+ return source.FirstImpl(Futures<TSource>.Undefined);
+ }
- using (var e = source.GetEnumerator()) // fallback for enumeration
- return e.MoveNext() ? e.Current : empty();
- }
+ /// <summary>
+ /// Returns the first element in a sequence that satisfies a specified condition.
+ /// </summary>
- /// <summary>
- /// Returns the first element of a sequence.
- /// </summary>
+ public static TSource First<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, bool> predicate)
+ {
+ return First(source.Where(predicate));
+ }
- public static TSource First<TSource>(
- this IEnumerable<TSource> source)
- {
- return source.FirstImpl(Futures<TSource>.Undefined);
- }
+ /// <summary>
+ /// Returns the first element of a sequence, or a default value if
+ /// the sequence contains no elements.
+ /// </summary>
- /// <summary>
- /// Returns the first element in a sequence that satisfies a specified condition.
- /// </summary>
+ public static TSource FirstOrDefault<TSource>(
+ this IEnumerable<TSource> source)
+ {
+ return source.FirstImpl(Futures<TSource>.Default);
+ }
- public static TSource First<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, bool> predicate)
- {
- return First(source.Where(predicate));
- }
+ /// <summary>
+ /// Returns the first element of the sequence that satisfies a
+ /// condition or a default value if no such element is found.
+ /// </summary>
- /// <summary>
- /// Returns the first element of a sequence, or a default value if
- /// the sequence contains no elements.
- /// </summary>
+ public static TSource FirstOrDefault<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, bool> predicate)
+ {
+ return FirstOrDefault(source.Where(predicate));
+ }
- public static TSource FirstOrDefault<TSource>(
- this IEnumerable<TSource> source)
- {
- return source.FirstImpl(Futures<TSource>.Default);
- }
+ /// <summary>
+ /// Base implementation of Last operator.
+ /// </summary>
- /// <summary>
- /// Returns the first element of the sequence that satisfies a
- /// condition or a default value if no such element is found.
- /// </summary>
+ private static TSource LastImpl<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource> empty)
+ {
+ CheckNotNull(source, "source");
- public static TSource FirstOrDefault<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, bool> predicate)
- {
- return FirstOrDefault(source.Where(predicate));
- }
+ var list = source as IList<TSource>; // optimized case for lists
+ if (list != null)
+ return list.Count > 0 ? list[list.Count - 1] : empty();
- /// <summary>
- /// Base implementation of Last operator.
- /// </summary>
+ using (var e = source.GetEnumerator())
+ {
+ if (!e.MoveNext())
+ return empty();
- private static TSource LastImpl<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource> empty)
- {
- CheckNotNull(source, "source");
+ var last = e.Current;
+ while (e.MoveNext())
+ last = e.Current;
- var list = source as IList<TSource>; // 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();
+ /// <summary>
+ /// Returns the last element of a sequence.
+ /// </summary>
+ public static TSource Last<TSource>(
+ this IEnumerable<TSource> source)
+ {
+ return source.LastImpl(Futures<TSource>.Undefined);
+ }
- var last = e.Current;
- while (e.MoveNext())
- last = e.Current;
+ /// <summary>
+ /// Returns the last element of a sequence that satisfies a
+ /// specified condition.
+ /// </summary>
- return last;
- }
- }
+ public static TSource Last<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, bool> predicate)
+ {
+ return Last(source.Where(predicate));
+ }
- /// <summary>
- /// Returns the last element of a sequence.
- /// </summary>
- public static TSource Last<TSource>(
- this IEnumerable<TSource> source)
- {
- return source.LastImpl(Futures<TSource>.Undefined);
- }
+ /// <summary>
+ /// Returns the last element of a sequence, or a default value if
+ /// the sequence contains no elements.
+ /// </summary>
- /// <summary>
- /// Returns the last element of a sequence that satisfies a
- /// specified condition.
- /// </summary>
+ public static TSource LastOrDefault<TSource>(
+ this IEnumerable<TSource> source)
+ {
+ return source.LastImpl(Futures<TSource>.Default);
+ }
- public static TSource Last<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, bool> predicate)
- {
- return Last(source.Where(predicate));
- }
+ /// <summary>
+ /// Returns the last element of a sequence that satisfies a
+ /// condition or a default value if no such element is found.
+ /// </summary>
- /// <summary>
- /// Returns the last element of a sequence, or a default value if
- /// the sequence contains no elements.
- /// </summary>
+ public static TSource LastOrDefault<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, bool> predicate)
+ {
+ return LastOrDefault(source.Where(predicate));
+ }
- public static TSource LastOrDefault<TSource>(
- this IEnumerable<TSource> source)
- {
- return source.LastImpl(Futures<TSource>.Default);
- }
+ /// <summary>
+ /// Base implementation of Single operator.
+ /// </summary>
- /// <summary>
- /// Returns the last element of a sequence that satisfies a
- /// condition or a default value if no such element is found.
- /// </summary>
+ private static TSource SingleImpl<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource> empty)
+ {
+ CheckNotNull(source, "source");
- public static TSource LastOrDefault<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, bool> 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();
}
- /// <summary>
- /// Base implementation of Single operator.
- /// </summary>
-
- private static TSource SingleImpl<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource> empty)
- {
- CheckNotNull(source, "source");
+ return empty();
+ }
+ }
- using (var e = source.GetEnumerator())
- {
- if (e.MoveNext())
- {
- var single = e.Current;
- if (!e.MoveNext())
- return single;
+ /// <summary>
+ /// Returns the only element of a sequence, and throws an exception
+ /// if there is not exactly one element in the sequence.
+ /// </summary>
- throw new InvalidOperationException();
- }
+ public static TSource Single<TSource>(
+ this IEnumerable<TSource> source)
+ {
+ return source.SingleImpl(Futures<TSource>.Undefined);
+ }
- return empty();
- }
- }
+ /// <summary>
+ /// Returns the only element of a sequence that satisfies a
+ /// specified condition, and throws an exception if more than one
+ /// such element exists.
+ /// </summary>
- /// <summary>
- /// Returns the only element of a sequence, and throws an exception
- /// if there is not exactly one element in the sequence.
- /// </summary>
+ public static TSource Single<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, bool> predicate)
+ {
+ return Single(source.Where(predicate));
+ }
- public static TSource Single<TSource>(
- this IEnumerable<TSource> source)
- {
- return source.SingleImpl(Futures<TSource>.Undefined);
- }
+ /// <summary>
+ /// 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.
+ /// </summary>
- /// <summary>
- /// Returns the only element of a sequence that satisfies a
- /// specified condition, and throws an exception if more than one
- /// such element exists.
- /// </summary>
+ public static TSource SingleOrDefault<TSource>(
+ this IEnumerable<TSource> source)
+ {
+ return source.SingleImpl(Futures<TSource>.Default);
+ }
- public static TSource Single<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, bool> predicate)
- {
- return Single(source.Where(predicate));
- }
+ /// <summary>
+ /// 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.
+ /// </summary>
- /// <summary>
- /// 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.
- /// </summary>
+ public static TSource SingleOrDefault<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, bool> predicate)
+ {
+ return SingleOrDefault(source.Where(predicate));
+ }
- public static TSource SingleOrDefault<TSource>(
- this IEnumerable<TSource> source)
- {
- return source.SingleImpl(Futures<TSource>.Default);
- }
+ /// <summary>
+ /// Returns the element at a specified index in a sequence.
+ /// </summary>
- /// <summary>
- /// 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.
- /// </summary>
+ public static TSource ElementAt<TSource>(
+ this IEnumerable<TSource> source,
+ int index)
+ {
+ CheckNotNull(source, "source");
+
+ if (index < 0)
+ throw new ArgumentOutOfRangeException("index", index, null);
+
+ var list = source as IList<TSource>;
+ 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<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, bool> predicate)
- {
- return SingleOrDefault(source.Where(predicate));
- }
+ /// <summary>
+ /// Returns the element at a specified index in a sequence or a
+ /// default value if the index is out of range.
+ /// </summary>
- /// <summary>
- /// Returns the element at a specified index in a sequence.
- /// </summary>
+ public static TSource ElementAtOrDefault<TSource>(
+ this IEnumerable<TSource> source,
+ int index)
+ {
+ CheckNotNull(source, "source");
- public static TSource ElementAt<TSource>(
- this IEnumerable<TSource> source,
- int index)
- {
- CheckNotNull(source, "source");
-
- if (index < 0)
- throw new ArgumentOutOfRangeException("index", index, null);
-
- var list = source as IList<TSource>;
- 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);
- /// <summary>
- /// Returns the element at a specified index in a sequence or a
- /// default value if the index is out of range.
- /// </summary>
+ var list = source as IList<TSource>;
+ if (list != null)
+ return index < list.Count ? list[index] : default(TSource);
- public static TSource ElementAtOrDefault<TSource>(
- this IEnumerable<TSource> source,
- int index)
- {
- CheckNotNull(source, "source");
+ return source.SkipWhile((item, i) => i < index).FirstOrDefault();
+ }
- if (index < 0)
- return default(TSource);
+ /// <summary>
+ /// Inverts the order of the elements in a sequence.
+ /// </summary>
- var list = source as IList<TSource>;
- if (list != null)
- return index < list.Count ? list[index] : default(TSource);
+ public static IEnumerable<TSource> Reverse<TSource>(
+ this IEnumerable<TSource> source)
+ {
+ CheckNotNull(source, "source");
- return source.SkipWhile((item, i) => i < index).FirstOrDefault();
- }
+ return ReverseYield(source);
+ }
- /// <summary>
- /// Inverts the order of the elements in a sequence.
- /// </summary>
-
- public static IEnumerable<TSource> Reverse<TSource>(
- this IEnumerable<TSource> source)
- {
- CheckNotNull(source, "source");
+ private static IEnumerable<TSource> ReverseYield<TSource>(IEnumerable<TSource> source)
+ {
+ var stack = new Stack<TSource>();
+ foreach (var item in source)
+ stack.Push(item);
- return ReverseYield(source);
- }
+ foreach (var item in stack)
+ yield return item;
+ }
- private static IEnumerable<TSource> ReverseYield<TSource>(IEnumerable<TSource> source)
- {
- var stack = new Stack<TSource>();
- foreach (var item in source)
- stack.Push(item);
+ /// <summary>
+ /// Returns a specified number of contiguous elements from the start
+ /// of a sequence.
+ /// </summary>
- foreach (var item in stack)
- yield return item;
- }
+ public static IEnumerable<TSource> Take<TSource>(
+ this IEnumerable<TSource> source,
+ int count)
+ {
+ return source.Where((item, i) => i < count);
+ }
- /// <summary>
- /// Returns a specified number of contiguous elements from the start
- /// of a sequence.
- /// </summary>
+ /// <summary>
+ /// Bypasses a specified number of elements in a sequence and then
+ /// returns the remaining elements.
+ /// </summary>
- public static IEnumerable<TSource> Take<TSource>(
- this IEnumerable<TSource> source,
- int count)
- {
- return source.Where((item, i) => i < count);
- }
+ public static IEnumerable<TSource> Skip<TSource>(
+ this IEnumerable<TSource> source,
+ int count)
+ {
+ return source.Where((item, i) => i >= count);
+ }
- /// <summary>
- /// Bypasses a specified number of elements in a sequence and then
- /// returns the remaining elements.
- /// </summary>
+ /// <summary>
+ /// Bypasses elements in a sequence as long as a specified condition
+ /// is true and then returns the remaining elements.
+ /// </summary>
- public static IEnumerable<TSource> Skip<TSource>(
- this IEnumerable<TSource> source,
- int count)
- {
- return source.Where((item, i) => i >= count);
- }
+ public static IEnumerable<TSource> SkipWhile<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, bool> predicate)
+ {
+ CheckNotNull(predicate, "predicate");
- /// <summary>
- /// Bypasses elements in a sequence as long as a specified condition
- /// is true and then returns the remaining elements.
- /// </summary>
+ return source.SkipWhile((item, i) => predicate(item));
+ }
- public static IEnumerable<TSource> SkipWhile<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, bool> predicate)
- {
- CheckNotNull(predicate, "predicate");
+ /// <summary>
+ /// 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.
+ /// </summary>
- return source.SkipWhile((item, i) => predicate(item));
- }
+ public static IEnumerable<TSource> SkipWhile<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, int, bool> predicate)
+ {
+ CheckNotNull(source, "source");
+ CheckNotNull(predicate, "predicate");
- /// <summary>
- /// 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.
- /// </summary>
+ return SkipWhileYield(source, predicate);
+ }
- public static IEnumerable<TSource> SkipWhile<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, int, bool> predicate)
+ private static IEnumerable<TSource> SkipWhileYield<TSource>(
+ IEnumerable<TSource> source,
+ Func<TSource, int, bool> 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<TSource> SkipWhileYield<TSource>(
- IEnumerable<TSource> source,
- Func<TSource, int, bool> 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());
+ }
+ }
- /// <summary>
- /// Returns the number of elements in a sequence.
- /// </summary>
+ /// <summary>
+ /// Returns the number of elements in a sequence.
+ /// </summary>
- public static int Count<TSource>(
- this IEnumerable<TSource> source)
- {
- CheckNotNull(source, "source");
+ public static int Count<TSource>(
+ this IEnumerable<TSource> 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));
+ }
- /// <summary>
- /// Returns a number that represents how many elements in the
- /// specified sequence satisfy a condition.
- /// </summary>
+ /// <summary>
+ /// Returns a number that represents how many elements in the
+ /// specified sequence satisfy a condition.
+ /// </summary>
- public static int Count<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, bool> predicate)
- {
- return Count(source.Where(predicate));
- }
+ public static int Count<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, bool> predicate)
+ {
+ return Count(source.Where(predicate));
+ }
- /// <summary>
- /// Returns an <see cref="Int64"/> that represents the total number
- /// of elements in a sequence.
- /// </summary>
+ /// <summary>
+ /// Returns an <see cref="Int64"/> that represents the total number
+ /// of elements in a sequence.
+ /// </summary>
- public static long LongCount<TSource>(
- this IEnumerable<TSource> source)
- {
- CheckNotNull(source, "source");
+ public static long LongCount<TSource>(
+ this IEnumerable<TSource> 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);
+ }
- /// <summary>
- /// Returns an <see cref="Int64"/> that represents how many elements
- /// in a sequence satisfy a condition.
- /// </summary>
+ /// <summary>
+ /// Returns an <see cref="Int64"/> that represents how many elements
+ /// in a sequence satisfy a condition.
+ /// </summary>
- public static long LongCount<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, bool> predicate)
- {
- return LongCount(source.Where(predicate));
- }
+ public static long LongCount<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, bool> predicate)
+ {
+ return LongCount(source.Where(predicate));
+ }
- /// <summary>
- /// Concatenates two sequences.
- /// </summary>
+ /// <summary>
+ /// Concatenates two sequences.
+ /// </summary>
- public static IEnumerable<TSource> Concat<TSource>(
- this IEnumerable<TSource> first,
- IEnumerable<TSource> second)
- {
- CheckNotNull(first, "first");
- CheckNotNull(second, "second");
+ public static IEnumerable<TSource> Concat<TSource>(
+ this IEnumerable<TSource> first,
+ IEnumerable<TSource> second)
+ {
+ CheckNotNull(first, "first");
+ CheckNotNull(second, "second");
- return ConcatYield(first, second);
- }
+ return ConcatYield(first, second);
+ }
- private static IEnumerable<TSource> ConcatYield<TSource>(
- IEnumerable<TSource> first,
- IEnumerable<TSource> second)
- {
- foreach (var item in first)
- yield return item;
+ private static IEnumerable<TSource> ConcatYield<TSource>(
+ IEnumerable<TSource> first,
+ IEnumerable<TSource> 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;
+ }
- /// <summary>
- /// Creates a <see cref="List{T}"/> from an <see cref="IEnumerable{T}"/>.
- /// </summary>
+ /// <summary>
+ /// Creates a <see cref="List{T}"/> from an <see cref="IEnumerable{T}"/>.
+ /// </summary>
- public static List<TSource> ToList<TSource>(
- this IEnumerable<TSource> source)
- {
- CheckNotNull(source, "source");
+ public static List<TSource> ToList<TSource>(
+ this IEnumerable<TSource> source)
+ {
+ CheckNotNull(source, "source");
- return new List<TSource>(source);
- }
+ return new List<TSource>(source);
+ }
- /// <summary>
- /// Creates an array from an <see cref="IEnumerable{T}"/>.
- /// </summary>
+ /// <summary>
+ /// Creates an array from an <see cref="IEnumerable{T}"/>.
+ /// </summary>
- public static TSource[] ToArray<TSource>(
- this IEnumerable<TSource> source)
- {
- return source.ToList().ToArray();
- }
+ public static TSource[] ToArray<TSource>(
+ this IEnumerable<TSource> source)
+ {
+ return source.ToList().ToArray();
+ }
- /// <summary>
- /// Returns distinct elements from a sequence by using the default
- /// equality comparer to compare values.
- /// </summary>
+ /// <summary>
+ /// Returns distinct elements from a sequence by using the default
+ /// equality comparer to compare values.
+ /// </summary>
- public static IEnumerable<TSource> Distinct<TSource>(
- this IEnumerable<TSource> source)
- {
- return Distinct(source, /* comparer */ null);
- }
+ public static IEnumerable<TSource> Distinct<TSource>(
+ this IEnumerable<TSource> source)
+ {
+ return Distinct(source, /* comparer */ null);
+ }
- /// <summary>
- /// Returns distinct elements from a sequence by using a specified
- /// <see cref="IEqualityComparer{T}"/> to compare values.
- /// </summary>
+ /// <summary>
+ /// Returns distinct elements from a sequence by using a specified
+ /// <see cref="IEqualityComparer{T}"/> to compare values.
+ /// </summary>
- public static IEnumerable<TSource> Distinct<TSource>(
- this IEnumerable<TSource> source,
- IEqualityComparer<TSource> comparer)
- {
- CheckNotNull(source, "source");
+ public static IEnumerable<TSource> Distinct<TSource>(
+ this IEnumerable<TSource> source,
+ IEqualityComparer<TSource> comparer)
+ {
+ CheckNotNull(source, "source");
- return DistinctYield(source, comparer);
- }
+ return DistinctYield(source, comparer);
+ }
- private static IEnumerable<TSource> DistinctYield<TSource>(
- IEnumerable<TSource> source,
- IEqualityComparer<TSource> comparer)
+ private static IEnumerable<TSource> DistinctYield<TSource>(
+ IEnumerable<TSource> source,
+ IEqualityComparer<TSource> comparer)
+ {
+ var set = new Dictionary<TSource, object>(comparer);
+ var gotNull = false;
+
+ foreach (var item in source)
+ {
+ if (item == null)
{
- var set = new Dictionary<TSource, object>(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;
}
-
- /// <summary>
- /// Creates a <see cref="Lookup{TKey,TElement}" /> from an
- /// <see cref="IEnumerable{T}" /> according to a specified key
- /// selector function.
- /// </summary>
-
- public static ILookup<TKey, TSource> ToLookup<TSource, TKey>(
- this IEnumerable<TSource> source,
- Func<TSource, TKey> keySelector)
+ else
{
- return ToLookup(source, keySelector, e => e, /* comparer */ null);
+ if (set.ContainsKey(item))
+ continue;
+ set.Add(item, null);
}
- /// <summary>
- /// Creates a <see cref="Lookup{TKey,TElement}" /> from an
- /// <see cref="IEnumerable{T}" /> according to a specified key
- /// selector function and a key comparer.
- /// </summary>
+ yield return item;
+ }
+ }
- public static ILookup<TKey, TSource> ToLookup<TSource, TKey>(
- this IEnumerable<TSource> source,
- Func<TSource, TKey> keySelector,
- IEqualityComparer<TKey> comparer)
- {
- return ToLookup(source, keySelector, e => e, comparer);
- }
+ /// <summary>
+ /// Creates a <see cref="Lookup{TKey,TElement}" /> from an
+ /// <see cref="IEnumerable{T}" /> according to a specified key
+ /// selector function.
+ /// </summary>
- /// <summary>
- /// Creates a <see cref="Lookup{TKey,TElement}" /> from an
- /// <see cref="IEnumerable{T}" /> according to specified key
- /// and element selector functions.
- /// </summary>
+ public static ILookup<TKey, TSource> ToLookup<TSource, TKey>(
+ this IEnumerable<TSource> source,
+ Func<TSource, TKey> keySelector)
+ {
+ return ToLookup(source, keySelector, e => e, /* comparer */ null);
+ }
- public static ILookup<TKey, TElement> ToLookup<TSource, TKey, TElement>(
- this IEnumerable<TSource> source,
- Func<TSource, TKey> keySelector,
- Func<TSource, TElement> elementSelector)
- {
- return ToLookup(source, keySelector, elementSelector, /* comparer */ null);
- }
+ /// <summary>
+ /// Creates a <see cref="Lookup{TKey,TElement}" /> from an
+ /// <see cref="IEnumerable{T}" /> according to a specified key
+ /// selector function and a key comparer.
+ /// </summary>
- /// <summary>
- /// Creates a <see cref="Lookup{TKey,TElement}" /> from an
- /// <see cref="IEnumerable{T}" /> according to a specified key
- /// selector function, a comparer and an element selector function.
- /// </summary>
-
- public static ILookup<TKey, TElement> ToLookup<TSource, TKey, TElement>(
- this IEnumerable<TSource> source,
- Func<TSource, TKey> keySelector,
- Func<TSource, TElement> elementSelector,
- IEqualityComparer<TKey> comparer)
- {
- CheckNotNull(source, "source");
- CheckNotNull(keySelector, "keySelector");
- CheckNotNull(elementSelector, "elementSelector");
-
- var lookup = new Lookup<TKey, TElement>(comparer);
-
- foreach (var item in source)
- {
- var key = keySelector(item);
-
- var grouping = (Grouping<TKey, TElement>) lookup.Find(key);
- if (grouping == null)
- {
- grouping = new Grouping<TKey, TElement>(key);
- lookup.Add(grouping);
- }
-
- grouping.Add(elementSelector(item));
- }
-
- return lookup;
- }
+ public static ILookup<TKey, TSource> ToLookup<TSource, TKey>(
+ this IEnumerable<TSource> source,
+ Func<TSource, TKey> keySelector,
+ IEqualityComparer<TKey> comparer)
+ {
+ return ToLookup(source, keySelector, e => e, comparer);
+ }
- /// <summary>
- /// Groups the elements of a sequence according to a specified key
- /// selector function.
- /// </summary>
+ /// <summary>
+ /// Creates a <see cref="Lookup{TKey,TElement}" /> from an
+ /// <see cref="IEnumerable{T}" /> according to specified key
+ /// and element selector functions.
+ /// </summary>
- public static IEnumerable<IGrouping<TKey, TSource>> GroupBy<TSource, TKey>(
- this IEnumerable<TSource> source,
- Func<TSource, TKey> keySelector)
- {
- return GroupBy(source, keySelector, /* comparer */ null);
- }
+ public static ILookup<TKey, TElement> ToLookup<TSource, TKey, TElement>(
+ this IEnumerable<TSource> source,
+ Func<TSource, TKey> keySelector,
+ Func<TSource, TElement> elementSelector)
+ {
+ return ToLookup(source, keySelector, elementSelector, /* comparer */ null);
+ }
- /// <summary>
- /// Groups the elements of a sequence according to a specified key
- /// selector function and compares the keys by using a specified
- /// comparer.
- /// </summary>
+ /// <summary>
+ /// Creates a <see cref="Lookup{TKey,TElement}" /> from an
+ /// <see cref="IEnumerable{T}" /> according to a specified key
+ /// selector function, a comparer and an element selector function.
+ /// </summary>
- public static IEnumerable<IGrouping<TKey, TSource>> GroupBy<TSource, TKey>(
- this IEnumerable<TSource> source,
- Func<TSource, TKey> keySelector,
- IEqualityComparer<TKey> comparer)
- {
- return GroupBy(source, keySelector, e => e, comparer);
- }
+ public static ILookup<TKey, TElement> ToLookup<TSource, TKey, TElement>(
+ this IEnumerable<TSource> source,
+ Func<TSource, TKey> keySelector,
+ Func<TSource, TElement> elementSelector,
+ IEqualityComparer<TKey> comparer)
+ {
+ CheckNotNull(source, "source");
+ CheckNotNull(keySelector, "keySelector");
+ CheckNotNull(elementSelector, "elementSelector");
+
+ var lookup = new Lookup<TKey, TElement>(comparer);
- /// <summary>
- /// 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.
- /// </summary>
+ foreach (var item in source)
+ {
+ var key = keySelector(item);
- public static IEnumerable<IGrouping<TKey, TElement>> GroupBy<TSource, TKey, TElement>(
- this IEnumerable<TSource> source,
- Func<TSource, TKey> keySelector,
- Func<TSource, TElement> elementSelector)
+ var grouping = (Grouping<TKey, TElement>) lookup.Find(key);
+ if (grouping == null)
{
- return GroupBy(source, keySelector, elementSelector, /* comparer */ null);
+ grouping = new Grouping<TKey, TElement>(key);
+ lookup.Add(grouping);
}
- /// <summary>
- /// Groups the elements of a sequence according to a specified key
- /// selector function and creates a result value from each group and
- /// its key.
- /// </summary>
-
- public static IEnumerable<IGrouping<TKey, TElement>> GroupBy<TSource, TKey, TElement>(
- this IEnumerable<TSource> source,
- Func<TSource, TKey> keySelector,
- Func<TSource, TElement> elementSelector,
- IEqualityComparer<TKey> comparer)
- {
- CheckNotNull(source, "source");
- CheckNotNull(keySelector, "keySelector");
- CheckNotNull(elementSelector, "elementSelector");
+ grouping.Add(elementSelector(item));
+ }
- return ToLookup(source, keySelector, elementSelector, comparer);
- }
+ return lookup;
+ }
- /// <summary>
- /// 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.
- /// </summary>
+ /// <summary>
+ /// Groups the elements of a sequence according to a specified key
+ /// selector function.
+ /// </summary>
- public static IEnumerable<TResult> GroupBy<TSource, TKey, TResult>(
- this IEnumerable<TSource> source,
- Func<TSource, TKey> keySelector,
- Func<TKey, IEnumerable<TSource>, TResult> resultSelector)
- {
- return GroupBy(source, keySelector, resultSelector, /* comparer */ null);
- }
+ public static IEnumerable<IGrouping<TKey, TSource>> GroupBy<TSource, TKey>(
+ this IEnumerable<TSource> source,
+ Func<TSource, TKey> keySelector)
+ {
+ return GroupBy(source, keySelector, /* comparer */ null);
+ }
- /// <summary>
- /// 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.
- /// </summary>
-
- public static IEnumerable<TResult> GroupBy<TSource, TKey, TResult>(
- this IEnumerable<TSource> source,
- Func<TSource, TKey> keySelector,
- Func<TKey, IEnumerable<TSource>, TResult> resultSelector,
- IEqualityComparer<TKey> comparer)
- {
- CheckNotNull(source, "source");
- CheckNotNull(keySelector, "keySelector");
- CheckNotNull(resultSelector, "resultSelector");
+ /// <summary>
+ /// Groups the elements of a sequence according to a specified key
+ /// selector function and compares the keys by using a specified
+ /// comparer.
+ /// </summary>
- return ToLookup(source, keySelector, comparer).Select(g => resultSelector(g.Key, g));
- }
+ public static IEnumerable<IGrouping<TKey, TSource>> GroupBy<TSource, TKey>(
+ this IEnumerable<TSource> source,
+ Func<TSource, TKey> keySelector,
+ IEqualityComparer<TKey> comparer)
+ {
+ return GroupBy(source, keySelector, e => e, comparer);
+ }
- /// <summary>
- /// 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.
- /// </summary>
-
- public static IEnumerable<TResult> GroupBy<TSource, TKey, TElement, TResult>(
- this IEnumerable<TSource> source,
- Func<TSource, TKey> keySelector,
- Func<TSource, TElement> elementSelector,
- Func<TKey, IEnumerable<TElement>, TResult> resultSelector)
- {
- return GroupBy(source, keySelector, elementSelector, resultSelector, /* comparer */ null);
- }
+ /// <summary>
+ /// 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.
+ /// </summary>
- /// <summary>
- /// 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.
- /// </summary>
-
- public static IEnumerable<TResult> GroupBy<TSource, TKey, TElement, TResult>(
- this IEnumerable<TSource> source,
- Func<TSource, TKey> keySelector,
- Func<TSource, TElement> elementSelector,
- Func<TKey, IEnumerable<TElement>, TResult> resultSelector,
- IEqualityComparer<TKey> comparer)
- {
- CheckNotNull(source, "source");
- CheckNotNull(keySelector, "keySelector");
- CheckNotNull(elementSelector, "elementSelector");
- CheckNotNull(resultSelector, "resultSelector");
+ public static IEnumerable<IGrouping<TKey, TElement>> GroupBy<TSource, TKey, TElement>(
+ this IEnumerable<TSource> source,
+ Func<TSource, TKey> keySelector,
+ Func<TSource, TElement> elementSelector)
+ {
+ return GroupBy(source, keySelector, elementSelector, /* comparer */ null);
+ }
- return ToLookup(source, keySelector, elementSelector, comparer)
- .Select(g => resultSelector(g.Key, g));
- }
+ /// <summary>
+ /// Groups the elements of a sequence according to a specified key
+ /// selector function and creates a result value from each group and
+ /// its key.
+ /// </summary>
- /// <summary>
- /// Applies an accumulator function over a sequence.
- /// </summary>
+ public static IEnumerable<IGrouping<TKey, TElement>> GroupBy<TSource, TKey, TElement>(
+ this IEnumerable<TSource> source,
+ Func<TSource, TKey> keySelector,
+ Func<TSource, TElement> elementSelector,
+ IEqualityComparer<TKey> comparer)
+ {
+ CheckNotNull(source, "source");
+ CheckNotNull(keySelector, "keySelector");
+ CheckNotNull(elementSelector, "elementSelector");
- public static TSource Aggregate<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, TSource, TSource> func)
- {
- CheckNotNull(source, "source");
- CheckNotNull(func, "func");
+ return ToLookup(source, keySelector, elementSelector, comparer);
+ }
- using (var e = source.GetEnumerator())
- {
- if (!e.MoveNext())
- throw new InvalidOperationException();
+ /// <summary>
+ /// 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.
+ /// </summary>
- return e.Renumerable().Skip(1).Aggregate(e.Current, func);
- }
- }
+ public static IEnumerable<TResult> GroupBy<TSource, TKey, TResult>(
+ this IEnumerable<TSource> source,
+ Func<TSource, TKey> keySelector,
+ Func<TKey, IEnumerable<TSource>, TResult> resultSelector)
+ {
+ return GroupBy(source, keySelector, resultSelector, /* comparer */ null);
+ }
- /// <summary>
- /// Applies an accumulator function over a sequence. The specified
- /// seed value is used as the initial accumulator value.
- /// </summary>
+ /// <summary>
+ /// 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.
+ /// </summary>
- public static TAccumulate Aggregate<TSource, TAccumulate>(
- this IEnumerable<TSource> source,
- TAccumulate seed,
- Func<TAccumulate, TSource, TAccumulate> func)
- {
- return Aggregate(source, seed, func, r => r);
- }
+ public static IEnumerable<TResult> GroupBy<TSource, TKey, TResult>(
+ this IEnumerable<TSource> source,
+ Func<TSource, TKey> keySelector,
+ Func<TKey, IEnumerable<TSource>, TResult> resultSelector,
+ IEqualityComparer<TKey> comparer)
+ {
+ CheckNotNull(source, "source");
+ CheckNotNull(keySelector, "keySelector");
+ CheckNotNull(resultSelector, "resultSelector");
- /// <summary>
- /// 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.
- /// </summary>
-
- public static TResult Aggregate<TSource, TAccumulate, TResult>(
- this IEnumerable<TSource> source,
- TAccumulate seed,
- Func<TAccumulate, TSource, TAccumulate> func,
- Func<TAccumulate, TResult> resultSelector)
- {
- CheckNotNull(source, "source");
- CheckNotNull(func, "func");
- CheckNotNull(resultSelector, "resultSelector");
+ return ToLookup(source, keySelector, comparer).Select(g => resultSelector(g.Key, g));
+ }
- var result = seed;
+ /// <summary>
+ /// 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.
+ /// </summary>
- foreach (var item in source)
- result = func(result, item);
+ public static IEnumerable<TResult> GroupBy<TSource, TKey, TElement, TResult>(
+ this IEnumerable<TSource> source,
+ Func<TSource, TKey> keySelector,
+ Func<TSource, TElement> elementSelector,
+ Func<TKey, IEnumerable<TElement>, TResult> resultSelector)
+ {
+ return GroupBy(source, keySelector, elementSelector, resultSelector, /* comparer */ null);
+ }
- return resultSelector(result);
- }
+ /// <summary>
+ /// 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.
+ /// </summary>
- /// <summary>
- /// Produces the set union of two sequences by using the default
- /// equality comparer.
- /// </summary>
+ public static IEnumerable<TResult> GroupBy<TSource, TKey, TElement, TResult>(
+ this IEnumerable<TSource> source,
+ Func<TSource, TKey> keySelector,
+ Func<TSource, TElement> elementSelector,
+ Func<TKey, IEnumerable<TElement>, TResult> resultSelector,
+ IEqualityComparer<TKey> comparer)
+ {
+ CheckNotNull(source, "source");
+ CheckNotNull(keySelector, "keySelector");
+ CheckNotNull(elementSelector, "elementSelector");
+ CheckNotNull(resultSelector, "resultSelector");
- public static IEnumerable<TSource> Union<TSource>(
- this IEnumerable<TSource> first,
- IEnumerable<TSource> second)
- {
- return Union(first, second, /* comparer */ null);
- }
+ return ToLookup(source, keySelector, elementSelector, comparer)
+ .Select(g => resultSelector(g.Key, g));
+ }
- /// <summary>
- /// Produces the set union of two sequences by using a specified
- /// <see cref="IEqualityComparer{T}" />.
- /// </summary>
+ /// <summary>
+ /// Applies an accumulator function over a sequence.
+ /// </summary>
- public static IEnumerable<TSource> Union<TSource>(
- this IEnumerable<TSource> first,
- IEnumerable<TSource> second,
- IEqualityComparer<TSource> comparer)
- {
- return first.Concat(second).Distinct(comparer);
- }
+ public static TSource Aggregate<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, TSource, TSource> func)
+ {
+ CheckNotNull(source, "source");
+ CheckNotNull(func, "func");
- /// <summary>
- /// Returns the elements of the specified sequence or the type
- /// parameter's default value in a singleton collection if the
- /// sequence is empty.
- /// </summary>
+ using (var e = source.GetEnumerator())
+ {
+ if (!e.MoveNext())
+ throw new InvalidOperationException();
- public static IEnumerable<TSource> DefaultIfEmpty<TSource>(
- this IEnumerable<TSource> source)
- {
- return source.DefaultIfEmpty(default(TSource));
- }
+ return e.Renumerable().Skip(1).Aggregate(e.Current, func);
+ }
+ }
- /// <summary>
- /// Returns the elements of the specified sequence or the specified
- /// value in a singleton collection if the sequence is empty.
- /// </summary>
-
- public static IEnumerable<TSource> DefaultIfEmpty<TSource>(
- this IEnumerable<TSource> source,
- TSource defaultValue)
- {
- CheckNotNull(source, "source");
+ /// <summary>
+ /// Applies an accumulator function over a sequence. The specified
+ /// seed value is used as the initial accumulator value.
+ /// </summary>
- return DefaultIfEmptyYield(source, defaultValue);
- }
+ public static TAccumulate Aggregate<TSource, TAccumulate>(
+ this IEnumerable<TSource> source,
+ TAccumulate seed,
+ Func<TAccumulate, TSource, TAccumulate> func)
+ {
+ return Aggregate(source, seed, func, r => r);
+ }
- private static IEnumerable<TSource> DefaultIfEmptyYield<TSource>(
- IEnumerable<TSource> source,
- TSource defaultValue)
- {
- using (var e = source.GetEnumerator())
- {
- if (!e.MoveNext())
- yield return defaultValue;
- else
- do { yield return e.Current; } while (e.MoveNext());
- }
- }
+ /// <summary>
+ /// 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.
+ /// </summary>
- /// <summary>
- /// Determines whether all elements of a sequence satisfy a condition.
- /// </summary>
+ public static TResult Aggregate<TSource, TAccumulate, TResult>(
+ this IEnumerable<TSource> source,
+ TAccumulate seed,
+ Func<TAccumulate, TSource, TAccumulate> func,
+ Func<TAccumulate, TResult> resultSelector)
+ {
+ CheckNotNull(source, "source");
+ CheckNotNull(func, "func");
+ CheckNotNull(resultSelector, "resultSelector");
- public static bool All<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, bool> 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);
+ }
- /// <summary>
- /// Determines whether a sequence contains any elements.
- /// </summary>
+ /// <summary>
+ /// Produces the set union of two sequences by using the default
+ /// equality comparer.
+ /// </summary>
- public static bool Any<TSource>(
- this IEnumerable<TSource> source)
- {
- CheckNotNull(source, "source");
+ public static IEnumerable<TSource> Union<TSource>(
+ this IEnumerable<TSource> first,
+ IEnumerable<TSource> second)
+ {
+ return Union(first, second, /* comparer */ null);
+ }
- using (var e = source.GetEnumerator())
- return e.MoveNext();
- }
+ /// <summary>
+ /// Produces the set union of two sequences by using a specified
+ /// <see cref="IEqualityComparer{T}" />.
+ /// </summary>
- /// <summary>
- /// Determines whether any element of a sequence satisfies a
- /// condition.
- /// </summary>
+ public static IEnumerable<TSource> Union<TSource>(
+ this IEnumerable<TSource> first,
+ IEnumerable<TSource> second,
+ IEqualityComparer<TSource> comparer)
+ {
+ return first.Concat(second).Distinct(comparer);
+ }
- public static bool Any<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, bool> predicate)
- {
- return source.Where(predicate).Any();
- }
+ /// <summary>
+ /// Returns the elements of the specified sequence or the type
+ /// parameter's default value in a singleton collection if the
+ /// sequence is empty.
+ /// </summary>
- /// <summary>
- /// Determines whether a sequence contains a specified element by
- /// using the default equality comparer.
- /// </summary>
+ public static IEnumerable<TSource> DefaultIfEmpty<TSource>(
+ this IEnumerable<TSource> source)
+ {
+ return source.DefaultIfEmpty(default(TSource));
+ }
- public static bool Contains<TSource>(
- this IEnumerable<TSource> source,
- TSource value)
- {
- return source.Contains(value, /* comparer */ null);
- }
+ /// <summary>
+ /// Returns the elements of the specified sequence or the specified
+ /// value in a singleton collection if the sequence is empty.
+ /// </summary>
- /// <summary>
- /// Determines whether a sequence contains a specified element by
- /// using a specified <see cref="IEqualityComparer{T}" />.
- /// </summary>
+ public static IEnumerable<TSource> DefaultIfEmpty<TSource>(
+ this IEnumerable<TSource> source,
+ TSource defaultValue)
+ {
+ CheckNotNull(source, "source");
- public static bool Contains<TSource>(
- this IEnumerable<TSource> source,
- TSource value,
- IEqualityComparer<TSource> comparer)
- {
- CheckNotNull(source, "source");
+ return DefaultIfEmptyYield(source, defaultValue);
+ }
- if (comparer == null)
- {
- var collection = source as ICollection<TSource>;
- if (collection != null)
- return collection.Contains(value);
- }
+ private static IEnumerable<TSource> DefaultIfEmptyYield<TSource>(
+ IEnumerable<TSource> 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<TSource>.Default;
- return source.Any(item => comparer.Equals(item, value));
- }
+ /// <summary>
+ /// Determines whether all elements of a sequence satisfy a condition.
+ /// </summary>
- /// <summary>
- /// Determines whether two sequences are equal by comparing the
- /// elements by using the default equality comparer for their type.
- /// </summary>
+ public static bool All<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, bool> predicate)
+ {
+ CheckNotNull(source, "source");
+ CheckNotNull(predicate, "predicate");
- public static bool SequenceEqual<TSource>(
- this IEnumerable<TSource> first,
- IEnumerable<TSource> second)
- {
- return first.SequenceEqual(second, /* comparer */ null);
- }
+ foreach (var item in source)
+ if (!predicate(item))
+ return false;
- /// <summary>
- /// Determines whether two sequences are equal by comparing their
- /// elements by using a specified <see cref="IEqualityComparer{T}" />.
- /// </summary>
+ return true;
+ }
- public static bool SequenceEqual<TSource>(
- this IEnumerable<TSource> first,
- IEnumerable<TSource> second,
- IEqualityComparer<TSource> comparer)
- {
- CheckNotNull(first, "frist");
- CheckNotNull(second, "second");
+ /// <summary>
+ /// Determines whether a sequence contains any elements.
+ /// </summary>
- comparer = comparer ?? EqualityComparer<TSource>.Default;
+ public static bool Any<TSource>(
+ this IEnumerable<TSource> source)
+ {
+ CheckNotNull(source, "source");
- using (IEnumerator<TSource> 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));
- }
+ /// <summary>
+ /// Determines whether any element of a sequence satisfies a
+ /// condition.
+ /// </summary>
- return false;
- }
+ public static bool Any<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, bool> predicate)
+ {
+ return source.Where(predicate).Any();
+ }
- /// <summary>
- /// Base implementation for Min/Max operator.
- /// </summary>
+ /// <summary>
+ /// Determines whether a sequence contains a specified element by
+ /// using the default equality comparer.
+ /// </summary>
- private static TSource MinMaxImpl<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, TSource, bool> lesser)
- {
- CheckNotNull(source, "source");
- Debug.Assert(lesser != null);
+ public static bool Contains<TSource>(
+ this IEnumerable<TSource> source,
+ TSource value)
+ {
+ return source.Contains(value, /* comparer */ null);
+ }
- return source.Aggregate((a, item) => lesser(a, item) ? a : item);
- }
+ /// <summary>
+ /// Determines whether a sequence contains a specified element by
+ /// using a specified <see cref="IEqualityComparer{T}" />.
+ /// </summary>
- /// <summary>
- /// Base implementation for Min/Max operator for nullable types.
- /// </summary>
+ public static bool Contains<TSource>(
+ this IEnumerable<TSource> source,
+ TSource value,
+ IEqualityComparer<TSource> comparer)
+ {
+ CheckNotNull(source, "source");
- private static TSource? MinMaxImpl<TSource>(
- this IEnumerable<TSource?> source,
- TSource? seed, Func<TSource?, TSource?, bool> lesser) where TSource : struct
- {
- CheckNotNull(source, "source");
- Debug.Assert(lesser != null);
+ if (comparer == null)
+ {
+ var collection = source as ICollection<TSource>;
+ if (collection != null)
+ return collection.Contains(value);
+ }
- return source.Aggregate(seed, (a, item) => lesser(a, item) ? a : item);
- // == MinMaxImpl(Repeat<TSource?>(null, 1).Concat(source), lesser);
- }
+ comparer = comparer ?? EqualityComparer<TSource>.Default;
+ return source.Any(item => comparer.Equals(item, value));
+ }
- /// <summary>
- /// Returns the minimum value in a generic sequence.
- /// </summary>
+ /// <summary>
+ /// Determines whether two sequences are equal by comparing the
+ /// elements by using the default equality comparer for their type.
+ /// </summary>
- public static TSource Min<TSource>(
- this IEnumerable<TSource> source)
- {
- var comparer = Comparer<TSource>.Default;
- return source.MinMaxImpl((x, y) => comparer.Compare(x, y) < 0);
- }
+ public static bool SequenceEqual<TSource>(
+ this IEnumerable<TSource> first,
+ IEnumerable<TSource> second)
+ {
+ return first.SequenceEqual(second, /* comparer */ null);
+ }
- /// <summary>
- /// Invokes a transform function on each element of a generic
- /// sequence and returns the minimum resulting value.
- /// </summary>
+ /// <summary>
+ /// Determines whether two sequences are equal by comparing their
+ /// elements by using a specified <see cref="IEqualityComparer{T}" />.
+ /// </summary>
- public static TResult Min<TSource, TResult>(
- this IEnumerable<TSource> source,
- Func<TSource, TResult> selector)
- {
- return source.Select(selector).Min();
- }
+ public static bool SequenceEqual<TSource>(
+ this IEnumerable<TSource> first,
+ IEnumerable<TSource> second,
+ IEqualityComparer<TSource> comparer)
+ {
+ CheckNotNull(first, "frist");
+ CheckNotNull(second, "second");
- /// <summary>
- /// Returns the maximum value in a generic sequence.
- /// </summary>
+ comparer = comparer ?? EqualityComparer<TSource>.Default;
- public static TSource Max<TSource>(
- this IEnumerable<TSource> source)
+ using (IEnumerator<TSource> lhs = first.GetEnumerator(),
+ rhs = second.GetEnumerator())
+ {
+ do
{
- var comparer = Comparer<TSource>.Default;
- return source.MinMaxImpl((x, y) => comparer.Compare(x, y) > 0);
- }
+ if (!lhs.MoveNext())
+ return !rhs.MoveNext();
- /// <summary>
- /// Invokes a transform function on each element of a generic
- /// sequence and returns the maximum resulting value.
- /// </summary>
+ if (!rhs.MoveNext())
+ return false;
+ } while (comparer.Equals(lhs.Current, rhs.Current));
+ }
- public static TResult Max<TSource, TResult>(
- this IEnumerable<TSource> source,
- Func<TSource, TResult> selector)
- {
- return source.Select(selector).Max();
- }
+ return false;
+ }
- /// <summary>
- /// Makes an enumerator seen as enumerable once more.
- /// </summary>
- /// <remarks>
- /// 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.
- /// </remarks>
-
- private static IEnumerable<T> Renumerable<T>(this IEnumerator<T> e)
- {
- Debug.Assert(e != null);
+ /// <summary>
+ /// Base implementation for Min/Max operator.
+ /// </summary>
- do { yield return e.Current; } while (e.MoveNext());
- }
+ private static TSource MinMaxImpl<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, TSource, bool> lesser)
+ {
+ CheckNotNull(source, "source");
+ Debug.Assert(lesser != null);
- /// <summary>
- /// Sorts the elements of a sequence in ascending order according to a key.
- /// </summary>
+ return source.Aggregate((a, item) => lesser(a, item) ? a : item);
+ }
- public static IOrderedEnumerable<TSource> OrderBy<TSource, TKey>(
- this IEnumerable<TSource> source,
- Func<TSource, TKey> keySelector)
- {
- return source.OrderBy(keySelector, /* comparer */ null);
- }
+ /// <summary>
+ /// Base implementation for Min/Max operator for nullable types.
+ /// </summary>
- /// <summary>
- /// Sorts the elements of a sequence in ascending order by using a
- /// specified comparer.
- /// </summary>
+ private static TSource? MinMaxImpl<TSource>(
+ this IEnumerable<TSource?> source,
+ TSource? seed, Func<TSource?, TSource?, bool> lesser) where TSource : struct
+ {
+ CheckNotNull(source, "source");
+ Debug.Assert(lesser != null);
- public static IOrderedEnumerable<TSource> OrderBy<TSource, TKey>(
- this IEnumerable<TSource> source,
- Func<TSource, TKey> keySelector,
- IComparer<TKey> comparer)
- {
- CheckNotNull(source, "source");
- CheckNotNull(keySelector, "keySelector");
+ return source.Aggregate(seed, (a, item) => lesser(a, item) ? a : item);
+ // == MinMaxImpl(Repeat<TSource?>(null, 1).Concat(source), lesser);
+ }
- return new OrderedEnumerable<TSource, TKey>(source, keySelector, comparer, /* descending */ false);
- }
+ /// <summary>
+ /// Returns the minimum value in a generic sequence.
+ /// </summary>
- /// <summary>
- /// Sorts the elements of a sequence in descending order according to a key.
- /// </summary>
+ public static TSource Min<TSource>(
+ this IEnumerable<TSource> source)
+ {
+ var comparer = Comparer<TSource>.Default;
+ return source.MinMaxImpl((x, y) => comparer.Compare(x, y) < 0);
+ }
- public static IOrderedEnumerable<TSource> OrderByDescending<TSource, TKey>(
- this IEnumerable<TSource> source,
- Func<TSource, TKey> keySelector)
- {
- return source.OrderByDescending(keySelector, /* comparer */ null);
- }
+ /// <summary>
+ /// Invokes a transform function on each element of a generic
+ /// sequence and returns the minimum resulting value.
+ /// </summary>
- /// <summary>
- /// Sorts the elements of a sequence in descending order by using a
- /// specified comparer.
- /// </summary>
+ public static TResult Min<TSource, TResult>(
+ this IEnumerable<TSource> source,
+ Func<TSource, TResult> selector)
+ {
+ return source.Select(selector).Min();
+ }
- public static IOrderedEnumerable<TSource> OrderByDescending<TSource, TKey>(
- this IEnumerable<TSource> source,
- Func<TSource, TKey> keySelector,
- IComparer<TKey> comparer)
- {
- CheckNotNull(source, "source");
- CheckNotNull(source, "keySelector");
+ /// <summary>
+ /// Returns the maximum value in a generic sequence.
+ /// </summary>
- return new OrderedEnumerable<TSource, TKey>(source, keySelector, comparer, /* descending */ true);
- }
+ public static TSource Max<TSource>(
+ this IEnumerable<TSource> source)
+ {
+ var comparer = Comparer<TSource>.Default;
+ return source.MinMaxImpl((x, y) => comparer.Compare(x, y) > 0);
+ }
- /// <summary>
- /// Performs a subsequent ordering of the elements in a sequence in
- /// ascending order according to a key.
- /// </summary>
+ /// <summary>
+ /// Invokes a transform function on each element of a generic
+ /// sequence and returns the maximum resulting value.
+ /// </summary>
- public static IOrderedEnumerable<TSource> ThenBy<TSource, TKey>(
- this IOrderedEnumerable<TSource> source,
- Func<TSource, TKey> keySelector)
- {
- return source.ThenBy(keySelector, /* comparer */ null);
- }
+ public static TResult Max<TSource, TResult>(
+ this IEnumerable<TSource> source,
+ Func<TSource, TResult> selector)
+ {
+ return source.Select(selector).Max();
+ }
- /// <summary>
- /// Performs a subsequent ordering of the elements in a sequence in
- /// ascending order by using a specified comparer.
- /// </summary>
+ /// <summary>
+ /// Makes an enumerator seen as enumerable once more.
+ /// </summary>
+ /// <remarks>
+ /// 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.
+ /// </remarks>
- public static IOrderedEnumerable<TSource> ThenBy<TSource, TKey>(
- this IOrderedEnumerable<TSource> source,
- Func<TSource, TKey> keySelector,
- IComparer<TKey> comparer)
- {
- CheckNotNull(source, "source");
+ private static IEnumerable<T> Renumerable<T>(this IEnumerator<T> e)
+ {
+ Debug.Assert(e != null);
- return source.CreateOrderedEnumerable(keySelector, comparer, /* descending */ false);
- }
+ do
+ {
+ yield return e.Current;
+ } while (e.MoveNext());
+ }
- /// <summary>
- /// Performs a subsequent ordering of the elements in a sequence in
- /// descending order, according to a key.
- /// </summary>
+ /// <summary>
+ /// Sorts the elements of a sequence in ascending order according to a key.
+ /// </summary>
- public static IOrderedEnumerable<TSource> ThenByDescending<TSource, TKey>(
- this IOrderedEnumerable<TSource> source,
- Func<TSource, TKey> keySelector)
- {
- return source.ThenByDescending(keySelector, /* comparer */ null);
- }
+ public static IOrderedEnumerable<TSource> OrderBy<TSource, TKey>(
+ this IEnumerable<TSource> source,
+ Func<TSource, TKey> keySelector)
+ {
+ return source.OrderBy(keySelector, /* comparer */ null);
+ }
- /// <summary>
- /// Performs a subsequent ordering of the elements in a sequence in
- /// descending order by using a specified comparer.
- /// </summary>
+ /// <summary>
+ /// Sorts the elements of a sequence in ascending order by using a
+ /// specified comparer.
+ /// </summary>
- public static IOrderedEnumerable<TSource> ThenByDescending<TSource, TKey>(
- this IOrderedEnumerable<TSource> source,
- Func<TSource, TKey> keySelector,
- IComparer<TKey> comparer)
- {
- CheckNotNull(source, "source");
+ public static IOrderedEnumerable<TSource> OrderBy<TSource, TKey>(
+ this IEnumerable<TSource> source,
+ Func<TSource, TKey> keySelector,
+ IComparer<TKey> comparer)
+ {
+ CheckNotNull(source, "source");
+ CheckNotNull(keySelector, "keySelector");
- return source.CreateOrderedEnumerable(keySelector, comparer, /* descending */ true);
- }
+ return new OrderedEnumerable<TSource, TKey>(source, keySelector, comparer, /* descending */ false);
+ }
- /// <summary>
- /// Base implementation for Intersect and Except operators.
- /// </summary>
+ /// <summary>
+ /// Sorts the elements of a sequence in descending order according to a key.
+ /// </summary>
- private static IEnumerable<TSource> IntersectExceptImpl<TSource>(
- this IEnumerable<TSource> first,
- IEnumerable<TSource> second,
- IEqualityComparer<TSource> comparer,
- bool flag)
- {
- CheckNotNull(first, "first");
- CheckNotNull(second, "second");
+ public static IOrderedEnumerable<TSource> OrderByDescending<TSource, TKey>(
+ this IEnumerable<TSource> source,
+ Func<TSource, TKey> keySelector)
+ {
+ return source.OrderByDescending(keySelector, /* comparer */ null);
+ }
- var keys = new List<TSource>();
- var flags = new Dictionary<TSource, bool>(comparer);
+ /// <summary>
+ /// Sorts the elements of a sequence in descending order by using a
+ /// specified comparer.
+ /// </summary>
- foreach (var item in first.Where(k => !flags.ContainsKey(k)))
- {
- flags.Add(item, !flag);
- keys.Add(item);
- }
+ public static IOrderedEnumerable<TSource> OrderByDescending<TSource, TKey>(
+ this IEnumerable<TSource> source,
+ Func<TSource, TKey> keySelector,
+ IComparer<TKey> comparer)
+ {
+ CheckNotNull(source, "source");
+ CheckNotNull(source, "keySelector");
- foreach (var item in second.Where(flags.ContainsKey))
- flags[item] = flag;
+ return new OrderedEnumerable<TSource, TKey>(source, keySelector, comparer, /* descending */ true);
+ }
- //
- // As per docs, "the marked elements are yielded in the order in
- // which they were collected.
- //
+ /// <summary>
+ /// Performs a subsequent ordering of the elements in a sequence in
+ /// ascending order according to a key.
+ /// </summary>
- return keys.Where(item => flags[item]);
- }
+ public static IOrderedEnumerable<TSource> ThenBy<TSource, TKey>(
+ this IOrderedEnumerable<TSource> source,
+ Func<TSource, TKey> keySelector)
+ {
+ return source.ThenBy(keySelector, /* comparer */ null);
+ }
- /// <summary>
- /// Produces the set intersection of two sequences by using the
- /// default equality comparer to compare values.
- /// </summary>
+ /// <summary>
+ /// Performs a subsequent ordering of the elements in a sequence in
+ /// ascending order by using a specified comparer.
+ /// </summary>
- public static IEnumerable<TSource> Intersect<TSource>(
- this IEnumerable<TSource> first,
- IEnumerable<TSource> second)
- {
- return first.Intersect(second, /* comparer */ null);
- }
+ public static IOrderedEnumerable<TSource> ThenBy<TSource, TKey>(
+ this IOrderedEnumerable<TSource> source,
+ Func<TSource, TKey> keySelector,
+ IComparer<TKey> comparer)
+ {
+ CheckNotNull(source, "source");
- /// <summary>
- /// Produces the set intersection of two sequences by using the
- /// specified <see cref="IEqualityComparer{T}" /> to compare values.
- /// </summary>
+ return source.CreateOrderedEnumerable(keySelector, comparer, /* descending */ false);
+ }
- public static IEnumerable<TSource> Intersect<TSource>(
- this IEnumerable<TSource> first,
- IEnumerable<TSource> second,
- IEqualityComparer<TSource> comparer)
- {
- return IntersectExceptImpl(first, second, comparer, /* flag */ true);
- }
+ /// <summary>
+ /// Performs a subsequent ordering of the elements in a sequence in
+ /// descending order, according to a key.
+ /// </summary>
- /// <summary>
- /// Produces the set difference of two sequences by using the
- /// default equality comparer to compare values.
- /// </summary>
+ public static IOrderedEnumerable<TSource> ThenByDescending<TSource, TKey>(
+ this IOrderedEnumerable<TSource> source,
+ Func<TSource, TKey> keySelector)
+ {
+ return source.ThenByDescending(keySelector, /* comparer */ null);
+ }
- public static IEnumerable<TSource> Except<TSource>(
- this IEnumerable<TSource> first,
- IEnumerable<TSource> second)
- {
- return first.Except(second, /* comparer */ null);
- }
+ /// <summary>
+ /// Performs a subsequent ordering of the elements in a sequence in
+ /// descending order by using a specified comparer.
+ /// </summary>
- /// <summary>
- /// Produces the set difference of two sequences by using the
- /// specified <see cref="IEqualityComparer{T}" /> to compare values.
- /// </summary>
+ public static IOrderedEnumerable<TSource> ThenByDescending<TSource, TKey>(
+ this IOrderedEnumerable<TSource> source,
+ Func<TSource, TKey> keySelector,
+ IComparer<TKey> comparer)
+ {
+ CheckNotNull(source, "source");
- public static IEnumerable<TSource> Except<TSource>(
- this IEnumerable<TSource> first,
- IEnumerable<TSource> second,
- IEqualityComparer<TSource> comparer)
- {
- return IntersectExceptImpl(first, second, comparer, /* flag */ false);
- }
+ return source.CreateOrderedEnumerable(keySelector, comparer, /* descending */ true);
+ }
- /// <summary>
- /// Creates a <see cref="Dictionary{TKey,TValue}" /> from an
- /// <see cref="IEnumerable{T}" /> according to a specified key
- /// selector function.
- /// </summary>
+ /// <summary>
+ /// Base implementation for Intersect and Except operators.
+ /// </summary>
- public static Dictionary<TKey, TSource> ToDictionary<TSource, TKey>(
- this IEnumerable<TSource> source,
- Func<TSource, TKey> keySelector)
- {
- return source.ToDictionary(keySelector, /* comparer */ null);
- }
+ private static IEnumerable<TSource> IntersectExceptImpl<TSource>(
+ this IEnumerable<TSource> first,
+ IEnumerable<TSource> second,
+ IEqualityComparer<TSource> comparer,
+ bool flag)
+ {
+ CheckNotNull(first, "first");
+ CheckNotNull(second, "second");
- /// <summary>
- /// Creates a <see cref="Dictionary{TKey,TValue}" /> from an
- /// <see cref="IEnumerable{T}" /> according to a specified key
- /// selector function and key comparer.
- /// </summary>
+ var keys = new List<TSource>();
+ var flags = new Dictionary<TSource, bool>(comparer);
- public static Dictionary<TKey, TSource> ToDictionary<TSource, TKey>(
- this IEnumerable<TSource> source,
- Func<TSource, TKey> keySelector,
- IEqualityComparer<TKey> comparer)
- {
- return source.ToDictionary(keySelector, e => e);
- }
+ foreach (var item in first.Where(k => !flags.ContainsKey(k)))
+ {
+ flags.Add(item, !flag);
+ keys.Add(item);
+ }
- /// <summary>
- /// Creates a <see cref="Dictionary{TKey,TValue}" /> from an
- /// <see cref="IEnumerable{T}" /> according to specified key
- /// selector and element selector functions.
- /// </summary>
-
- public static Dictionary<TKey, TElement> ToDictionary<TSource, TKey, TElement>(
- this IEnumerable<TSource> source,
- Func<TSource, TKey> keySelector,
- Func<TSource, TElement> elementSelector)
- {
- return source.ToDictionary(keySelector, elementSelector, /* comparer */ null);
- }
+ foreach (var item in second.Where(flags.ContainsKey))
+ flags[item] = flag;
- /// <summary>
- /// Creates a <see cref="Dictionary{TKey,TValue}" /> from an
- /// <see cref="IEnumerable{T}" /> according to a specified key
- /// selector function, a comparer, and an element selector function.
- /// </summary>
-
- public static Dictionary<TKey, TElement> ToDictionary<TSource, TKey, TElement>(
- this IEnumerable<TSource> source,
- Func<TSource, TKey> keySelector,
- Func<TSource, TElement> elementSelector,
- IEqualityComparer<TKey> comparer)
- {
- CheckNotNull(source, "source");
- CheckNotNull(keySelector, "keySelector");
- CheckNotNull(elementSelector, "elementSelector");
-
- var dict = new Dictionary<TKey, TElement>(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<TKey, TValue>.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;
- }
+ //
+ // As per docs, "the marked elements are yielded in the order in
+ // which they were collected.
+ //
- /// <summary>
- /// Correlates the elements of two sequences based on matching keys.
- /// The default equality comparer is used to compare keys.
- /// </summary>
-
- public static IEnumerable<TResult> Join<TOuter, TInner, TKey, TResult>(
- this IEnumerable<TOuter> outer,
- IEnumerable<TInner> inner,
- Func<TOuter, TKey> outerKeySelector,
- Func<TInner, TKey> innerKeySelector,
- Func<TOuter, TInner, TResult> resultSelector)
- {
- return outer.Join(inner, outerKeySelector, innerKeySelector, resultSelector, /* comparer */ null);
- }
+ return keys.Where(item => flags[item]);
+ }
- /// <summary>
- /// Correlates the elements of two sequences based on matching keys.
- /// The default equality comparer is used to compare keys. A
- /// specified <see cref="IEqualityComparer{T}" /> is used to compare keys.
- /// </summary>
-
- public static IEnumerable<TResult> Join<TOuter, TInner, TKey, TResult>(
- this IEnumerable<TOuter> outer,
- IEnumerable<TInner> inner,
- Func<TOuter, TKey> outerKeySelector,
- Func<TInner, TKey> innerKeySelector,
- Func<TOuter, TInner, TResult> resultSelector,
- IEqualityComparer<TKey> 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);
- }
+ /// <summary>
+ /// Produces the set intersection of two sequences by using the
+ /// default equality comparer to compare values.
+ /// </summary>
- /// <summary>
- /// Correlates the elements of two sequences based on equality of
- /// keys and groups the results. The default equality comparer is
- /// used to compare keys.
- /// </summary>
-
- public static IEnumerable<TResult> GroupJoin<TOuter, TInner, TKey, TResult>(
- this IEnumerable<TOuter> outer,
- IEnumerable<TInner> inner,
- Func<TOuter, TKey> outerKeySelector,
- Func<TInner, TKey> innerKeySelector,
- Func<TOuter, IEnumerable<TInner>, TResult> resultSelector)
- {
- return outer.GroupJoin(inner, outerKeySelector, innerKeySelector, resultSelector, /* comparer */ null);
- }
+ public static IEnumerable<TSource> Intersect<TSource>(
+ this IEnumerable<TSource> first,
+ IEnumerable<TSource> second)
+ {
+ return first.Intersect(second, /* comparer */ null);
+ }
- /// <summary>
- /// 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 <see cref="IEqualityComparer{T}" />
- /// is used to compare keys.
- /// </summary>
-
- public static IEnumerable<TResult> GroupJoin<TOuter, TInner, TKey, TResult>(
- this IEnumerable<TOuter> outer,
- IEnumerable<TInner> inner,
- Func<TOuter, TKey> outerKeySelector,
- Func<TInner, TKey> innerKeySelector,
- Func<TOuter, IEnumerable<TInner>, TResult> resultSelector,
- IEqualityComparer<TKey> 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>(T value, string name) where T : class
- {
- if (value == null)
- throw new ArgumentNullException(name);
- }
+ /// <summary>
+ /// Produces the set intersection of two sequences by using the
+ /// specified <see cref="IEqualityComparer{T}" /> to compare values.
+ /// </summary>
- private static class Sequence<T>
- {
- public static readonly IEnumerable<T> Empty = new T[0];
- }
+ public static IEnumerable<TSource> Intersect<TSource>(
+ this IEnumerable<TSource> first,
+ IEnumerable<TSource> second,
+ IEqualityComparer<TSource> comparer)
+ {
+ return IntersectExceptImpl(first, second, comparer, /* flag */ true);
+ }
- private sealed class Grouping<K, V> : List<V>, IGrouping<K, V>
- {
- internal Grouping(K key)
- {
- Key = key;
- }
+ /// <summary>
+ /// Produces the set difference of two sequences by using the
+ /// default equality comparer to compare values.
+ /// </summary>
- public K Key { get; private set; }
- }
+ public static IEnumerable<TSource> Except<TSource>(
+ this IEnumerable<TSource> first,
+ IEnumerable<TSource> second)
+ {
+ return first.Except(second, /* comparer */ null);
}
-}
-// $Id: Enumerable.g.cs 215 2009-10-03 13:31:49Z azizatif $
+ /// <summary>
+ /// Produces the set difference of two sequences by using the
+ /// specified <see cref="IEqualityComparer{T}" /> to compare values.
+ /// </summary>
-namespace System.Linq
-{
- #region Imports
+ public static IEnumerable<TSource> Except<TSource>(
+ this IEnumerable<TSource> first,
+ IEnumerable<TSource> second,
+ IEqualityComparer<TSource> comparer)
+ {
+ return IntersectExceptImpl(first, second, comparer, /* flag */ false);
+ }
- using System;
- using System.Collections.Generic;
+ /// <summary>
+ /// Creates a <see cref="Dictionary{TKey,TValue}" /> from an
+ /// <see cref="IEnumerable{T}" /> according to a specified key
+ /// selector function.
+ /// </summary>
- #endregion
-
- // This partial implementation was template-generated:
- // Sat, 03 Oct 2009 09:42:39 GMT
+ public static Dictionary<TKey, TSource> ToDictionary<TSource, TKey>(
+ this IEnumerable<TSource> source,
+ Func<TSource, TKey> keySelector)
+ {
+ return source.ToDictionary(keySelector, /* comparer */ null);
+ }
- partial class Enumerable
+ /// <summary>
+ /// Creates a <see cref="Dictionary{TKey,TValue}" /> from an
+ /// <see cref="IEnumerable{T}" /> according to a specified key
+ /// selector function and key comparer.
+ /// </summary>
+
+ public static Dictionary<TKey, TSource> ToDictionary<TSource, TKey>(
+ this IEnumerable<TSource> source,
+ Func<TSource, TKey> keySelector,
+ IEqualityComparer<TKey> comparer)
{
- /// <summary>
- /// Computes the sum of a sequence of nullable <see cref="System.Int32" /> values.
- /// </summary>
+ return source.ToDictionary(keySelector, e => e);
+ }
- public static int Sum(
- this IEnumerable<int> source)
- {
- CheckNotNull(source, "source");
+ /// <summary>
+ /// Creates a <see cref="Dictionary{TKey,TValue}" /> from an
+ /// <see cref="IEnumerable{T}" /> according to specified key
+ /// selector and element selector functions.
+ /// </summary>
- int sum = 0;
- foreach (var num in source)
- sum = checked(sum + num);
+ public static Dictionary<TKey, TElement> ToDictionary<TSource, TKey, TElement>(
+ this IEnumerable<TSource> source,
+ Func<TSource, TKey> keySelector,
+ Func<TSource, TElement> elementSelector)
+ {
+ return source.ToDictionary(keySelector, elementSelector, /* comparer */ null);
+ }
- return sum;
- }
+ /// <summary>
+ /// Creates a <see cref="Dictionary{TKey,TValue}" /> from an
+ /// <see cref="IEnumerable{T}" /> according to a specified key
+ /// selector function, a comparer, and an element selector function.
+ /// </summary>
- /// <summary>
- /// Computes the sum of a sequence of nullable <see cref="System.Int32" />
- /// values that are obtained by invoking a transform function on
- /// each element of the input sequence.
- /// </summary>
+ public static Dictionary<TKey, TElement> ToDictionary<TSource, TKey, TElement>(
+ this IEnumerable<TSource> source,
+ Func<TSource, TKey> keySelector,
+ Func<TSource, TElement> elementSelector,
+ IEqualityComparer<TKey> comparer)
+ {
+ CheckNotNull(source, "source");
+ CheckNotNull(keySelector, "keySelector");
+ CheckNotNull(elementSelector, "elementSelector");
+
+ var dict = new Dictionary<TKey, TElement>(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<TKey, TValue>.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;
+ }
- public static int Sum<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, int> selector)
- {
- return source.Select(selector).Sum();
- }
-
- /// <summary>
- /// Computes the average of a sequence of nullable <see cref="System.Int32" /> values.
- /// </summary>
+ /// <summary>
+ /// Correlates the elements of two sequences based on matching keys.
+ /// The default equality comparer is used to compare keys.
+ /// </summary>
- public static double Average(
- this IEnumerable<int> source)
- {
- CheckNotNull(source, "source");
+ public static IEnumerable<TResult> Join<TOuter, TInner, TKey, TResult>(
+ this IEnumerable<TOuter> outer,
+ IEnumerable<TInner> inner,
+ Func<TOuter, TKey> outerKeySelector,
+ Func<TInner, TKey> innerKeySelector,
+ Func<TOuter, TInner, TResult> resultSelector)
+ {
+ return outer.Join(inner, outerKeySelector, innerKeySelector, resultSelector, /* comparer */ null);
+ }
- long sum = 0;
- long count = 0;
+ /// <summary>
+ /// Correlates the elements of two sequences based on matching keys.
+ /// The default equality comparer is used to compare keys. A
+ /// specified <see cref="IEqualityComparer{T}" /> is used to compare keys.
+ /// </summary>
- foreach (var num in source)
- checked
- {
- sum += (int) num;
- count++;
- }
+ public static IEnumerable<TResult> Join<TOuter, TInner, TKey, TResult>(
+ this IEnumerable<TOuter> outer,
+ IEnumerable<TInner> inner,
+ Func<TOuter, TKey> outerKeySelector,
+ Func<TInner, TKey> innerKeySelector,
+ Func<TOuter, TInner, TResult> resultSelector,
+ IEqualityComparer<TKey> 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);
+ }
- if (count == 0)
- throw new InvalidOperationException();
+ /// <summary>
+ /// Correlates the elements of two sequences based on equality of
+ /// keys and groups the results. The default equality comparer is
+ /// used to compare keys.
+ /// </summary>
- return (double) sum / count;
- }
+ public static IEnumerable<TResult> GroupJoin<TOuter, TInner, TKey, TResult>(
+ this IEnumerable<TOuter> outer,
+ IEnumerable<TInner> inner,
+ Func<TOuter, TKey> outerKeySelector,
+ Func<TInner, TKey> innerKeySelector,
+ Func<TOuter, IEnumerable<TInner>, TResult> resultSelector)
+ {
+ return outer.GroupJoin(inner, outerKeySelector, innerKeySelector, resultSelector, /* comparer */ null);
+ }
- /// <summary>
- /// Computes the average of a sequence of nullable <see cref="System.Int32" /> values
- /// that are obtained by invoking a transform function on each
- /// element of the input sequence.
- /// </summary>
+ /// <summary>
+ /// 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 <see cref="IEqualityComparer{T}" />
+ /// is used to compare keys.
+ /// </summary>
- public static double Average<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, int> selector)
- {
- return source.Select(selector).Average();
- }
-
+ public static IEnumerable<TResult> GroupJoin<TOuter, TInner, TKey, TResult>(
+ this IEnumerable<TOuter> outer,
+ IEnumerable<TInner> inner,
+ Func<TOuter, TKey> outerKeySelector,
+ Func<TInner, TKey> innerKeySelector,
+ Func<TOuter, IEnumerable<TInner>, TResult> resultSelector,
+ IEqualityComparer<TKey> 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)]));
+ }
- /// <summary>
- /// Computes the sum of a sequence of <see cref="System.Int32" /> values.
- /// </summary>
+ [DebuggerStepThrough]
+ private static void CheckNotNull<T>(T value, string name) where T : class
+ {
+ if (value == null)
+ throw new ArgumentNullException(name);
+ }
- public static int? Sum(
- this IEnumerable<int?> source)
- {
- CheckNotNull(source, "source");
+ private static class Sequence<T>
+ {
+ public static readonly IEnumerable<T> Empty = new T[0];
+ }
- int sum = 0;
- foreach (var num in source)
- sum = checked(sum + (num ?? 0));
+ private sealed class Grouping<K, V> : List<V>, IGrouping<K, V>
+ {
+ internal Grouping(K key)
+ {
+ Key = key;
+ }
- return sum;
- }
+ public K Key { get; private set; }
+ }
+ }
- /// <summary>
- /// Computes the sum of a sequence of <see cref="System.Int32" />
- /// values that are obtained by invoking a transform function on
- /// each element of the input sequence.
- /// </summary>
+ internal partial class Enumerable
+ {
+ /// <summary>
+ /// Computes the sum of a sequence of nullable <see cref="System.Int32" /> values.
+ /// </summary>
- public static int? Sum<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, int?> selector)
- {
- return source.Select(selector).Sum();
- }
-
- /// <summary>
- /// Computes the average of a sequence of <see cref="System.Int32" /> values.
- /// </summary>
+ public static int Sum(
+ this IEnumerable<int> source)
+ {
+ CheckNotNull(source, "source");
- public static double? Average(
- this IEnumerable<int?> source)
- {
- CheckNotNull(source, "source");
+ int sum = 0;
+ foreach (var num in source)
+ sum = checked(sum + num);
- long sum = 0;
- long count = 0;
+ return sum;
+ }
- foreach (var num in source.Where(n => n != null))
- checked
- {
- sum += (int) num;
- count++;
- }
+ /// <summary>
+ /// Computes the sum of a sequence of nullable <see cref="System.Int32" />
+ /// values that are obtained by invoking a transform function on
+ /// each element of the input sequence.
+ /// </summary>
- if (count == 0)
- return null;
+ public static int Sum<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, int> selector)
+ {
+ return source.Select(selector).Sum();
+ }
- return (double?) sum / count;
- }
+ /// <summary>
+ /// Computes the average of a sequence of nullable <see cref="System.Int32" /> values.
+ /// </summary>
+
+ public static double Average(
+ this IEnumerable<int> source)
+ {
+ CheckNotNull(source, "source");
- /// <summary>
- /// Computes the average of a sequence of <see cref="System.Int32" /> values
- /// that are obtained by invoking a transform function on each
- /// element of the input sequence.
- /// </summary>
+ long sum = 0;
+ long count = 0;
- public static double? Average<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, int?> selector)
- {
- return source.Select(selector).Average();
- }
-
- /// <summary>
- /// Returns the minimum value in a sequence of nullable
- /// <see cref="System.Int32" /> values.
- /// </summary>
-
- public static int? Min(
- this IEnumerable<int?> source)
+ foreach (var num in source)
+ checked
{
- CheckNotNull(source, "source");
-
- return MinMaxImpl(source.Where(x => x != null), null, (min, x) => min < x);
+ sum += (int) num;
+ count++;
}
- /// <summary>
- /// Invokes a transform function on each element of a sequence and
- /// returns the minimum nullable <see cref="System.Int32" /> value.
- /// </summary>
+ if (count == 0)
+ throw new InvalidOperationException();
- public static int? Min<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, int?> selector)
- {
- return source.Select(selector).Min();
- }
+ return (double) sum/count;
+ }
- /// <summary>
- /// Returns the maximum value in a sequence of nullable
- /// <see cref="System.Int32" /> values.
- /// </summary>
+ /// <summary>
+ /// Computes the average of a sequence of nullable <see cref="System.Int32" /> values
+ /// that are obtained by invoking a transform function on each
+ /// element of the input sequence.
+ /// </summary>
- public static int? Max(
- this IEnumerable<int?> 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 Average<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, int> selector)
+ {
+ return source.Select(selector).Average();
+ }
- /// <summary>
- /// Invokes a transform function on each element of a sequence and
- /// returns the maximum nullable <see cref="System.Int32" /> value.
- /// </summary>
- public static int? Max<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, int?> selector)
- {
- return source.Select(selector).Max();
- }
+ /// <summary>
+ /// Computes the sum of a sequence of <see cref="System.Int32" /> values.
+ /// </summary>
- /// <summary>
- /// Computes the sum of a sequence of nullable <see cref="System.Int64" /> values.
- /// </summary>
+ public static int? Sum(
+ this IEnumerable<int?> source)
+ {
+ CheckNotNull(source, "source");
- public static long Sum(
- this IEnumerable<long> source)
- {
- CheckNotNull(source, "source");
+ int sum = 0;
+ foreach (var num in source)
+ sum = checked(sum + (num ?? 0));
- long sum = 0;
- foreach (var num in source)
- sum = checked(sum + num);
+ return sum;
+ }
- return sum;
- }
+ /// <summary>
+ /// Computes the sum of a sequence of <see cref="System.Int32" />
+ /// values that are obtained by invoking a transform function on
+ /// each element of the input sequence.
+ /// </summary>
- /// <summary>
- /// Computes the sum of a sequence of nullable <see cref="System.Int64" />
- /// values that are obtained by invoking a transform function on
- /// each element of the input sequence.
- /// </summary>
+ public static int? Sum<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, int?> selector)
+ {
+ return source.Select(selector).Sum();
+ }
+
+ /// <summary>
+ /// Computes the average of a sequence of <see cref="System.Int32" /> values.
+ /// </summary>
- public static long Sum<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, long> selector)
+ public static double? Average(
+ this IEnumerable<int?> source)
+ {
+ CheckNotNull(source, "source");
+
+ long sum = 0;
+ long count = 0;
+
+ foreach (var num in source.Where(n => n != null))
+ checked
{
- return source.Select(selector).Sum();
+ sum += (int) num;
+ count++;
}
-
- /// <summary>
- /// Computes the average of a sequence of nullable <see cref="System.Int64" /> values.
- /// </summary>
- public static double Average(
- this IEnumerable<long> 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++;
- }
+ /// <summary>
+ /// Computes the average of a sequence of <see cref="System.Int32" /> values
+ /// that are obtained by invoking a transform function on each
+ /// element of the input sequence.
+ /// </summary>
- if (count == 0)
- throw new InvalidOperationException();
+ public static double? Average<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, int?> selector)
+ {
+ return source.Select(selector).Average();
+ }
- return (double) sum / count;
- }
+ /// <summary>
+ /// Returns the minimum value in a sequence of nullable
+ /// <see cref="System.Int32" /> values.
+ /// </summary>
- /// <summary>
- /// Computes the average of a sequence of nullable <see cref="System.Int64" /> values
- /// that are obtained by invoking a transform function on each
- /// element of the input sequence.
- /// </summary>
+ public static int? Min(
+ this IEnumerable<int?> source)
+ {
+ CheckNotNull(source, "source");
- public static double Average<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, long> selector)
- {
- return source.Select(selector).Average();
- }
-
+ return MinMaxImpl(source.Where(x => x != null), null, (min, x) => min < x);
+ }
- /// <summary>
- /// Computes the sum of a sequence of <see cref="System.Int64" /> values.
- /// </summary>
+ /// <summary>
+ /// Invokes a transform function on each element of a sequence and
+ /// returns the minimum nullable <see cref="System.Int32" /> value.
+ /// </summary>
- public static long? Sum(
- this IEnumerable<long?> source)
- {
- CheckNotNull(source, "source");
+ public static int? Min<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, int?> selector)
+ {
+ return source.Select(selector).Min();
+ }
- long sum = 0;
- foreach (var num in source)
- sum = checked(sum + (num ?? 0));
+ /// <summary>
+ /// Returns the maximum value in a sequence of nullable
+ /// <see cref="System.Int32" /> values.
+ /// </summary>
- return sum;
- }
+ public static int? Max(
+ this IEnumerable<int?> source)
+ {
+ CheckNotNull(source, "source");
- /// <summary>
- /// Computes the sum of a sequence of <see cref="System.Int64" />
- /// values that are obtained by invoking a transform function on
- /// each element of the input sequence.
- /// </summary>
+ return MinMaxImpl(source.Where(x => x != null),
+ null, (max, x) => x == null || (max != null && x.Value < max.Value));
+ }
- public static long? Sum<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, long?> selector)
- {
- return source.Select(selector).Sum();
- }
-
- /// <summary>
- /// Computes the average of a sequence of <see cref="System.Int64" /> values.
- /// </summary>
+ /// <summary>
+ /// Invokes a transform function on each element of a sequence and
+ /// returns the maximum nullable <see cref="System.Int32" /> value.
+ /// </summary>
- public static double? Average(
- this IEnumerable<long?> source)
- {
- CheckNotNull(source, "source");
+ public static int? Max<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, int?> selector)
+ {
+ return source.Select(selector).Max();
+ }
- long sum = 0;
- long count = 0;
+ /// <summary>
+ /// Computes the sum of a sequence of nullable <see cref="System.Int64" /> values.
+ /// </summary>
- foreach (var num in source.Where(n => n != null))
- checked
- {
- sum += (long) num;
- count++;
- }
+ public static long Sum(
+ this IEnumerable<long> source)
+ {
+ CheckNotNull(source, "source");
- if (count == 0)
- return null;
+ long sum = 0;
+ foreach (var num in source)
+ sum = checked(sum + num);
- return (double?) sum / count;
- }
+ return sum;
+ }
- /// <summary>
- /// Computes the average of a sequence of <see cref="System.Int64" /> values
- /// that are obtained by invoking a transform function on each
- /// element of the input sequence.
- /// </summary>
+ /// <summary>
+ /// Computes the sum of a sequence of nullable <see cref="System.Int64" />
+ /// values that are obtained by invoking a transform function on
+ /// each element of the input sequence.
+ /// </summary>
- public static double? Average<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, long?> selector)
- {
- return source.Select(selector).Average();
- }
-
- /// <summary>
- /// Returns the minimum value in a sequence of nullable
- /// <see cref="System.Int64" /> values.
- /// </summary>
-
- public static long? Min(
- this IEnumerable<long?> source)
- {
- CheckNotNull(source, "source");
-
- return MinMaxImpl(source.Where(x => x != null), null, (min, x) => min < x);
- }
+ public static long Sum<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, long> selector)
+ {
+ return source.Select(selector).Sum();
+ }
- /// <summary>
- /// Invokes a transform function on each element of a sequence and
- /// returns the minimum nullable <see cref="System.Int64" /> value.
- /// </summary>
+ /// <summary>
+ /// Computes the average of a sequence of nullable <see cref="System.Int64" /> values.
+ /// </summary>
- public static long? Min<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, long?> selector)
- {
- return source.Select(selector).Min();
- }
+ public static double Average(
+ this IEnumerable<long> source)
+ {
+ CheckNotNull(source, "source");
- /// <summary>
- /// Returns the maximum value in a sequence of nullable
- /// <see cref="System.Int64" /> values.
- /// </summary>
+ long sum = 0;
+ long count = 0;
- public static long? Max(
- this IEnumerable<long?> 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 += (long) num;
+ count++;
}
- /// <summary>
- /// Invokes a transform function on each element of a sequence and
- /// returns the maximum nullable <see cref="System.Int64" /> value.
- /// </summary>
+ if (count == 0)
+ throw new InvalidOperationException();
- public static long? Max<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, long?> selector)
- {
- return source.Select(selector).Max();
- }
+ return (double) sum/count;
+ }
- /// <summary>
- /// Computes the sum of a sequence of nullable <see cref="System.Single" /> values.
- /// </summary>
+ /// <summary>
+ /// Computes the average of a sequence of nullable <see cref="System.Int64" /> values
+ /// that are obtained by invoking a transform function on each
+ /// element of the input sequence.
+ /// </summary>
- public static float Sum(
- this IEnumerable<float> source)
- {
- CheckNotNull(source, "source");
+ public static double Average<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, long> selector)
+ {
+ return source.Select(selector).Average();
+ }
- float sum = 0;
- foreach (var num in source)
- sum = checked(sum + num);
- return sum;
- }
+ /// <summary>
+ /// Computes the sum of a sequence of <see cref="System.Int64" /> values.
+ /// </summary>
- /// <summary>
- /// Computes the sum of a sequence of nullable <see cref="System.Single" />
- /// values that are obtained by invoking a transform function on
- /// each element of the input sequence.
- /// </summary>
+ public static long? Sum(
+ this IEnumerable<long?> source)
+ {
+ CheckNotNull(source, "source");
- public static float Sum<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, float> selector)
- {
- return source.Select(selector).Sum();
- }
-
- /// <summary>
- /// Computes the average of a sequence of nullable <see cref="System.Single" /> values.
- /// </summary>
+ long sum = 0;
+ foreach (var num in source)
+ sum = checked(sum + (num ?? 0));
- public static float Average(
- this IEnumerable<float> source)
- {
- CheckNotNull(source, "source");
+ return sum;
+ }
- float sum = 0;
- long count = 0;
+ /// <summary>
+ /// Computes the sum of a sequence of <see cref="System.Int64" />
+ /// values that are obtained by invoking a transform function on
+ /// each element of the input sequence.
+ /// </summary>
- foreach (var num in source)
- checked
- {
- sum += (float) num;
- count++;
- }
+ public static long? Sum<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, long?> selector)
+ {
+ return source.Select(selector).Sum();
+ }
- if (count == 0)
- throw new InvalidOperationException();
+ /// <summary>
+ /// Computes the average of a sequence of <see cref="System.Int64" /> values.
+ /// </summary>
- return (float) sum / count;
- }
+ public static double? Average(
+ this IEnumerable<long?> source)
+ {
+ CheckNotNull(source, "source");
- /// <summary>
- /// Computes the average of a sequence of nullable <see cref="System.Single" /> values
- /// that are obtained by invoking a transform function on each
- /// element of the input sequence.
- /// </summary>
+ long sum = 0;
+ long count = 0;
- public static float Average<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, float> selector)
+ foreach (var num in source.Where(n => n != null))
+ checked
{
- return source.Select(selector).Average();
+ sum += (long) num;
+ count++;
}
-
- /// <summary>
- /// Computes the sum of a sequence of <see cref="System.Single" /> values.
- /// </summary>
-
- public static float? Sum(
- this IEnumerable<float?> source)
- {
- CheckNotNull(source, "source");
+ if (count == 0)
+ return null;
- float sum = 0;
- foreach (var num in source)
- sum = checked(sum + (num ?? 0));
+ return (double?) sum/count;
+ }
- return sum;
- }
+ /// <summary>
+ /// Computes the average of a sequence of <see cref="System.Int64" /> values
+ /// that are obtained by invoking a transform function on each
+ /// element of the input sequence.
+ /// </summary>
- /// <summary>
- /// Computes the sum of a sequence of <see cref="System.Single" />
- /// values that are obtained by invoking a transform function on
- /// each element of the input sequence.
- /// </summary>
+ public static double? Average<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, long?> selector)
+ {
+ return source.Select(selector).Average();
+ }
- public static float? Sum<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, float?> selector)
- {
- return source.Select(selector).Sum();
- }
-
- /// <summary>
- /// Computes the average of a sequence of <see cref="System.Single" /> values.
- /// </summary>
+ /// <summary>
+ /// Returns the minimum value in a sequence of nullable
+ /// <see cref="System.Int64" /> values.
+ /// </summary>
- public static float? Average(
- this IEnumerable<float?> source)
- {
- CheckNotNull(source, "source");
+ public static long? Min(
+ this IEnumerable<long?> source)
+ {
+ CheckNotNull(source, "source");
- float sum = 0;
- long count = 0;
+ return MinMaxImpl(source.Where(x => x != null), null, (min, x) => min < x);
+ }
- foreach (var num in source.Where(n => n != null))
- checked
- {
- sum += (float) num;
- count++;
- }
+ /// <summary>
+ /// Invokes a transform function on each element of a sequence and
+ /// returns the minimum nullable <see cref="System.Int64" /> value.
+ /// </summary>
- if (count == 0)
- return null;
+ public static long? Min<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, long?> selector)
+ {
+ return source.Select(selector).Min();
+ }
- return (float?) sum / count;
- }
+ /// <summary>
+ /// Returns the maximum value in a sequence of nullable
+ /// <see cref="System.Int64" /> values.
+ /// </summary>
- /// <summary>
- /// Computes the average of a sequence of <see cref="System.Single" /> values
- /// that are obtained by invoking a transform function on each
- /// element of the input sequence.
- /// </summary>
+ public static long? Max(
+ this IEnumerable<long?> source)
+ {
+ CheckNotNull(source, "source");
- public static float? Average<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, float?> selector)
- {
- return source.Select(selector).Average();
- }
-
- /// <summary>
- /// Returns the minimum value in a sequence of nullable
- /// <see cref="System.Single" /> values.
- /// </summary>
-
- public static float? Min(
- this IEnumerable<float?> source)
- {
- CheckNotNull(source, "source");
-
- return MinMaxImpl(source.Where(x => x != null), null, (min, x) => min < x);
- }
+ return MinMaxImpl(source.Where(x => x != null),
+ null, (max, x) => x == null || (max != null && x.Value < max.Value));
+ }
- /// <summary>
- /// Invokes a transform function on each element of a sequence and
- /// returns the minimum nullable <see cref="System.Single" /> value.
- /// </summary>
+ /// <summary>
+ /// Invokes a transform function on each element of a sequence and
+ /// returns the maximum nullable <see cref="System.Int64" /> value.
+ /// </summary>
- public static float? Min<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, float?> selector)
- {
- return source.Select(selector).Min();
- }
+ public static long? Max<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, long?> selector)
+ {
+ return source.Select(selector).Max();
+ }
- /// <summary>
- /// Returns the maximum value in a sequence of nullable
- /// <see cref="System.Single" /> values.
- /// </summary>
+ /// <summary>
+ /// Computes the sum of a sequence of nullable <see cref="System.Single" /> values.
+ /// </summary>
- public static float? Max(
- this IEnumerable<float?> source)
- {
- CheckNotNull(source, "source");
-
- return MinMaxImpl(source.Where(x => x != null),
- null, (max, x) => x == null || (max != null && x.Value < max.Value));
- }
+ public static float Sum(
+ this IEnumerable<float> source)
+ {
+ CheckNotNull(source, "source");
- /// <summary>
- /// Invokes a transform function on each element of a sequence and
- /// returns the maximum nullable <see cref="System.Single" /> value.
- /// </summary>
+ float sum = 0;
+ foreach (var num in source)
+ sum = checked(sum + num);
- public static float? Max<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, float?> selector)
- {
- return source.Select(selector).Max();
- }
+ return sum;
+ }
- /// <summary>
- /// Computes the sum of a sequence of nullable <see cref="System.Double" /> values.
- /// </summary>
+ /// <summary>
+ /// Computes the sum of a sequence of nullable <see cref="System.Single" />
+ /// values that are obtained by invoking a transform function on
+ /// each element of the input sequence.
+ /// </summary>
- public static double Sum(
- this IEnumerable<double> source)
- {
- CheckNotNull(source, "source");
+ public static float Sum<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, float> selector)
+ {
+ return source.Select(selector).Sum();
+ }
- double sum = 0;
- foreach (var num in source)
- sum = checked(sum + num);
+ /// <summary>
+ /// Computes the average of a sequence of nullable <see cref="System.Single" /> values.
+ /// </summary>
- return sum;
- }
+ public static float Average(
+ this IEnumerable<float> source)
+ {
+ CheckNotNull(source, "source");
- /// <summary>
- /// Computes the sum of a sequence of nullable <see cref="System.Double" />
- /// values that are obtained by invoking a transform function on
- /// each element of the input sequence.
- /// </summary>
+ float sum = 0;
+ long count = 0;
- public static double Sum<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, double> selector)
+ foreach (var num in source)
+ checked
{
- return source.Select(selector).Sum();
+ sum += (float) num;
+ count++;
}
-
- /// <summary>
- /// Computes the average of a sequence of nullable <see cref="System.Double" /> values.
- /// </summary>
- public static double Average(
- this IEnumerable<double> source)
- {
- CheckNotNull(source, "source");
+ if (count == 0)
+ throw new InvalidOperationException();
- double sum = 0;
- long count = 0;
+ return (float) sum/count;
+ }
- foreach (var num in source)
- checked
- {
- sum += (double) num;
- count++;
- }
+ /// <summary>
+ /// Computes the average of a sequence of nullable <see cref="System.Single" /> values
+ /// that are obtained by invoking a transform function on each
+ /// element of the input sequence.
+ /// </summary>
- if (count == 0)
- throw new InvalidOperationException();
+ public static float Average<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, float> selector)
+ {
+ return source.Select(selector).Average();
+ }
- return (double) sum / count;
- }
- /// <summary>
- /// Computes the average of a sequence of nullable <see cref="System.Double" /> values
- /// that are obtained by invoking a transform function on each
- /// element of the input sequence.
- /// </summary>
+ /// <summary>
+ /// Computes the sum of a sequence of <see cref="System.Single" /> values.
+ /// </summary>
- public static double Average<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, double> selector)
- {
- return source.Select(selector).Average();
- }
-
+ public static float? Sum(
+ this IEnumerable<float?> source)
+ {
+ CheckNotNull(source, "source");
- /// <summary>
- /// Computes the sum of a sequence of <see cref="System.Double" /> values.
- /// </summary>
+ float sum = 0;
+ foreach (var num in source)
+ sum = checked(sum + (num ?? 0));
- public static double? Sum(
- this IEnumerable<double?> source)
- {
- CheckNotNull(source, "source");
+ return sum;
+ }
- double sum = 0;
- foreach (var num in source)
- sum = checked(sum + (num ?? 0));
+ /// <summary>
+ /// Computes the sum of a sequence of <see cref="System.Single" />
+ /// values that are obtained by invoking a transform function on
+ /// each element of the input sequence.
+ /// </summary>
- return sum;
- }
+ public static float? Sum<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, float?> selector)
+ {
+ return source.Select(selector).Sum();
+ }
+
+ /// <summary>
+ /// Computes the average of a sequence of <see cref="System.Single" /> values.
+ /// </summary>
+
+ public static float? Average(
+ this IEnumerable<float?> source)
+ {
+ CheckNotNull(source, "source");
- /// <summary>
- /// Computes the sum of a sequence of <see cref="System.Double" />
- /// values that are obtained by invoking a transform function on
- /// each element of the input sequence.
- /// </summary>
+ float sum = 0;
+ long count = 0;
- public static double? Sum<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, double?> selector)
+ foreach (var num in source.Where(n => n != null))
+ checked
{
- return source.Select(selector).Sum();
+ sum += (float) num;
+ count++;
}
-
- /// <summary>
- /// Computes the average of a sequence of <see cref="System.Double" /> values.
- /// </summary>
- public static double? Average(
- this IEnumerable<double?> source)
- {
- CheckNotNull(source, "source");
+ if (count == 0)
+ return null;
- double sum = 0;
- long count = 0;
+ return (float?) sum/count;
+ }
- foreach (var num in source.Where(n => n != null))
- checked
- {
- sum += (double) num;
- count++;
- }
+ /// <summary>
+ /// Computes the average of a sequence of <see cref="System.Single" /> values
+ /// that are obtained by invoking a transform function on each
+ /// element of the input sequence.
+ /// </summary>
- if (count == 0)
- return null;
+ public static float? Average<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, float?> selector)
+ {
+ return source.Select(selector).Average();
+ }
- return (double?) sum / count;
- }
+ /// <summary>
+ /// Returns the minimum value in a sequence of nullable
+ /// <see cref="System.Single" /> values.
+ /// </summary>
- /// <summary>
- /// Computes the average of a sequence of <see cref="System.Double" /> values
- /// that are obtained by invoking a transform function on each
- /// element of the input sequence.
- /// </summary>
+ public static float? Min(
+ this IEnumerable<float?> source)
+ {
+ CheckNotNull(source, "source");
- public static double? Average<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, double?> selector)
- {
- return source.Select(selector).Average();
- }
-
- /// <summary>
- /// Returns the minimum value in a sequence of nullable
- /// <see cref="System.Double" /> values.
- /// </summary>
-
- public static double? Min(
- this IEnumerable<double?> source)
- {
- CheckNotNull(source, "source");
-
- return MinMaxImpl(source.Where(x => x != null), null, (min, x) => min < x);
- }
+ return MinMaxImpl(source.Where(x => x != null), null, (min, x) => min < x);
+ }
- /// <summary>
- /// Invokes a transform function on each element of a sequence and
- /// returns the minimum nullable <see cref="System.Double" /> value.
- /// </summary>
+ /// <summary>
+ /// Invokes a transform function on each element of a sequence and
+ /// returns the minimum nullable <see cref="System.Single" /> value.
+ /// </summary>
- public static double? Min<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, double?> selector)
- {
- return source.Select(selector).Min();
- }
+ public static float? Min<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, float?> selector)
+ {
+ return source.Select(selector).Min();
+ }
- /// <summary>
- /// Returns the maximum value in a sequence of nullable
- /// <see cref="System.Double" /> values.
- /// </summary>
+ /// <summary>
+ /// Returns the maximum value in a sequence of nullable
+ /// <see cref="System.Single" /> values.
+ /// </summary>
- public static double? Max(
- this IEnumerable<double?> source)
- {
- CheckNotNull(source, "source");
-
- return MinMaxImpl(source.Where(x => x != null),
- null, (max, x) => x == null || (max != null && x.Value < max.Value));
- }
+ public static float? Max(
+ this IEnumerable<float?> source)
+ {
+ CheckNotNull(source, "source");
- /// <summary>
- /// Invokes a transform function on each element of a sequence and
- /// returns the maximum nullable <see cref="System.Double" /> value.
- /// </summary>
+ return MinMaxImpl(source.Where(x => x != null),
+ null, (max, x) => x == null || (max != null && x.Value < max.Value));
+ }
- public static double? Max<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, double?> selector)
- {
- return source.Select(selector).Max();
- }
+ /// <summary>
+ /// Invokes a transform function on each element of a sequence and
+ /// returns the maximum nullable <see cref="System.Single" /> value.
+ /// </summary>
- /// <summary>
- /// Computes the sum of a sequence of nullable <see cref="System.Decimal" /> values.
- /// </summary>
+ public static float? Max<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, float?> selector)
+ {
+ return source.Select(selector).Max();
+ }
- public static decimal Sum(
- this IEnumerable<decimal> source)
- {
- CheckNotNull(source, "source");
+ /// <summary>
+ /// Computes the sum of a sequence of nullable <see cref="System.Double" /> values.
+ /// </summary>
- decimal sum = 0;
- foreach (var num in source)
- sum = checked(sum + num);
+ public static double Sum(
+ this IEnumerable<double> source)
+ {
+ CheckNotNull(source, "source");
- return sum;
- }
+ double sum = 0;
+ foreach (var num in source)
+ sum = checked(sum + num);
- /// <summary>
- /// Computes the sum of a sequence of nullable <see cref="System.Decimal" />
- /// values that are obtained by invoking a transform function on
- /// each element of the input sequence.
- /// </summary>
+ return sum;
+ }
- public static decimal Sum<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, decimal> selector)
- {
- return source.Select(selector).Sum();
- }
-
- /// <summary>
- /// Computes the average of a sequence of nullable <see cref="System.Decimal" /> values.
- /// </summary>
+ /// <summary>
+ /// Computes the sum of a sequence of nullable <see cref="System.Double" />
+ /// values that are obtained by invoking a transform function on
+ /// each element of the input sequence.
+ /// </summary>
- public static decimal Average(
- this IEnumerable<decimal> source)
- {
- CheckNotNull(source, "source");
+ public static double Sum<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, double> selector)
+ {
+ return source.Select(selector).Sum();
+ }
- decimal sum = 0;
- long count = 0;
+ /// <summary>
+ /// Computes the average of a sequence of nullable <see cref="System.Double" /> values.
+ /// </summary>
- foreach (var num in source)
- checked
- {
- sum += (decimal) num;
- count++;
- }
+ public static double Average(
+ this IEnumerable<double> source)
+ {
+ CheckNotNull(source, "source");
- if (count == 0)
- throw new InvalidOperationException();
+ double sum = 0;
+ long count = 0;
- return (decimal) sum / count;
+ foreach (var num in source)
+ checked
+ {
+ sum += (double) num;
+ count++;
}
- /// <summary>
- /// Computes the average of a sequence of nullable <see cref="System.Decimal" /> values
- /// that are obtained by invoking a transform function on each
- /// element of the input sequence.
- /// </summary>
+ if (count == 0)
+ throw new InvalidOperationException();
- public static decimal Average<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, decimal> selector)
- {
- return source.Select(selector).Average();
- }
-
+ return (double) sum/count;
+ }
- /// <summary>
- /// Computes the sum of a sequence of <see cref="System.Decimal" /> values.
- /// </summary>
+ /// <summary>
+ /// Computes the average of a sequence of nullable <see cref="System.Double" /> values
+ /// that are obtained by invoking a transform function on each
+ /// element of the input sequence.
+ /// </summary>
- public static decimal? Sum(
- this IEnumerable<decimal?> source)
- {
- CheckNotNull(source, "source");
+ public static double Average<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, double> selector)
+ {
+ return source.Select(selector).Average();
+ }
- decimal sum = 0;
- foreach (var num in source)
- sum = checked(sum + (num ?? 0));
- return sum;
- }
+ /// <summary>
+ /// Computes the sum of a sequence of <see cref="System.Double" /> values.
+ /// </summary>
- /// <summary>
- /// Computes the sum of a sequence of <see cref="System.Decimal" />
- /// values that are obtained by invoking a transform function on
- /// each element of the input sequence.
- /// </summary>
+ public static double? Sum(
+ this IEnumerable<double?> source)
+ {
+ CheckNotNull(source, "source");
- public static decimal? Sum<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, decimal?> selector)
- {
- return source.Select(selector).Sum();
- }
-
- /// <summary>
- /// Computes the average of a sequence of <see cref="System.Decimal" /> values.
- /// </summary>
+ double sum = 0;
+ foreach (var num in source)
+ sum = checked(sum + (num ?? 0));
- public static decimal? Average(
- this IEnumerable<decimal?> source)
- {
- CheckNotNull(source, "source");
+ return sum;
+ }
- decimal sum = 0;
- long count = 0;
+ /// <summary>
+ /// Computes the sum of a sequence of <see cref="System.Double" />
+ /// values that are obtained by invoking a transform function on
+ /// each element of the input sequence.
+ /// </summary>
- foreach (var num in source.Where(n => n != null))
- checked
- {
- sum += (decimal) num;
- count++;
- }
+ public static double? Sum<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, double?> selector)
+ {
+ return source.Select(selector).Sum();
+ }
- if (count == 0)
- return null;
+ /// <summary>
+ /// Computes the average of a sequence of <see cref="System.Double" /> values.
+ /// </summary>
- return (decimal?) sum / count;
- }
+ public static double? Average(
+ this IEnumerable<double?> source)
+ {
+ CheckNotNull(source, "source");
- /// <summary>
- /// Computes the average of a sequence of <see cref="System.Decimal" /> values
- /// that are obtained by invoking a transform function on each
- /// element of the input sequence.
- /// </summary>
+ double sum = 0;
+ long count = 0;
- public static decimal? Average<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, decimal?> selector)
- {
- return source.Select(selector).Average();
- }
-
- /// <summary>
- /// Returns the minimum value in a sequence of nullable
- /// <see cref="System.Decimal" /> values.
- /// </summary>
-
- public static decimal? Min(
- this IEnumerable<decimal?> source)
+ foreach (var num in source.Where(n => n != null))
+ checked
{
- CheckNotNull(source, "source");
-
- return MinMaxImpl(source.Where(x => x != null), null, (min, x) => min < x);
+ sum += (double) num;
+ count++;
}
- /// <summary>
- /// Invokes a transform function on each element of a sequence and
- /// returns the minimum nullable <see cref="System.Decimal" /> value.
- /// </summary>
+ if (count == 0)
+ return null;
- public static decimal? Min<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, decimal?> selector)
- {
- return source.Select(selector).Min();
- }
+ return (double?) sum/count;
+ }
- /// <summary>
- /// Returns the maximum value in a sequence of nullable
- /// <see cref="System.Decimal" /> values.
- /// </summary>
+ /// <summary>
+ /// Computes the average of a sequence of <see cref="System.Double" /> values
+ /// that are obtained by invoking a transform function on each
+ /// element of the input sequence.
+ /// </summary>
- public static decimal? Max(
- this IEnumerable<decimal?> 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? Average<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, double?> selector)
+ {
+ return source.Select(selector).Average();
+ }
- /// <summary>
- /// Invokes a transform function on each element of a sequence and
- /// returns the maximum nullable <see cref="System.Decimal" /> value.
- /// </summary>
+ /// <summary>
+ /// Returns the minimum value in a sequence of nullable
+ /// <see cref="System.Double" /> values.
+ /// </summary>
- public static decimal? Max<TSource>(
- this IEnumerable<TSource> source,
- Func<TSource, decimal?> selector)
- {
- return source.Select(selector).Max();
- }
+ public static double? Min(
+ this IEnumerable<double?> source)
+ {
+ CheckNotNull(source, "source");
+
+ return MinMaxImpl(source.Where(x => x != null), null, (min, x) => min < x);
}
-}
-namespace System.Runtime.CompilerServices
-{
- /// <remarks>
- /// This attribute allows us to define extension methods without
- /// requiring .NET Framework 3.5. For more information, see the section,
- /// <a href="http://msdn.microsoft.com/en-us/magazine/cc163317.aspx#S7">Extension Methods in .NET Framework 2.0 Apps</a>,
- /// of <a href="http://msdn.microsoft.com/en-us/magazine/cc163317.aspx">Basic Instincts: Extension Methods</a>
- /// column in <a href="http://msdn.microsoft.com/msdnmag/">MSDN Magazine</a>,
- /// issue <a href="http://msdn.microsoft.com/en-us/magazine/cc135410.aspx">Nov 2007</a>.
- /// </remarks>
+ /// <summary>
+ /// Invokes a transform function on each element of a sequence and
+ /// returns the minimum nullable <see cref="System.Double" /> value.
+ /// </summary>
- [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class | AttributeTargets.Assembly)]
- internal sealed class ExtensionAttribute : Attribute { }
-}
+ public static double? Min<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, double?> selector)
+ {
+ return source.Select(selector).Min();
+ }
+
+ /// <summary>
+ /// Returns the maximum value in a sequence of nullable
+ /// <see cref="System.Double" /> values.
+ /// </summary>
-// $Id: Func.cs 224 2009-10-04 07:13:08Z azizatif $
+ public static double? Max(
+ this IEnumerable<double?> source)
+ {
+ CheckNotNull(source, "source");
-namespace System
-{
-#if LINQBRIDGE_LIB
- public delegate TResult Func<TResult>();
- public delegate TResult Func<T, TResult>(T a);
- public delegate TResult Func<T1, T2, TResult>(T1 arg1, T2 arg2);
- public delegate TResult Func<T1, T2, T3, TResult>(T1 arg1, T2 arg2, T3 arg3);
- public delegate TResult Func<T1, T2, T3, T4, TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4);
-#else
- delegate TResult Func<TResult>();
- delegate TResult Func<T, TResult>(T a);
- delegate TResult Func<T1, T2, TResult>(T1 arg1, T2 arg2);
- delegate TResult Func<T1, T2, T3, TResult>(T1 arg1, T2 arg2, T3 arg3);
- delegate TResult Func<T1, T2, T3, T4, TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4);
-#endif
-}
+ return MinMaxImpl(source.Where(x => x != null),
+ null, (max, x) => x == null || (max != null && x.Value < max.Value));
+ }
-// $Id: IGrouping.cs 225 2009-10-04 07:16:14Z azizatif $
+ /// <summary>
+ /// Invokes a transform function on each element of a sequence and
+ /// returns the maximum nullable <see cref="System.Double" /> value.
+ /// </summary>
-namespace System.Linq
-{
- #region Imports
+ public static double? Max<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, double?> selector)
+ {
+ return source.Select(selector).Max();
+ }
+
+ /// <summary>
+ /// Computes the sum of a sequence of nullable <see cref="System.Decimal" /> values.
+ /// </summary>
+
+ public static decimal Sum(
+ this IEnumerable<decimal> source)
+ {
+ CheckNotNull(source, "source");
+
+ decimal sum = 0;
+ foreach (var num in source)
+ sum = checked(sum + num);
+
+ return sum;
+ }
- using System.Collections.Generic;
+ /// <summary>
+ /// Computes the sum of a sequence of nullable <see cref="System.Decimal" />
+ /// values that are obtained by invoking a transform function on
+ /// each element of the input sequence.
+ /// </summary>
- #endregion
+ public static decimal Sum<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, decimal> selector)
+ {
+ return source.Select(selector).Sum();
+ }
/// <summary>
- /// Represents a collection of objects that have a common key.
+ /// Computes the average of a sequence of nullable <see cref="System.Decimal" /> values.
/// </summary>
- partial interface IGrouping<TKey, TElement> : IEnumerable<TElement>
+ public static decimal Average(
+ this IEnumerable<decimal> source)
{
- /// <summary>
- /// Gets the key of the <see cref="IGrouping{TKey,TElement}" />.
- /// </summary>
+ CheckNotNull(source, "source");
+
+ decimal sum = 0;
+ long count = 0;
+
+ foreach (var num in source)
+ checked
+ {
+ sum += (decimal) num;
+ count++;
+ }
+
+ if (count == 0)
+ throw new InvalidOperationException();
- TKey Key { get; }
+ return (decimal) sum/count;
}
-}
-// $Id: ILookup.cs 224 2009-10-04 07:13:08Z azizatif $
+ /// <summary>
+ /// Computes the average of a sequence of nullable <see cref="System.Decimal" /> values
+ /// that are obtained by invoking a transform function on each
+ /// element of the input sequence.
+ /// </summary>
+
+ public static decimal Average<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, decimal> selector)
+ {
+ return source.Select(selector).Average();
+ }
-namespace System.Linq
-{
- using System.Collections.Generic;
/// <summary>
- /// Defines an indexer, size property, and Boolean search method for
- /// data structures that map keys to <see cref="IEnumerable{T}"/>
- /// sequences of values.
+ /// Computes the sum of a sequence of <see cref="System.Decimal" /> values.
/// </summary>
- partial interface ILookup<TKey, TElement> : IEnumerable<IGrouping<TKey, TElement>>
+ public static decimal? Sum(
+ this IEnumerable<decimal?> source)
{
- bool Contains(TKey key);
- int Count { get; }
- IEnumerable<TElement> this[TKey key] { get; }
+ CheckNotNull(source, "source");
+
+ decimal sum = 0;
+ foreach (var num in source)
+ sum = checked(sum + (num ?? 0));
+
+ return sum;
}
-}
-// $Id: IOrderedEnumerable.cs 224 2009-10-04 07:13:08Z azizatif $
+ /// <summary>
+ /// Computes the sum of a sequence of <see cref="System.Decimal" />
+ /// values that are obtained by invoking a transform function on
+ /// each element of the input sequence.
+ /// </summary>
-namespace System.Linq
-{
- using System.Collections.Generic;
+ public static decimal? Sum<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, decimal?> selector)
+ {
+ return source.Select(selector).Sum();
+ }
/// <summary>
- /// Represents a sorted sequence.
+ /// Computes the average of a sequence of <see cref="System.Decimal" /> values.
/// </summary>
- partial interface IOrderedEnumerable<TElement> : IEnumerable<TElement>
+ public static decimal? Average(
+ this IEnumerable<decimal?> source)
{
- /// <summary>
- /// Performs a subsequent ordering on the elements of an
- /// <see cref="IOrderedEnumerable{T}"/> according to a key.
- /// </summary>
+ CheckNotNull(source, "source");
+
+ decimal sum = 0;
+ long count = 0;
+
+ foreach (var num in source.Where(n => n != null))
+ checked
+ {
+ sum += (decimal) num;
+ count++;
+ }
+
+ if (count == 0)
+ return null;
- IOrderedEnumerable<TElement> CreateOrderedEnumerable<TKey>(
- Func<TElement, TKey> keySelector, IComparer<TKey> comparer, bool descending);
+ return (decimal?) sum/count;
}
-}
-// $Id: Lookup.cs 224 2009-10-04 07:13:08Z azizatif $
+ /// <summary>
+ /// Computes the average of a sequence of <see cref="System.Decimal" /> values
+ /// that are obtained by invoking a transform function on each
+ /// element of the input sequence.
+ /// </summary>
-namespace System.Linq
-{
- #region Imports
+ public static decimal? Average<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, decimal?> selector)
+ {
+ return source.Select(selector).Average();
+ }
+
+ /// <summary>
+ /// Returns the minimum value in a sequence of nullable
+ /// <see cref="System.Decimal" /> values.
+ /// </summary>
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using IEnumerable=System.Collections.IEnumerable;
+ public static decimal? Min(
+ this IEnumerable<decimal?> source)
+ {
+ CheckNotNull(source, "source");
- #endregion
+ return MinMaxImpl(source.Where(x => x != null), null, (min, x) => min < x);
+ }
/// <summary>
- /// Represents a collection of keys each mapped to one or more values.
+ /// Invokes a transform function on each element of a sequence and
+ /// returns the minimum nullable <see cref="System.Decimal" /> value.
/// </summary>
- internal sealed class Lookup<TKey, TElement> : ILookup<TKey, TElement>
+ public static decimal? Min<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, decimal?> selector)
{
- private readonly Dictionary<TKey, IGrouping<TKey, TElement>> _map;
+ return source.Select(selector).Min();
+ }
- internal Lookup(IEqualityComparer<TKey> comparer)
- {
- _map = new Dictionary<TKey, IGrouping<TKey, TElement>>(comparer);
- }
+ /// <summary>
+ /// Returns the maximum value in a sequence of nullable
+ /// <see cref="System.Decimal" /> values.
+ /// </summary>
- internal void Add(IGrouping<TKey, TElement> item)
- {
- _map.Add(item.Key, item);
- }
+ public static decimal? Max(
+ this IEnumerable<decimal?> source)
+ {
+ CheckNotNull(source, "source");
- internal IEnumerable<TElement> Find(TKey key)
- {
- IGrouping<TKey, TElement> grouping;
- return _map.TryGetValue(key, out grouping) ? grouping : null;
- }
+ return MinMaxImpl(source.Where(x => x != null),
+ null, (max, x) => x == null || (max != null && x.Value < max.Value));
+ }
- /// <summary>
- /// Gets the number of key/value collection pairs in the <see cref="Lookup{TKey,TElement}" />.
- /// </summary>
+ /// <summary>
+ /// Invokes a transform function on each element of a sequence and
+ /// returns the maximum nullable <see cref="System.Decimal" /> value.
+ /// </summary>
- public int Count
- {
- get { return _map.Count; }
- }
+ public static decimal? Max<TSource>(
+ this IEnumerable<TSource> source,
+ Func<TSource, decimal?> selector)
+ {
+ return source.Select(selector).Max();
+ }
+ }
- /// <summary>
- /// Gets the collection of values indexed by the specified key.
- /// </summary>
+ /// <summary>
+ /// Represents a collection of objects that have a common key.
+ /// </summary>
+ internal partial interface IGrouping<TKey, TElement> : IEnumerable<TElement>
+ {
+ /// <summary>
+ /// Gets the key of the <see cref="IGrouping{TKey,TElement}" />.
+ /// </summary>
- public IEnumerable<TElement> this[TKey key]
- {
- get
- {
- IGrouping<TKey, TElement> result;
- return _map.TryGetValue(key, out result) ? result : Enumerable.Empty<TElement>();
- }
- }
+ TKey Key { get; }
+ }
+
+ /// <summary>
+ /// Defines an indexer, size property, and Boolean search method for
+ /// data structures that map keys to <see cref="IEnumerable{T}"/>
+ /// sequences of values.
+ /// </summary>
+ internal partial interface ILookup<TKey, TElement> : IEnumerable<IGrouping<TKey, TElement>>
+ {
+ bool Contains(TKey key);
+ int Count { get; }
+ IEnumerable<TElement> this[TKey key] { get; }
+ }
+
+ /// <summary>
+ /// Represents a sorted sequence.
+ /// </summary>
+ internal partial interface IOrderedEnumerable<TElement> : IEnumerable<TElement>
+ {
+ /// <summary>
+ /// Performs a subsequent ordering on the elements of an
+ /// <see cref="IOrderedEnumerable{T}"/> according to a key.
+ /// </summary>
- /// <summary>
- /// Determines whether a specified key is in the <see cref="Lookup{TKey,TElement}" />.
- /// </summary>
+ IOrderedEnumerable<TElement> CreateOrderedEnumerable<TKey>(
+ Func<TElement, TKey> keySelector, IComparer<TKey> comparer, bool descending);
+ }
- public bool Contains(TKey key)
- {
- return _map.ContainsKey(key);
- }
+ /// <summary>
+ /// Represents a collection of keys each mapped to one or more values.
+ /// </summary>
+ internal sealed class Lookup<TKey, TElement> : ILookup<TKey, TElement>
+ {
+ private readonly Dictionary<TKey, IGrouping<TKey, TElement>> _map;
- /// <summary>
- /// Applies a transform function to each key and its associated
- /// values and returns the results.
- /// </summary>
+ internal Lookup(IEqualityComparer<TKey> comparer)
+ {
+ _map = new Dictionary<TKey, IGrouping<TKey, TElement>>(comparer);
+ }
- public IEnumerable<TResult> ApplyResultSelector<TResult>(
- Func<TKey, IEnumerable<TElement>, TResult> resultSelector)
- {
- if (resultSelector == null)
- throw new ArgumentNullException("resultSelector");
-
- foreach (var pair in _map)
- yield return resultSelector(pair.Key, pair.Value);
- }
+ internal void Add(IGrouping<TKey, TElement> item)
+ {
+ _map.Add(item.Key, item);
+ }
- /// <summary>
- /// Returns a generic enumerator that iterates through the <see cref="Lookup{TKey,TElement}" />.
- /// </summary>
+ internal IEnumerable<TElement> Find(TKey key)
+ {
+ IGrouping<TKey, TElement> grouping;
+ return _map.TryGetValue(key, out grouping) ? grouping : null;
+ }
- public IEnumerator<IGrouping<TKey, TElement>> GetEnumerator()
- {
- return _map.Values.GetEnumerator();
- }
+ /// <summary>
+ /// Gets the number of key/value collection pairs in the <see cref="Lookup{TKey,TElement}" />.
+ /// </summary>
- IEnumerator IEnumerable.GetEnumerator()
- {
- return GetEnumerator();
- }
+ public int Count
+ {
+ get { return _map.Count; }
}
-}
-// $Id: OrderedEnumerable.cs 237 2010-01-31 12:20:24Z azizatif $
+ /// <summary>
+ /// Gets the collection of values indexed by the specified key.
+ /// </summary>
-namespace LinqBridge
-{
- #region Imports
+ public IEnumerable<TElement> this[TKey key]
+ {
+ get
+ {
+ IGrouping<TKey, TElement> result;
+ return _map.TryGetValue(key, out result) ? result : Enumerable.Empty<TElement>();
+ }
+ }
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
+ /// <summary>
+ /// Determines whether a specified key is in the <see cref="Lookup{TKey,TElement}" />.
+ /// </summary>
+
+ public bool Contains(TKey key)
+ {
+ return _map.ContainsKey(key);
+ }
- #endregion
+ /// <summary>
+ /// Applies a transform function to each key and its associated
+ /// values and returns the results.
+ /// </summary>
- internal sealed class OrderedEnumerable<T, K> : IOrderedEnumerable<T>
+ public IEnumerable<TResult> ApplyResultSelector<TResult>(
+ Func<TKey, IEnumerable<TElement>, TResult> resultSelector)
{
- private readonly IEnumerable<T> _source;
- private readonly List<Comparison<T>> _comparisons;
+ if (resultSelector == null)
+ throw new ArgumentNullException("resultSelector");
- public OrderedEnumerable(IEnumerable<T> source,
- Func<T, K> keySelector, IComparer<K> comparer, bool descending) :
- this(source, null, keySelector, comparer, descending) {}
+ foreach (var pair in _map)
+ yield return resultSelector(pair.Key, pair.Value);
+ }
- private OrderedEnumerable(IEnumerable<T> source, List<Comparison<T>> comparisons,
- Func<T, K> keySelector, IComparer<K> comparer, bool descending)
- {
- if (source == null) throw new ArgumentNullException("source");
- if (keySelector == null) throw new ArgumentNullException("keySelector");
+ /// <summary>
+ /// Returns a generic enumerator that iterates through the <see cref="Lookup{TKey,TElement}" />.
+ /// </summary>
- _source = source;
-
- comparer = comparer ?? Comparer<K>.Default;
+ public IEnumerator<IGrouping<TKey, TElement>> GetEnumerator()
+ {
+ return _map.Values.GetEnumerator();
+ }
- if (comparisons == null)
- comparisons = new List<Comparison<T>>(/* capacity */ 4);
+ IEnumerator IEnumerable.GetEnumerator()
+ {
+ return GetEnumerator();
+ }
+ }
- comparisons.Add((x, y)
- => (descending ? -1 : 1) * comparer.Compare(keySelector(x), keySelector(y)));
+ internal sealed class OrderedEnumerable<T, K> : IOrderedEnumerable<T>
+ {
+ private readonly IEnumerable<T> _source;
+ private readonly List<Comparison<T>> _comparisons;
- _comparisons = comparisons;
- }
+ public OrderedEnumerable(IEnumerable<T> source,
+ Func<T, K> keySelector, IComparer<K> comparer, bool descending) :
+ this(source, null, keySelector, comparer, descending)
+ {
+ }
- public IOrderedEnumerable<T> CreateOrderedEnumerable<KK>(
- Func<T, KK> keySelector, IComparer<KK> comparer, bool descending)
- {
- return new OrderedEnumerable<T, KK>(_source, _comparisons, keySelector, comparer, descending);
- }
+ private OrderedEnumerable(IEnumerable<T> source, List<Comparison<T>> comparisons,
+ Func<T, K> keySelector, IComparer<K> comparer, bool descending)
+ {
+ if (source == null) throw new ArgumentNullException("source");
+ if (keySelector == null) throw new ArgumentNullException("keySelector");
- public IEnumerator<T> GetEnumerator()
- {
- //
- // We sort using List<T>.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<T, int, Tuple<T, int>>(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<Tuple<T, int>, T>(GetFirst)).GetEnumerator();
+ _source = source;
- }
+ comparer = comparer ?? Comparer<K>.Default;
- /// <remarks>
- /// See <a href="http://code.google.com/p/linqbridge/issues/detail?id=11">issue #11</a>
- /// for why this method is needed and cannot be expressed as a
- /// lambda at the call site.
- /// </remarks>
+ if (comparisons == null)
+ comparisons = new List<Comparison<T>>( /* capacity */ 4);
- private static Tuple<T, int> TagPosition(T e, int i)
- {
- return new Tuple<T, int>(e, i);
- }
+ comparisons.Add((x, y)
+ => (descending ? -1 : 1)*comparer.Compare(keySelector(x), keySelector(y)));
- /// <remarks>
- /// See <a href="http://code.google.com/p/linqbridge/issues/detail?id=11">issue #11</a>
- /// for why this method is needed and cannot be expressed as a
- /// lambda at the call site.
- /// </remarks>
+ _comparisons = comparisons;
+ }
- private static T GetFirst(Tuple<T, int> pv)
- {
- return pv.First;
- }
+ public IOrderedEnumerable<T> CreateOrderedEnumerable<KK>(
+ Func<T, KK> keySelector, IComparer<KK> comparer, bool descending)
+ {
+ return new OrderedEnumerable<T, KK>(_source, _comparisons, keySelector, comparer, descending);
+ }
+
+ public IEnumerator<T> GetEnumerator()
+ {
+ //
+ // We sort using List<T>.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<T, int, Tuple<T, int>>(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<Tuple<T, int>, T>(GetFirst)).GetEnumerator();
- IEnumerator IEnumerable.GetEnumerator()
- {
- return GetEnumerator();
- }
}
-}
-// $Id: Tuple.cs 215 2009-10-03 13:31:49Z azizatif $
+ /// <remarks>
+ /// See <a href="http://code.google.com/p/linqbridge/issues/detail?id=11">issue #11</a>
+ /// for why this method is needed and cannot be expressed as a
+ /// lambda at the call site.
+ /// </remarks>
-namespace LinqBridge
-{
- #region Imports
+ private static Tuple<T, int> TagPosition(T e, int i)
+ {
+ return new Tuple<T, int>(e, i);
+ }
- using System;
- using System.Collections.Generic;
- using System.Text;
+ /// <remarks>
+ /// See <a href="http://code.google.com/p/linqbridge/issues/detail?id=11">issue #11</a>
+ /// for why this method is needed and cannot be expressed as a
+ /// lambda at the call site.
+ /// </remarks>
- #endregion
+ private static T GetFirst(Tuple<T, int> pv)
+ {
+ return pv.First;
+ }
- [ Serializable ]
- internal struct Tuple<TFirst, TSecond> : IEquatable<Tuple<TFirst, TSecond>>
+ IEnumerator IEnumerable.GetEnumerator()
{
- public TFirst First { get; private set; }
- public TSecond Second { get; private set; }
+ return GetEnumerator();
+ }
+ }
- public Tuple(TFirst first, TSecond second) : this()
- {
- First = first;
- Second = second;
- }
+ [Serializable]
+ internal struct Tuple<TFirst, TSecond> : IEquatable<Tuple<TFirst, TSecond>>
+ {
+ public TFirst First { get; private set; }
+ public TSecond Second { get; private set; }
- public override bool Equals(object obj)
- {
- return obj != null
- && obj is Tuple<TFirst, TSecond>
- && base.Equals((Tuple<TFirst, TSecond>) obj);
- }
+ public Tuple(TFirst first, TSecond second)
+ : this()
+ {
+ First = first;
+ Second = second;
+ }
- public bool Equals(Tuple<TFirst, TSecond> other)
- {
- return EqualityComparer<TFirst>.Default.Equals(other.First, First)
- && EqualityComparer<TSecond>.Default.Equals(other.Second, Second);
- }
+ public override bool Equals(object obj)
+ {
+ return obj != null
+ && obj is Tuple<TFirst, TSecond>
+ && base.Equals((Tuple<TFirst, TSecond>) obj);
+ }
- public override int GetHashCode()
- {
- var num = 0x7a2f0b42;
- num = (-1521134295 * num) + EqualityComparer<TFirst>.Default.GetHashCode(First);
- return (-1521134295 * num) + EqualityComparer<TSecond>.Default.GetHashCode(Second);
- }
+ public bool Equals(Tuple<TFirst, TSecond> other)
+ {
+ return EqualityComparer<TFirst>.Default.Equals(other.First, First)
+ && EqualityComparer<TSecond>.Default.Equals(other.Second, Second);
+ }
- public override string ToString()
- {
- return string.Format(@"{{ First = {0}, Second = {1} }}", First, Second);
- }
+ public override int GetHashCode()
+ {
+ var num = 0x7a2f0b42;
+ num = (-1521134295*num) + EqualityComparer<TFirst>.Default.GetHashCode(First);
+ return (-1521134295*num) + EqualityComparer<TSecond>.Default.GetHashCode(Second);
}
+
+ public override string ToString()
+ {
+ return string.Format(@"{{ First = {0}, Second = {1} }}", First, Second);
+ }
+ }
}
-// $Id: Action.cs 239 2010-02-05 23:26:23Z azizatif $
+namespace Newtonsoft.Json.Serialization
+{
+ public delegate TResult Func<TResult>();
+
+ public delegate TResult Func<T, TResult>(T a);
-namespace System
+ public delegate TResult Func<T1, T2, TResult>(T1 arg1, T2 arg2);
+
+ public delegate TResult Func<T1, T2, T3, TResult>(T1 arg1, T2 arg2, T3 arg3);
+
+ public delegate TResult Func<T1, T2, T3, T4, TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4);
+
+ public delegate void Action();
+
+ public delegate void Action<T1, T2>(T1 arg1, T2 arg2);
+
+ public delegate void Action<T1, T2, T3>(T1 arg1, T2 arg2, T3 arg3);
+
+ public delegate void Action<T1, T2, T3, T4>(T1 arg1, T2 arg2, T3 arg3, T4 arg4);
+}
+
+namespace System.Runtime.CompilerServices
{
-#if LINQBRIDGE_LIB
- public delegate void Action();
- public delegate void Action<T1, T2>(T1 arg1, T2 arg2);
- public delegate void Action<T1, T2, T3>(T1 arg1, T2 arg2, T3 arg3);
- public delegate void Action<T1, T2, T3, T4>(T1 arg1, T2 arg2, T3 arg3, T4 arg4);
-#else
- delegate void Action();
- delegate void Action<T1, T2>(T1 arg1, T2 arg2);
- delegate void Action<T1, T2, T3>(T1 arg1, T2 arg2, T3 arg3);
- delegate void Action<T1, T2, T3, T4>(T1 arg1, T2 arg2, T3 arg3, T4 arg4);
-#endif
+ /// <remarks>
+ /// This attribute allows us to define extension methods without
+ /// requiring .NET Framework 3.5. For more information, see the section,
+ /// <a href="http://msdn.microsoft.com/en-us/magazine/cc163317.aspx#S7">Extension Methods in .NET Framework 2.0 Apps</a>,
+ /// of <a href="http://msdn.microsoft.com/en-us/magazine/cc163317.aspx">Basic Instincts: Extension Methods</a>
+ /// column in <a href="http://msdn.microsoft.com/msdnmag/">MSDN Magazine</a>,
+ /// issue <a href="http://msdn.microsoft.com/en-us/magazine/cc135410.aspx">Nov 2007</a>.
+ /// </remarks>
+
+ [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
{