Remove System.Linq dependency from System.Net.NetworkInformation (dotnet/corefx#41566)
authorStephen Toub <stoub@microsoft.com>
Sat, 5 Oct 2019 11:53:40 +0000 (07:53 -0400)
committerGitHub <noreply@github.com>
Sat, 5 Oct 2019 11:53:40 +0000 (07:53 -0400)
It was only being used on Unix, and the uses are all easily replaced with more efficient implementations.

Commit migrated from https://github.com/dotnet/corefx/commit/e3fe1db114a283b8719ed1d02a1e26d57010cb55

src/libraries/System.Net.NetworkInformation/src/System.Net.NetworkInformation.csproj
src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/BsdIPGlobalProperties.cs
src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/BsdIPv4GlobalStatistics.cs
src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/BsdIpInterfaceProperties.cs
src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/BsdNetworkInterface.cs
src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/LinuxIPGlobalProperties.cs
src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/LinuxNetworkInterface.cs
src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/UnixIPInterfaceProperties.cs
src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/UnixNetworkInterface.cs

index f66d25c..dacbb8b 100644 (file)
   </ItemGroup>
   <ItemGroup Condition="'$(TargetsUnix)' == 'true'">
     <Reference Include="System.IO.FileSystem" />
-    <Reference Include="System.Linq" />
     <Reference Include="System.Threading.Thread" />
   </ItemGroup>
   <ItemGroup Condition="'$(TargetsLinux)' == 'true'">
index 1222791..2ce6f1e 100644 (file)
@@ -2,9 +2,6 @@
 // The .NET Foundation licenses this file to you under the MIT license.
 // See the LICENSE file in the project root for more information.
 
-using System.Linq;
-using System.Threading.Tasks;
-
 namespace System.Net.NetworkInformation
 {
     internal class BsdIPGlobalProperties : UnixIPGlobalProperties
@@ -73,7 +70,12 @@ namespace System.Net.NetworkInformation
         public override IPEndPoint[] GetActiveTcpListeners()
         {
             TcpConnectionInformation[] allConnections = GetTcpConnections(listeners:true);
-            return allConnections.Select(tci => tci.LocalEndPoint).ToArray();
+            var endPoints = new IPEndPoint[allConnections.Length];
+            for (int i = 0; i < allConnections.Length; i++)
+            {
+                endPoints[i] = allConnections[i].LocalEndPoint;
+            }
+            return endPoints;
         }
 
         public unsafe override IPEndPoint[] GetActiveUdpListeners()
index 25f8867..2d619af 100644 (file)
@@ -3,7 +3,6 @@
 // See the LICENSE file in the project root for more information.
 
 using System.Collections.Generic;
-using System.Linq;
 
 namespace System.Net.NetworkInformation
 {
@@ -102,7 +101,13 @@ namespace System.Net.NetworkInformation
                 }
                 else if (result == 0)
                 {
-                    return interfacesByName.Values.ToArray();
+                    var results = new BsdNetworkInterface[interfacesByName.Count];
+                    int i = 0;
+                    foreach (KeyValuePair<string, BsdNetworkInterface> item in interfacesByName)
+                    {
+                        results[i++] = item.Value;
+                    }
+                    return results;
                 }
                 else
                 {
index 5396bf8..81f2d5e 100644 (file)
@@ -2,12 +2,6 @@
 // The .NET Foundation licenses this file to you under the MIT license.
 // See the LICENSE file in the project root for more information.
 
-using System.Diagnostics;
-using System.Globalization;
-using System.IO;
-using System.Linq;
-using System.Threading.Tasks;
-
 namespace System.Net.NetworkInformation
 {
     internal class LinuxIPGlobalProperties : UnixIPGlobalProperties
index dcd9c75..b9db5d4 100644 (file)
@@ -4,7 +4,6 @@
 
 using System.Collections.Generic;
 using System.IO;
-using System.Linq;
 
 namespace System.Net.NetworkInformation
 {
@@ -93,7 +92,13 @@ namespace System.Net.NetworkInformation
                 }
                 else if (result == 0)
                 {
-                    return interfacesByName.Values.ToArray();
+                    var results = new LinuxNetworkInterface[interfacesByName.Count];
+                    int i = 0;
+                    foreach (KeyValuePair<string, LinuxNetworkInterface> item in interfacesByName)
+                    {
+                        results[i++] = item.Value;
+                    }
+                    return results;
                 }
                 else
                 {
index e994f3a..3a2a719 100644 (file)
@@ -4,7 +4,6 @@
 
 using System.Collections.Generic;
 using System.IO;
-using System.Linq;
 using System.Net.Sockets;
 
 namespace System.Net.NetworkInformation
@@ -82,12 +81,15 @@ namespace System.Net.NetworkInformation
         private static UnicastIPAddressInformationCollection GetUnicastAddresses(UnixNetworkInterface uni)
         {
             var collection = new UnicastIPAddressInformationCollection();
-            foreach (IPAddress address in uni.Addresses.Where((addr) => !IPAddressUtil.IsMulticast(addr)))
+            foreach (IPAddress address in uni.Addresses)
             {
-                IPAddress netMask = (address.AddressFamily == AddressFamily.InterNetwork)
-                                    ? uni.GetNetMaskForIPv4Address(address)
-                                    : IPAddress.Any; // Windows compatibility
-                collection.InternalAdd(new UnixUnicastIPAddressInformation(address, netMask));
+                if (!IPAddressUtil.IsMulticast(address))
+                {
+                    IPAddress netMask = (address.AddressFamily == AddressFamily.InterNetwork)
+                                        ? uni.GetNetMaskForIPv4Address(address)
+                                        : IPAddress.Any; // Windows compatibility
+                    collection.InternalAdd(new UnixUnicastIPAddressInformation(address, netMask));
+                }
             }
 
             return collection;
@@ -96,9 +98,12 @@ namespace System.Net.NetworkInformation
         private static MulticastIPAddressInformationCollection GetMulticastAddresses(UnixNetworkInterface uni)
         {
             var collection = new MulticastIPAddressInformationCollection();
-            foreach (IPAddress address in uni.Addresses.Where(IPAddressUtil.IsMulticast))
+            foreach (IPAddress address in uni.Addresses)
             {
-                collection.InternalAdd(new UnixMulticastIPAddressInformation(address));
+                if (IPAddressUtil.IsMulticast(address))
+                {
+                    collection.InternalAdd(new UnixMulticastIPAddressInformation(address));
+                }
             }
 
             return collection;
index b98511a..6fe6403 100644 (file)
@@ -4,7 +4,6 @@
 
 using System.Collections.Generic;
 using System.Diagnostics;
-using System.Linq;
 
 namespace System.Net.NetworkInformation
 {
@@ -36,12 +35,19 @@ namespace System.Net.NetworkInformation
 
         public override bool Supports(NetworkInterfaceComponent networkInterfaceComponent)
         {
-            Sockets.AddressFamily family =
-                (networkInterfaceComponent == NetworkInterfaceComponent.IPv4)
-                ? Sockets.AddressFamily.InterNetwork
-                : Sockets.AddressFamily.InterNetworkV6;
+            Sockets.AddressFamily family = (networkInterfaceComponent == NetworkInterfaceComponent.IPv4) ?
+                Sockets.AddressFamily.InterNetwork :
+                Sockets.AddressFamily.InterNetworkV6;
 
-            return _addresses.Any(addr => addr.AddressFamily == family);
+            foreach (IPAddress addr in _addresses)
+            {
+                if (addr.AddressFamily == family)
+                {
+                    return true;
+                }
+            }
+
+            return false;
         }
 
         /// <summary>