* use new OperatingSystem APIs in System.Net.Ping and remove dependency from System.Runtime.InteropServices.RuntimeInformation
* use new OperatingSystem APIs in System.Security.Cryptography.Algorithms
* use new OperatingSystem APIs in System.ComponentModel.Composition and improve perf..
* use new OperatingSystem APIs in System.Net.Http and remove dependency from System.Runtime.InteropServices.RuntimeInformation
* Apply suggestions from code review
* there is no need to cache the results anymore
using System;
using System.Diagnostics;
using System.IO;
-using System.Runtime.InteropServices;
namespace Microsoft.Extensions.CommandLineUtils
{
private static string TryFindMuxerPath()
{
var fileName = MuxerName;
- if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
+ if (OperatingSystem.IsWindows())
{
fileName += ".exe";
}
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
-using System.Runtime.InteropServices;
-
namespace System.Net.Quic.Implementations.MsQuic.Internal
{
internal static class MsQuicStatusCodes
{
- internal static readonly uint Success = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? Windows.Success : Linux.Success;
- internal static readonly uint Pending = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? Windows.Pending : Linux.Pending;
- internal static readonly uint InternalError = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? Windows.InternalError : Linux.InternalError;
+ internal static uint Success => OperatingSystem.IsWindows() ? Windows.Success : Linux.Success;
+ internal static uint Pending => OperatingSystem.IsWindows() ? Windows.Pending : Linux.Pending;
+ internal static uint InternalError => OperatingSystem.IsWindows() ? Windows.InternalError : Linux.InternalError;
// TODO return better error messages here.
- public static string GetError(uint status)
- {
- return RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
- ? Windows.GetError(status) : Linux.GetError(status);
- }
+ public static string GetError(uint status) => OperatingSystem.IsWindows() ? Windows.GetError(status) : Linux.GetError(status);
private static class Windows
{
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
-using System.Runtime.InteropServices;
-
namespace System.Net.Quic.Implementations.MsQuic.Internal
{
internal static class MsQuicStatusHelper
{
internal static bool SuccessfulStatusCode(uint status)
{
- if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
+ if (OperatingSystem.IsWindows())
{
return status < 0x80000000;
}
- if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
+ if (OperatingSystem.IsLinux())
{
return (int)status <= 0;
}
#nullable enable
using System.Globalization;
using System.IO;
-using System.Runtime.InteropServices;
using System.Text;
namespace System.Net.NetworkInformation
private static readonly string? s_discoveredPing4UtilityPath = GetPingUtilityPath(ipv4: true);
private static readonly string? s_discoveredPing6UtilityPath = GetPingUtilityPath(ipv4: false);
- private static readonly bool s_isOSX = RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
- private static readonly bool s_isBSD = RuntimeInformation.IsOSPlatform(OSPlatform.FreeBSD);
private static readonly Lazy<bool> s_isBusybox = new Lazy<bool>(() => IsBusyboxPing(s_discoveredPing4UtilityPath));
// We don't want to pick up an arbitrary or malicious ping
// FreeBSD: ping requires -W flag which accepts timeout in MILLISECONDS;
// ping6 requires -x which accepts timeout in MILLISECONDS
// OSX: ping requires -W flag which accepts timeout in MILLISECONDS; ping6 doesn't support timeout
- if (s_isBSD)
+ if (OperatingSystem.IsFreeBSD())
{
if (ipv4)
{
sb.Append(" -x ");
}
}
- else if (s_isOSX)
+ else if (OperatingSystem.IsMacOS())
{
if (ipv4)
{
if (ttl > 0)
{
- if (s_isBSD | s_isOSX)
+ if (OperatingSystem.IsFreeBSD() || OperatingSystem.IsMacOS())
{
// OSX and FreeBSD use -h to set hop limit for IPv6 and -m ttl for IPv4
if (ipv4)
if (fragmentOption != PingFragmentOptions.Default)
{
- if (s_isBSD | s_isOSX)
+ if (OperatingSystem.IsFreeBSD() || OperatingSystem.IsMacOS())
{
// The bit is off by default on OSX & FreeBSD
if (fragmentOption == PingFragmentOptions.Dont) {
[DebuggerTypeProxy(typeof(DirectoryCatalogDebuggerProxy))]
public partial class DirectoryCatalog : ComposablePartCatalog, INotifyComposablePartCatalogChanged, ICompositionElement
{
+ private static bool IsWindows =>
+#if NETSTANDARD || NETCOREAPP2_0
+ RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
+#else
+ OperatingSystem.IsWindows();
+#endif
+
private readonly Lock _thisLock = new Lock();
private readonly ICompositionElement? _definitionOrigin;
private ComposablePartCatalogCollection _catalogCollection;
private string[] GetFiles()
{
string[] files = Directory.GetFiles(_fullPath, _searchPattern);
- return Array.ConvertAll<string, string>(files, (file) => RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? file.ToUpperInvariant() : file);
+
+ if (!IsWindows)
+ {
+ return files;
+ }
+
+ return Array.ConvertAll<string, string>(files, (file) => file.ToUpperInvariant());
}
private static string GetFullPath(string path)
{
var fullPath = IOPath.GetFullPath(path);
- return RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? fullPath.ToUpperInvariant() : fullPath;
+ return IsWindows ? fullPath.ToUpperInvariant() : fullPath;
}
[MemberNotNull(nameof(_path))]
<Reference Include="System.Runtime" />
<Reference Include="System.Runtime.Extensions" />
<Reference Include="System.Runtime.InteropServices" />
- <Reference Include="System.Runtime.InteropServices.RuntimeInformation" />
<Reference Include="System.Security.Claims" Condition="'$(TargetsWindows)' == 'true'" />
<Reference Include="System.Security.Cryptography.Algorithms" />
<Reference Include="System.Security.Cryptography.Csp" />
<ItemGroup Condition="'$(TargetsUnix)' == 'true'">
<Reference Include="System.Diagnostics.Process" />
<Reference Include="System.IO.FileSystem" />
- <Reference Include="System.Runtime.InteropServices.RuntimeInformation" />
</ItemGroup>
</Project>
private const int IcmpHeaderLengthInBytes = 8;
private const int MinIpHeaderLengthInBytes = 20;
private const int MaxIpHeaderLengthInBytes = 60;
- private static readonly bool _sendIpHeader = RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
- private static readonly bool _needsConnect = RuntimeInformation.IsOSPlatform(OSPlatform.Linux);
+ private static bool SendIpHeader => OperatingSystem.IsMacOS();
+ private static bool NeedsConnect => OperatingSystem.IsLinux();
[ThreadStatic]
private static Random? t_idGenerator;
IpHeader iph = default;
bool ipv4 = address.AddressFamily == AddressFamily.InterNetwork;
- bool sendIpHeader = ipv4 && options != null && _sendIpHeader;
+ bool sendIpHeader = ipv4 && options != null && SendIpHeader;
if (sendIpHeader)
{
Socket socket = new Socket(addrFamily, SocketType.Raw, socketConfig.ProtocolType);
socket.ReceiveTimeout = socketConfig.Timeout;
socket.SendTimeout = socketConfig.Timeout;
- if (addrFamily == AddressFamily.InterNetworkV6 && RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
+ if (addrFamily == AddressFamily.InterNetworkV6 && OperatingSystem.IsMacOS())
{
socket.DualMode = false;
}
if (socketConfig.Options != null && addrFamily == AddressFamily.InterNetwork)
{
- if (_sendIpHeader)
+ if (SendIpHeader)
{
// some platforms like OSX don't support DontFragment so we construct IP header instead.
socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.HeaderIncluded, 1);
#pragma warning disable 618
// Disable warning about obsolete property. We could use GetAddressBytes but that allocates.
// IPv4 multicast address starts with 1110 bits so mask rest and test if we get correct value e.g. 0xe0.
- if (_needsConnect && !ep.Address.IsIPv6Multicast && !(addrFamily == AddressFamily.InterNetwork && (ep.Address.Address & 0xf0) == 0xe0))
+ if (NeedsConnect && !ep.Address.IsIPv6Multicast && !(addrFamily == AddressFamily.InterNetwork && (ep.Address.Address & 0xf0) == 0xe0))
{
// If it is not multicast, use Connect to scope responses only to the target address.
socket.Connect(socketConfig.EndPoint);
ht.Add("System.Security.Cryptography.DSA", DSACryptoServiceProviderType);
// Windows will register the public ECDsaCng type. Non-Windows gets a special handler.
- if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
+ if (OperatingSystem.IsWindows())
{
ht.Add(ECDsaIdentifier, ECDsaCngType);
}