diff --git a/src/libraries/Common/src/Interop/Windows/CryptUI/Interop.CryptUIDlgCertificate.cs b/src/libraries/Common/src/Interop/Windows/CryptUI/Interop.CryptUIDlgCertificate.cs index 04925e171d71f1..a82ac99918aeba 100644 --- a/src/libraries/Common/src/Interop/Windows/CryptUI/Interop.CryptUIDlgCertificate.cs +++ b/src/libraries/Common/src/Interop/Windows/CryptUI/Interop.CryptUIDlgCertificate.cs @@ -15,7 +15,7 @@ internal sealed class CRYPTUI_VIEWCERTIFICATE_STRUCTW internal uint dwSize; internal IntPtr hwndParent; internal uint dwFlags; - internal string szTitle; + internal string? szTitle; internal IntPtr pCertContext; internal IntPtr rgszPurposes; internal uint cPurposes; @@ -38,9 +38,9 @@ internal sealed class CRYPTUI_SELECTCERTIFICATE_STRUCTW internal uint dwSize; internal IntPtr hwndParent; internal uint dwFlags; - internal string szTitle; + internal string? szTitle; internal uint dwDontUseColumn; - internal string szDisplayString; + internal string? szDisplayString; internal IntPtr pFilterCallback; internal IntPtr pDisplayCallback; internal IntPtr pvCallbackData; diff --git a/src/libraries/Common/src/Interop/Windows/WinMm/Interop.PlaySound.cs b/src/libraries/Common/src/Interop/Windows/WinMm/Interop.PlaySound.cs index 14b789bf118069..c87142948c54f1 100644 --- a/src/libraries/Common/src/Interop/Windows/WinMm/Interop.PlaySound.cs +++ b/src/libraries/Common/src/Interop/Windows/WinMm/Interop.PlaySound.cs @@ -21,6 +21,6 @@ internal static partial class WinMM internal static partial bool PlaySound(string soundName, IntPtr hmod, int soundFlags); [GeneratedDllImport(Libraries.WinMM, EntryPoint = "PlaySoundW", ExactSpelling = true)] - internal static partial bool PlaySound(byte[] soundName, IntPtr hmod, int soundFlags); + internal static partial bool PlaySound(byte[]? soundName, IntPtr hmod, int soundFlags); } } diff --git a/src/libraries/System.Windows.Extensions/ref/System.Windows.Extensions.cs b/src/libraries/System.Windows.Extensions/ref/System.Windows.Extensions.cs index 057fbfc5ed3189..c1f732f2baad40 100644 --- a/src/libraries/System.Windows.Extensions/ref/System.Windows.Extensions.cs +++ b/src/libraries/System.Windows.Extensions/ref/System.Windows.Extensions.cs @@ -10,17 +10,17 @@ namespace System.Media public partial class SoundPlayer : System.ComponentModel.Component, System.Runtime.Serialization.ISerializable { public SoundPlayer() { } - public SoundPlayer(System.IO.Stream stream) { } + public SoundPlayer(System.IO.Stream? stream) { } protected SoundPlayer(System.Runtime.Serialization.SerializationInfo serializationInfo, System.Runtime.Serialization.StreamingContext context) { } public SoundPlayer(string soundLocation) { } public bool IsLoadCompleted { get { throw null; } } public int LoadTimeout { get { throw null; } set { } } public string SoundLocation { get { throw null; } set { } } - public System.IO.Stream Stream { get { throw null; } set { } } - public object Tag { get { throw null; } set { } } - public event System.ComponentModel.AsyncCompletedEventHandler LoadCompleted { add { } remove { } } - public event System.EventHandler SoundLocationChanged { add { } remove { } } - public event System.EventHandler StreamChanged { add { } remove { } } + public System.IO.Stream? Stream { get { throw null; } set { } } + public object? Tag { get { throw null; } set { } } + public event System.ComponentModel.AsyncCompletedEventHandler? LoadCompleted { add { } remove { } } + public event System.EventHandler? SoundLocationChanged { add { } remove { } } + public event System.EventHandler? StreamChanged { add { } remove { } } public void Load() { } public void LoadAsync() { } protected virtual void OnLoadCompleted(System.ComponentModel.AsyncCompletedEventArgs e) { } @@ -53,8 +53,8 @@ public sealed partial class X509Certificate2UI public X509Certificate2UI() { } public static void DisplayCertificate(System.Security.Cryptography.X509Certificates.X509Certificate2 certificate) { } public static void DisplayCertificate(System.Security.Cryptography.X509Certificates.X509Certificate2 certificate, System.IntPtr hwndParent) { } - public static System.Security.Cryptography.X509Certificates.X509Certificate2Collection SelectFromCollection(System.Security.Cryptography.X509Certificates.X509Certificate2Collection certificates, string title, string message, System.Security.Cryptography.X509Certificates.X509SelectionFlag selectionFlag) { throw null; } - public static System.Security.Cryptography.X509Certificates.X509Certificate2Collection SelectFromCollection(System.Security.Cryptography.X509Certificates.X509Certificate2Collection certificates, string title, string message, System.Security.Cryptography.X509Certificates.X509SelectionFlag selectionFlag, System.IntPtr hwndParent) { throw null; } + public static System.Security.Cryptography.X509Certificates.X509Certificate2Collection SelectFromCollection(System.Security.Cryptography.X509Certificates.X509Certificate2Collection certificates, string? title, string? message, System.Security.Cryptography.X509Certificates.X509SelectionFlag selectionFlag) { throw null; } + public static System.Security.Cryptography.X509Certificates.X509Certificate2Collection SelectFromCollection(System.Security.Cryptography.X509Certificates.X509Certificate2Collection certificates, string? title, string? message, System.Security.Cryptography.X509Certificates.X509SelectionFlag selectionFlag, System.IntPtr hwndParent) { throw null; } } public enum X509SelectionFlag { @@ -68,7 +68,7 @@ public partial class XamlAccessLevel { internal XamlAccessLevel() { } public System.Reflection.AssemblyName AssemblyAccessToAssemblyName { get { throw null; } } - public string PrivateAccessToTypeName { get { throw null; } } + public string? PrivateAccessToTypeName { get { throw null; } } public static System.Xaml.Permissions.XamlAccessLevel AssemblyAccessTo(System.Reflection.Assembly assembly) { throw null; } public static System.Xaml.Permissions.XamlAccessLevel AssemblyAccessTo(System.Reflection.AssemblyName assemblyName) { throw null; } public static System.Xaml.Permissions.XamlAccessLevel PrivateAccessTo(string assemblyQualifiedTypeName) { throw null; } diff --git a/src/libraries/System.Windows.Extensions/ref/System.Windows.Extensions.csproj b/src/libraries/System.Windows.Extensions/ref/System.Windows.Extensions.csproj index 324bdf24af2466..76ce42118ef059 100644 --- a/src/libraries/System.Windows.Extensions/ref/System.Windows.Extensions.csproj +++ b/src/libraries/System.Windows.Extensions/ref/System.Windows.Extensions.csproj @@ -1,6 +1,7 @@ $(NetCoreAppCurrent);$(NetCoreAppMinimum) + enable diff --git a/src/libraries/System.Windows.Extensions/src/System.Windows.Extensions.csproj b/src/libraries/System.Windows.Extensions/src/System.Windows.Extensions.csproj index a35f1040ba9420..6e587b9c50f8f8 100644 --- a/src/libraries/System.Windows.Extensions/src/System.Windows.Extensions.csproj +++ b/src/libraries/System.Windows.Extensions/src/System.Windows.Extensions.csproj @@ -3,6 +3,7 @@ true annotations $(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent);$(NetCoreAppMinimum)-windows;$(NetCoreAppMinimum) + enable true Provides miscellaneous Windows-specific types diff --git a/src/libraries/System.Windows.Extensions/src/System/Media/SoundPlayer.cs b/src/libraries/System.Windows.Extensions/src/System/Media/SoundPlayer.cs index 336a7b4cd8344b..f84f503682efcf 100644 --- a/src/libraries/System.Windows.Extensions/src/System/Media/SoundPlayer.cs +++ b/src/libraries/System.Windows.Extensions/src/System/Media/SoundPlayer.cs @@ -3,6 +3,7 @@ using System.ComponentModel; using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; using System.IO; using System.Net; using System.Runtime.InteropServices; @@ -18,7 +19,7 @@ public class SoundPlayer : Component, ISerializable private const int BlockSize = 1024; private const int DefaultLoadTimeout = 10000; // 10 secs - private Uri _uri; + private Uri? _uri; private string _soundLocation = string.Empty; private int _loadTimeout = DefaultLoadTimeout; @@ -28,16 +29,16 @@ public class SoundPlayer : Component, ISerializable // the worker copyTask // we start the worker copyTask ONLY from entry points in the SoundPlayer API // we also set the tread to null only from the entry points in the SoundPlayer API - private Task _copyTask; - private CancellationTokenSource _copyTaskCancellation; + private Task? _copyTask; + private CancellationTokenSource? _copyTaskCancellation; // local buffer information private int _currentPos; - private Stream _stream; - private Exception _lastLoadException; + private Stream? _stream; + private Exception? _lastLoadException; private bool _doesLoadAppearSynchronous; - private byte[] _streamData; - private AsyncOperation _asyncOperation; + private byte[]? _streamData; + private AsyncOperation? _asyncOperation; private readonly SendOrPostCallback _loadAsyncOperationCompleted; // event @@ -55,7 +56,7 @@ public SoundPlayer(string soundLocation) : this() SetupSoundLocation(soundLocation ?? string.Empty); } - public SoundPlayer(Stream stream) : this() + public SoundPlayer(Stream? stream) : this() { _stream = stream; } @@ -99,7 +100,7 @@ public string SoundLocation } } - public Stream Stream + public Stream? Stream { get { @@ -126,7 +127,7 @@ public Stream Stream public bool IsLoadCompleted { get; private set; } - public object Tag { get; set; } + public object? Tag { get; set; } public void LoadAsync() { @@ -161,9 +162,9 @@ public void LoadAsync() LoadStream(false); } - private void LoadAsyncOperationCompleted(object arg) + private void LoadAsyncOperationCompleted(object? arg) { - OnLoadCompleted((AsyncCompletedEventArgs)arg); + OnLoadCompleted((AsyncCompletedEventArgs)arg!); } // called for loading a stream synchronously @@ -227,6 +228,7 @@ private void LoadAndPlay(int flags) else { LoadSync(); + Debug.Assert(_streamData != null); ValidateSoundData(_streamData); Interop.WinMM.PlaySound(_streamData, IntPtr.Zero, Interop.WinMM.SND_MEMORY | Interop.WinMM.SND_NODEFAULT | flags); } @@ -271,7 +273,9 @@ private void LoadSync() _stream = webResponse.GetResponseStream(); } - if (_stream.CanSeek) + // DO NOT assert - NRE is expected for null stream + // See SoundPlayerTests.Load_NullStream_ThrowsNullReferenceException + if (_stream!.CanSeek) { // if we can get data synchronously, then get it LoadStream(true); @@ -304,6 +308,7 @@ private void LoadSync() private void LoadStream(bool loadSync) { + Debug.Assert(_stream != null); if (loadSync && _stream.CanSeek) { int streamLen = (int)_stream.Length; @@ -339,9 +344,9 @@ public void PlayLooping() LoadAndPlay(Interop.WinMM.SND_LOOP | Interop.WinMM.SND_ASYNC); } - private static Uri ResolveUri(string partialUri) + private static Uri? ResolveUri(string partialUri) { - Uri result = null; + Uri? result = null; try { result = new Uri(partialUri); @@ -399,7 +404,7 @@ private void SetupSoundLocation(string soundLocation) } } - private void SetupStream(Stream stream) + private void SetupStream(Stream? stream) { if (_copyTask != null) { @@ -420,10 +425,10 @@ private void SetupStream(Stream stream) public void Stop() { - Interop.WinMM.PlaySound((byte[])null, IntPtr.Zero, Interop.WinMM.SND_PURGE); + Interop.WinMM.PlaySound((byte[]?)null, IntPtr.Zero, Interop.WinMM.SND_PURGE); } - public event AsyncCompletedEventHandler LoadCompleted + public event AsyncCompletedEventHandler? LoadCompleted { add { @@ -435,7 +440,7 @@ public event AsyncCompletedEventHandler LoadCompleted } } - public event EventHandler SoundLocationChanged + public event EventHandler? SoundLocationChanged { add { @@ -447,7 +452,7 @@ public event EventHandler SoundLocationChanged } } - public event EventHandler StreamChanged + public event EventHandler? StreamChanged { add { @@ -461,22 +466,22 @@ public event EventHandler StreamChanged protected virtual void OnLoadCompleted(AsyncCompletedEventArgs e) { - ((AsyncCompletedEventHandler)Events[s_eventLoadCompleted])?.Invoke(this, e); + ((AsyncCompletedEventHandler?)Events[s_eventLoadCompleted])?.Invoke(this, e); } protected virtual void OnSoundLocationChanged(EventArgs e) { - ((EventHandler)Events[s_eventSoundLocationChanged])?.Invoke(this, e); + ((EventHandler?)Events[s_eventSoundLocationChanged])?.Invoke(this, e); } protected virtual void OnStreamChanged(EventArgs e) { - ((EventHandler)Events[s_eventStreamChanged])?.Invoke(this, e); + ((EventHandler?)Events[s_eventStreamChanged])?.Invoke(this, e); } private async Task CopyStreamAsync(CancellationToken cancellationToken) { - Exception exception = null; + Exception? exception = null; try { // setup the http stream @@ -485,7 +490,7 @@ private async Task CopyStreamAsync(CancellationToken cancellationToken) #pragma warning disable SYSLIB0014 // WebRequest, HttpWebRequest, ServicePoint, and WebClient are obsolete. Use HttpClient instead. WebRequest webRequest = WebRequest.Create(_uri); #pragma warning restore SYSLIB0014 - using (cancellationToken.Register(r => ((WebRequest)r).Abort(), webRequest)) + using (cancellationToken.Register(r => ((WebRequest)r!).Abort(), webRequest)) { WebResponse webResponse = await webRequest.GetResponseAsync().ConfigureAwait(false); _stream = webResponse.GetResponseStream(); @@ -494,6 +499,7 @@ private async Task CopyStreamAsync(CancellationToken cancellationToken) _streamData = new byte[BlockSize]; + Debug.Assert(_stream != null); int readBytes = await _stream.ReadAsync(_streamData.AsMemory(_currentPos, BlockSize), cancellationToken).ConfigureAwait(false); int totalBytes = readBytes; @@ -525,6 +531,7 @@ private async Task CopyStreamAsync(CancellationToken cancellationToken) AsyncCompletedEventArgs ea = exception is OperationCanceledException ? new AsyncCompletedEventArgs(null, cancelled: true, null) : new AsyncCompletedEventArgs(exception, cancelled: false, null); + Debug.Assert(_asyncOperation != null); _asyncOperation.PostOperationCompleted(_loadAsyncOperationCompleted, ea); } } @@ -539,7 +546,7 @@ private unsafe void ValidateSoundFile(string fileName) try { - Interop.WinMM.WAVEFORMATEX waveFormat = null; + Interop.WinMM.WAVEFORMATEX? waveFormat = null; var ckRIFF = new Interop.WinMM.MMCKINFO() { fccType = mmioFOURCC('W', 'A', 'V', 'E') diff --git a/src/libraries/System.Windows.Extensions/src/System/Media/SystemSounds.cs b/src/libraries/System.Windows.Extensions/src/System/Media/SystemSounds.cs index 145b53cd8c1060..b6bffe257a733a 100644 --- a/src/libraries/System.Windows.Extensions/src/System/Media/SystemSounds.cs +++ b/src/libraries/System.Windows.Extensions/src/System/Media/SystemSounds.cs @@ -5,11 +5,11 @@ namespace System.Media { public static class SystemSounds { - private static volatile SystemSound s_asterisk; - private static volatile SystemSound s_beep; - private static volatile SystemSound s_exclamation; - private static volatile SystemSound s_hand; - private static volatile SystemSound s_question; + private static volatile SystemSound? s_asterisk; + private static volatile SystemSound? s_beep; + private static volatile SystemSound? s_exclamation; + private static volatile SystemSound? s_hand; + private static volatile SystemSound? s_question; public static SystemSound Asterisk { diff --git a/src/libraries/System.Windows.Extensions/src/System/Security/Cryptography/X509Certificates/X509Certificate2UI.cs b/src/libraries/System.Windows.Extensions/src/System/Security/Cryptography/X509Certificates/X509Certificate2UI.cs index 7f26dcf23e6fde..2f0db1203faca7 100644 --- a/src/libraries/System.Windows.Extensions/src/System/Security/Cryptography/X509Certificates/X509Certificate2UI.cs +++ b/src/libraries/System.Windows.Extensions/src/System/Security/Cryptography/X509Certificates/X509Certificate2UI.cs @@ -31,12 +31,12 @@ public static void DisplayCertificate(X509Certificate2 certificate, IntPtr hwndP DisplayX509Certificate(certificate, hwndParent); } - public static X509Certificate2Collection SelectFromCollection(X509Certificate2Collection certificates, string title, string message, X509SelectionFlag selectionFlag) + public static X509Certificate2Collection SelectFromCollection(X509Certificate2Collection certificates, string? title, string? message, X509SelectionFlag selectionFlag) { return SelectFromCollectionHelper(certificates, title, message, selectionFlag, IntPtr.Zero); } - public static X509Certificate2Collection SelectFromCollection(X509Certificate2Collection certificates, string title, string message, X509SelectionFlag selectionFlag, IntPtr hwndParent) + public static X509Certificate2Collection SelectFromCollection(X509Certificate2Collection certificates, string? title, string? message, X509SelectionFlag selectionFlag, IntPtr hwndParent) { return SelectFromCollectionHelper(certificates, title, message, selectionFlag, hwndParent); } @@ -82,7 +82,7 @@ private static void DisplayX509Certificate(X509Certificate2 certificate, IntPtr } } - private static X509Certificate2Collection SelectFromCollectionHelper(X509Certificate2Collection certificates, string title, string message, X509SelectionFlag selectionFlag, IntPtr hwndParent) + private static X509Certificate2Collection SelectFromCollectionHelper(X509Certificate2Collection certificates, string? title, string? message, X509SelectionFlag selectionFlag, IntPtr hwndParent) { if (certificates == null) throw new ArgumentNullException(nameof(certificates)); @@ -96,7 +96,7 @@ private static X509Certificate2Collection SelectFromCollectionHelper(X509Certifi } } - private static unsafe SafeCertStoreHandle SelectFromStore(SafeCertStoreHandle safeSourceStoreHandle, string title, string message, X509SelectionFlag selectionFlags, IntPtr hwndParent) + private static unsafe SafeCertStoreHandle SelectFromStore(SafeCertStoreHandle safeSourceStoreHandle, string? title, string? message, X509SelectionFlag selectionFlags, IntPtr hwndParent) { int dwErrorCode = ERROR_SUCCESS; diff --git a/src/libraries/System.Windows.Extensions/src/System/Xaml/Permissions/XamlAccessLevel.cs b/src/libraries/System.Windows.Extensions/src/System/Xaml/Permissions/XamlAccessLevel.cs index 123543a8024439..3c4d510fd3a17b 100644 --- a/src/libraries/System.Windows.Extensions/src/System/Xaml/Permissions/XamlAccessLevel.cs +++ b/src/libraries/System.Windows.Extensions/src/System/Xaml/Permissions/XamlAccessLevel.cs @@ -8,7 +8,7 @@ namespace System.Xaml.Permissions { public class XamlAccessLevel { - private XamlAccessLevel(string assemblyName, string typeName) + private XamlAccessLevel(string assemblyName, string? typeName) { AssemblyNameString = assemblyName; PrivateAccessToTypeName = typeName; @@ -16,7 +16,7 @@ private XamlAccessLevel(string assemblyName, string typeName) public static XamlAccessLevel AssemblyAccessTo(Assembly assembly) { - return new XamlAccessLevel(assembly.FullName, null); + return new XamlAccessLevel(assembly.FullName!, null); } public static XamlAccessLevel AssemblyAccessTo(AssemblyName assemblyName) @@ -26,7 +26,7 @@ public static XamlAccessLevel AssemblyAccessTo(AssemblyName assemblyName) public static XamlAccessLevel PrivateAccessTo(Type type) { - return new XamlAccessLevel(type.Assembly.FullName, type.FullName); + return new XamlAccessLevel(type.Assembly.FullName!, type.FullName); } public static XamlAccessLevel PrivateAccessTo(string assemblyQualifiedTypeName) @@ -43,7 +43,7 @@ public AssemblyName AssemblyAccessToAssemblyName get { return new AssemblyName(AssemblyNameString); } } - public string PrivateAccessToTypeName { get; private set; } + public string? PrivateAccessToTypeName { get; private set; } internal string AssemblyNameString { get; private set; } }