Skip to content
Merged
17 changes: 5 additions & 12 deletions src/libraries/Common/src/System/HexConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,11 @@ public static void EncodeToUtf16(ReadOnlySpan<byte> source, Span<char> destinati

public static string ToString(ReadOnlySpan<byte> bytes, Casing casing = Casing.Upper)
{
#if NETFRAMEWORK || NETSTANDARD2_0
#if NET
SpanCasingPair args = new() { Bytes = bytes, Casing = casing };
return string.Create(bytes.Length * 2, args, static (chars, args) =>
EncodeToUtf16(args.Bytes, chars, args.Casing));
#else
Span<char> result = (bytes.Length > 16) ?
new char[bytes.Length * 2].AsSpan() :
stackalloc char[bytes.Length * 2];
Expand All @@ -230,17 +234,6 @@ public static string ToString(ReadOnlySpan<byte> bytes, Casing casing = Casing.U
pos += 2;
}
return result.ToString();
#elif NET9_0_OR_GREATER
SpanCasingPair args = new() { Bytes = bytes, Casing = casing };
return string.Create(bytes.Length * 2, args, static (chars, args) =>
EncodeToUtf16(args.Bytes, chars, args.Casing));
#else
// .NET 8.0 path (doesn't support 'allow ref struct' feature)
unsafe
{
return string.Create(bytes.Length * 2, (RosPtr: (IntPtr)(&bytes), casing), static (chars, args) =>
EncodeToUtf16(*(ReadOnlySpan<byte>*)args.RosPtr, chars, args.casing));
}
#endif
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ internal static IncrementalHash CreateIncrementalHash(HashAlgorithmName hashAlgo

internal static CryptographicException CreateAlgorithmUnknownException(AsnWriter encodedId)
{
#if NET10_0_OR_GREATER
#if NET
return encodedId.Encode(static encoded => CreateAlgorithmUnknownException(Convert.ToHexString(encoded)));
#else
return CreateAlgorithmUnknownException(HexConverter.ToString(encodedId.Encode(), HexConverter.Casing.Upper));
Expand All @@ -231,7 +231,7 @@ internal static CryptographicException CreateAlgorithmUnknownException(string al
#if !BUILDING_PKCS
internal static string EncodeAsnWriterToPem(string label, AsnWriter writer, bool clear = true)
{
#if NET10_0_OR_GREATER
#if NET
return writer.Encode(label, static (label, span) => PemEncoding.WriteString(label, span));
#else
int length = writer.GetEncodedLength();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ public bool IgnoreEncryptedAuthSafes
/// <see langword="false" /> to fail loading when duplicate attributes are found.
/// The default is <see langword="false" />.
/// </value>
#if NET10_0_OR_GREATER
#if NET
public
#else
internal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ internal static byte[] AsciiBytes(string s)

internal static byte[] HexToByteArray(this string hexString)
{
#if NET9_0_OR_GREATER
#if NET
return Convert.FromHexString(hexString);
#else
byte[] bytes = new byte[hexString.Length / 2];
Expand Down Expand Up @@ -56,7 +56,7 @@ internal static string ByteArrayToHex(this ReadOnlyMemory<byte> bytes)

internal static string ByteArrayToHex(this ReadOnlySpan<byte> bytes)
{
#if NET9_0_OR_GREATER
#if NET
return Convert.ToHexString(bytes);
#else
StringBuilder builder = new StringBuilder(bytes.Length * 2);
Expand Down Expand Up @@ -89,7 +89,7 @@ internal static byte[] RepeatByte(byte b, int count)

internal static bool ContainsAnyExcept(this ReadOnlySpan<byte> bytes, byte value)
{
#if NET9_0_OR_GREATER
#if NET
return bytes.ContainsAnyExcept(value);
#else
foreach (byte b in bytes)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,7 @@ public void LoadWithDuplicateAttributes(bool allowDuplicates)
{
Pkcs12LoaderLimits limits = Pkcs12LoaderLimits.Defaults;

#if !NET10_0_OR_GREATER
#if !NET
if (allowDuplicates)
{
limits = Pkcs12LoaderLimits.DangerousNoLimits;
Expand All @@ -759,7 +759,7 @@ public void LoadWithDuplicateAttributes(bool allowDuplicates)
// remove the edit lock
limits = new Pkcs12LoaderLimits(limits)
{
#if NET10_0_OR_GREATER
#if NET
AllowDuplicateAttributes = allowDuplicates,
#endif
PreserveCertificateAlias = false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,7 @@ public void LoadWithDuplicateAttributes(bool allowDuplicates)
{
Pkcs12LoaderLimits limits = Pkcs12LoaderLimits.Defaults;

#if !NET10_0_OR_GREATER
#if !NET
if (allowDuplicates)
{
limits = Pkcs12LoaderLimits.DangerousNoLimits;
Expand All @@ -754,7 +754,7 @@ public void LoadWithDuplicateAttributes(bool allowDuplicates)
// remove the edit lock
limits = new Pkcs12LoaderLimits(limits)
{
#if NET10_0_OR_GREATER
#if NET
AllowDuplicateAttributes = allowDuplicates,
#endif
PreserveCertificateAlias = false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,9 @@
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.AesGcm))]
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.AuthenticationTagMismatchException))]
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.SP800108HmacCounterKdf))]
#endif
#if NET9_0_OR_GREATER
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.X509Certificates.Pkcs12LoaderLimits))]
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.X509Certificates.Pkcs12LoadLimitExceededException))]
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.X509Certificates.X509CertificateLoader))]
#endif
#if NET10_0_OR_GREATER
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.CompositeMLDsa))]
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.CompositeMLDsaAlgorithm))]
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.CompositeMLDsaCng))]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

using System.Diagnostics.CodeAnalysis;

#if !NET10_0_OR_GREATER
#if !NET
using System.Formats.Asn1;
using System.Security.Cryptography.Asn1;
#endif

#if !NET10_0_OR_GREATER && !NETSTANDARD
#if !NET && !NETSTANDARD
using System.Diagnostics;
using Internal.Cryptography;
#endif
Expand Down Expand Up @@ -43,7 +43,7 @@ public static class X509CertificateKeyAccessors
{
ArgumentNullException.ThrowIfNull(certificate);

#if NET10_0_OR_GREATER
#if NET
return certificate.GetMLKemPublicKey();
#else
if (MLKemAlgorithm.FromOid(certificate.GetKeyAlgorithm()) is null)
Expand Down Expand Up @@ -88,7 +88,7 @@ public static class X509CertificateKeyAccessors
{
ArgumentNullException.ThrowIfNull(certificate);

#if NET10_0_OR_GREATER
#if NET
return certificate.GetMLKemPrivateKey();
#else
throw new PlatformNotSupportedException(SR.Format(SR.Cryptography_AlgorithmNotSupported, nameof(MLKem)));
Expand Down Expand Up @@ -127,7 +127,7 @@ public static X509Certificate2 CopyWithPrivateKey(this X509Certificate2 certific
ArgumentNullException.ThrowIfNull(certificate);
ArgumentNullException.ThrowIfNull(privateKey);

#if NET10_0_OR_GREATER
#if NET
return certificate.CopyWithPrivateKey(privateKey);
#else
throw new PlatformNotSupportedException(SR.Format(SR.Cryptography_AlgorithmNotSupported, nameof(MLKem)));
Expand Down Expand Up @@ -157,7 +157,7 @@ public static X509Certificate2 CopyWithPrivateKey(this X509Certificate2 certific
{
ArgumentNullException.ThrowIfNull(certificate);

#if NET10_0_OR_GREATER
#if NET
return certificate.GetMLDsaPublicKey();
#else
if (MLDsaAlgorithm.GetMLDsaAlgorithmFromOid(certificate.GetKeyAlgorithm()) is null)
Expand Down Expand Up @@ -202,7 +202,7 @@ public static X509Certificate2 CopyWithPrivateKey(this X509Certificate2 certific
{
ArgumentNullException.ThrowIfNull(certificate);

#if NET10_0_OR_GREATER
#if NET
return certificate.GetMLDsaPrivateKey();
#elif NETSTANDARD
throw new PlatformNotSupportedException(SR.Format(SR.Cryptography_AlgorithmNotSupported, nameof(MLDsa)));
Expand Down Expand Up @@ -253,7 +253,7 @@ public static X509Certificate2 CopyWithPrivateKey(this X509Certificate2 certific
ArgumentNullException.ThrowIfNull(certificate);
ArgumentNullException.ThrowIfNull(privateKey);

#if NET10_0_OR_GREATER
#if NET
return certificate.CopyWithPrivateKey(privateKey);
#elif NETSTANDARD
throw new PlatformNotSupportedException(SR.Format(SR.Cryptography_AlgorithmNotSupported, nameof(MLDsa)));
Expand Down Expand Up @@ -316,7 +316,7 @@ public static X509Certificate2 CopyWithPrivateKey(this X509Certificate2 certific
{
ArgumentNullException.ThrowIfNull(certificate);

#if NET10_0_OR_GREATER
#if NET
return certificate.GetSlhDsaPublicKey();
#else
throw new PlatformNotSupportedException(SR.Format(SR.Cryptography_AlgorithmNotSupported, nameof(SlhDsa)));
Expand Down Expand Up @@ -346,7 +346,7 @@ public static X509Certificate2 CopyWithPrivateKey(this X509Certificate2 certific
{
ArgumentNullException.ThrowIfNull(certificate);

#if NET10_0_OR_GREATER
#if NET
return certificate.GetSlhDsaPrivateKey();
#else
throw new PlatformNotSupportedException(SR.Format(SR.Cryptography_AlgorithmNotSupported, nameof(SlhDsa)));
Expand Down Expand Up @@ -388,7 +388,7 @@ public static X509Certificate2 CopyWithPrivateKey(this X509Certificate2 certific
ArgumentNullException.ThrowIfNull(certificate);
ArgumentNullException.ThrowIfNull(privateKey);

#if NET10_0_OR_GREATER
#if NET
return certificate.CopyWithPrivateKey(privateKey);
#else
throw new PlatformNotSupportedException(SR.Format(SR.Cryptography_AlgorithmNotSupported, nameof(SlhDsa)));
Expand Down Expand Up @@ -418,7 +418,7 @@ public static X509Certificate2 CopyWithPrivateKey(this X509Certificate2 certific
{
ArgumentNullException.ThrowIfNull(certificate);

#if NET10_0_OR_GREATER
#if NET
return certificate.GetCompositeMLDsaPublicKey();
#else
if (CompositeMLDsaAlgorithm.GetAlgorithmFromOid(certificate.GetKeyAlgorithm()) is null)
Expand Down Expand Up @@ -463,7 +463,7 @@ public static X509Certificate2 CopyWithPrivateKey(this X509Certificate2 certific
{
ArgumentNullException.ThrowIfNull(certificate);

#if NET10_0_OR_GREATER
#if NET
return certificate.GetCompositeMLDsaPrivateKey();
#else
if (CompositeMLDsaAlgorithm.GetAlgorithmFromOid(certificate.GetKeyAlgorithm()) is null)
Expand Down Expand Up @@ -507,7 +507,7 @@ public static X509Certificate2 CopyWithPrivateKey(this X509Certificate2 certific
ArgumentNullException.ThrowIfNull(certificate);
ArgumentNullException.ThrowIfNull(privateKey);

#if NET10_0_OR_GREATER
#if NET
return certificate.CopyWithPrivateKey(privateKey);
#elif NETSTANDARD
throw new PlatformNotSupportedException(SR.Format(SR.Cryptography_AlgorithmNotSupported, nameof(CompositeMLDsa)));
Expand Down Expand Up @@ -543,7 +543,7 @@ public static X509Certificate2 CopyWithPrivateKey(this X509Certificate2 certific
#endif
}

#if !NET10_0_OR_GREATER
#if !NET
private static ArraySegment<byte> GetCertificateSubjectPublicKeyInfo(X509Certificate2 certificate)
{
// We construct the SubjectPublicKeyInfo from the certificate as-is, parameters and all. Consumers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,5 @@
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Range))]
#if NET
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Text.Unicode.Utf8))]
#endif
#if NET9_0_OR_GREATER
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Buffers.Text.Base64Url))]
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public ValueTask<T> GetOrCreateAsync<T>(string key, Func<CancellationToken, Valu
HybridCacheEntryOptions? options = null, IEnumerable<string>? tags = null, CancellationToken cancellationToken = default)
=> GetOrCreateAsync(key, factory, WrappedCallbackCache<T>.Instance, options, tags, cancellationToken);

#if NET10_0_OR_GREATER
#if NET
/// <summary>
/// Asynchronously gets the value associated with the key if it exists, or generates a new entry using the provided key and a value from the given factory if the key is not found.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ public bool TryGetValue(object key, out object? result)
return PostProcessTryGetValue(coherentState, entry, out result);
}

#if NET9_0_OR_GREATER
#if NET
/// <summary>
/// Gets the item associated with this key if present.
/// </summary>
Expand Down Expand Up @@ -720,7 +720,7 @@ private sealed class CoherentState
#endif
private readonly ConcurrentDictionary<object, CacheEntry> _nonStringEntries = [];

#if NET9_0_OR_GREATER
#if NET
private readonly ConcurrentDictionary<string, CacheEntry>.AlternateLookup<ReadOnlySpan<char>> _stringAltLookup;

public CoherentState()
Expand All @@ -734,7 +734,7 @@ public CoherentState()
internal bool TryGetValue(object key, [NotNullWhen(true)] out CacheEntry? entry)
=> key is string s ? _stringEntries.TryGetValue(s, out entry) : _nonStringEntries.TryGetValue(key, out entry);

#if NET9_0_OR_GREATER
#if NET
internal bool TryGetValue(ReadOnlySpan<char> key, [NotNullWhen(true)] out CacheEntry? entry)
=> _stringAltLookup.TryGetValue(key, out entry);
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void SimpleStringAccess()
Assert.False(cache.TryGetValue(objKey, out result));
Assert.Null(result);

#if NET9_0_OR_GREATER
#if NET
Assert.False(cache.TryGetValue(stringSpanKey, out result));
Assert.Null(result);

Expand All @@ -48,7 +48,7 @@ public void SimpleStringAccess()
Assert.True(cache.TryGetValue(objKey, out result));
Assert.Same(cachedValue, result);

#if NET9_0_OR_GREATER
#if NET
Assert.True(cache.TryGetValue(stringSpanKey, out result));
Assert.Same(cachedValue, result);

Expand All @@ -65,7 +65,7 @@ public void SimpleStringAccess()
Assert.False(cache.TryGetValue(objKey, out result));
Assert.Null(result);

#if NET9_0_OR_GREATER
#if NET
Assert.False(cache.TryGetValue(stringSpanKey, out result));
Assert.Null(result);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1094,7 +1094,7 @@ private static bool TypeIsASetInterface(Type type)
}

private static List<PropertyInfo> GetAllProperties(
#if NET10_0_OR_GREATER
#if NET
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.AllProperties)]
#else
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ public override string ToString()

if (KeyedImplementationFactory != null)
{
#if NET9_0_OR_GREATER
#if NET
DiagnosticMethodInfo? dmi = DiagnosticMethodInfo.Create(KeyedImplementationFactory);
string declaringTypeName = dmi?.DeclaringTypeName ?? "?";
string methodName = dmi?.Name ?? "?";
Expand All @@ -287,7 +287,7 @@ public override string ToString()

if (ImplementationFactory != null)
{
#if NET9_0_OR_GREATER
#if NET
DiagnosticMethodInfo? dmi = DiagnosticMethodInfo.Create(ImplementationFactory);
string declaringTypeName = dmi?.DeclaringTypeName ?? "?";
string methodName = dmi?.Name ?? "?";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1139,7 +1139,7 @@ public class NonKeyedServiceProvider : IServiceProvider
public object GetService(Type serviceType) => throw new NotImplementedException();
}

#if NET10_0_OR_GREATER
#if NET
[Fact]
public void ResolveKeyedServiceWithFromServiceKeyAttribute()
{
Expand Down
4 changes: 2 additions & 2 deletions src/libraries/Microsoft.Extensions.Options/gen/Emitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,7 @@ private void GenModelValidationMethod(
// We disable the warning on `new ValidationContext(object)` usage as we use it in a safe way that not require executing the reflection code.
// This is done by initializing the DisplayName in the context which is the part trigger reflection if it is not initialized. For
// projects targeting .NET 10 and above, we can avoid the suppression since we use the new trim-safe constructor.
OutLn("#if !NET10_0_OR_GREATER");
OutLn("#if !NET");
OutLn($"[System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessage(\"Trimming\", \"IL2026:RequiresUnreferencedCode\",");
OutLn($" Justification = \"The created ValidationContext object is used in a way that never call reflection\")]");
OutLn("#endif");
Expand All @@ -692,7 +692,7 @@ private void GenModelValidationMethod(
OutLn($"public {(makeStatic ? "static " : string.Empty)}global::Microsoft.Extensions.Options.ValidateOptionsResult Validate(string? name, {modelToValidate.Name} options)");
OutOpenBrace();
OutLn($"global::Microsoft.Extensions.Options.ValidateOptionsResultBuilder? builder = null;");
OutLn("#if NET10_0_OR_GREATER");
OutLn("#if NET");
OutLn($"var context = new {StaticValidationContextType}(options, \"{modelToValidate.SimpleName}\", null, null);");
OutLn("#else");
OutLn($"var context = new {StaticValidationContextType}(options);");
Expand Down
Loading
Loading