diff --git a/src/Prism.Core/Commands/AsyncDelegateCommand.cs b/src/Prism.Core/Commands/AsyncDelegateCommand.cs index e29562eef9..ad4082d160 100644 --- a/src/Prism.Core/Commands/AsyncDelegateCommand.cs +++ b/src/Prism.Core/Commands/AsyncDelegateCommand.cs @@ -130,7 +130,7 @@ public bool CanExecute() /// Handle the internal invocation of /// /// Command Parameter - protected override async void Execute(object parameter) + protected override async void Execute(object? parameter) { await Execute(_getCancellationToken()); } @@ -140,7 +140,7 @@ protected override async void Execute(object parameter) /// /// /// if the Command Can Execute, otherwise - protected override bool CanExecute(object parameter) + protected override bool CanExecute(object? parameter) { return CanExecute(); } diff --git a/src/Prism.Core/Commands/AsyncDelegateCommand{T}.cs b/src/Prism.Core/Commands/AsyncDelegateCommand{T}.cs index 6565d20003..ed257ce196 100644 --- a/src/Prism.Core/Commands/AsyncDelegateCommand{T}.cs +++ b/src/Prism.Core/Commands/AsyncDelegateCommand{T}.cs @@ -132,11 +132,11 @@ public bool CanExecute(T parameter) /// Handle the internal invocation of /// /// Command Parameter - protected override async void Execute(object parameter) + protected override async void Execute(object? parameter) { try { - await Execute((T)parameter, _getCancellationToken()); + await Execute((T)parameter!, _getCancellationToken()); } catch (Exception ex) { @@ -152,11 +152,11 @@ protected override async void Execute(object parameter) /// /// /// if the Command Can Execute, otherwise - protected override bool CanExecute(object parameter) + protected override bool CanExecute(object? parameter) { try { - return CanExecute((T)parameter); + return CanExecute((T)parameter!); } catch (Exception ex) { @@ -243,7 +243,7 @@ async Task IAsyncCommand.ExecuteAsync(object? parameter) try { // If T is not nullable this may throw an exception - await Execute((T)parameter, _getCancellationToken()); + await Execute((T)parameter!, _getCancellationToken()); } catch (Exception ex) { @@ -259,7 +259,7 @@ async Task IAsyncCommand.ExecuteAsync(object? parameter, CancellationToken cance try { // If T is not nullable this may throw an exception - await Execute((T)parameter, cancellationToken); + await Execute((T)parameter!, cancellationToken); } catch (Exception ex) { diff --git a/src/Prism.Core/Commands/CompositeCommand.cs b/src/Prism.Core/Commands/CompositeCommand.cs index 60f470728c..5ccb5fce0b 100644 --- a/src/Prism.Core/Commands/CompositeCommand.cs +++ b/src/Prism.Core/Commands/CompositeCommand.cs @@ -5,6 +5,7 @@ using System.Windows.Input; using Prism.Properties; +#nullable enable namespace Prism.Commands { /// @@ -12,17 +13,17 @@ namespace Prism.Commands /// public class CompositeCommand : ICommand { - private readonly List _registeredCommands = new List(); + private readonly List _registeredCommands = new(); private readonly bool _monitorCommandActivity; private readonly EventHandler _onRegisteredCommandCanExecuteChangedHandler; - private SynchronizationContext _synchronizationContext; + private readonly SynchronizationContext? _synchronizationContext; /// /// Initializes a new instance of . /// public CompositeCommand() { - this._onRegisteredCommandCanExecuteChangedHandler = new EventHandler(this.OnRegisteredCommandCanExecuteChanged); + _onRegisteredCommandCanExecuteChangedHandler = new EventHandler(OnRegisteredCommandCanExecuteChanged); _synchronizationContext = SynchronizationContext.Current; } @@ -33,7 +34,7 @@ public CompositeCommand() public CompositeCommand(bool monitorCommandActivity) : this() { - this._monitorCommandActivity = monitorCommandActivity; + _monitorCommandActivity = monitorCommandActivity; } /// @@ -53,24 +54,23 @@ public virtual void RegisterCommand(ICommand command) throw new ArgumentException(Resources.CannotRegisterCompositeCommandInItself); } - lock (this._registeredCommands) + lock (_registeredCommands) { - if (this._registeredCommands.Contains(command)) + if (_registeredCommands.Contains(command)) { throw new InvalidOperationException(Resources.CannotRegisterSameCommandTwice); } - this._registeredCommands.Add(command); + _registeredCommands.Add(command); } - command.CanExecuteChanged += this._onRegisteredCommandCanExecuteChangedHandler; - this.OnCanExecuteChanged(); + command.CanExecuteChanged += _onRegisteredCommandCanExecuteChangedHandler; + OnCanExecuteChanged(); - if (this._monitorCommandActivity) + if (_monitorCommandActivity) { - var activeAwareCommand = command as IActiveAware; - if (activeAwareCommand != null) + if (command is IActiveAware activeAwareCommand) { - activeAwareCommand.IsActiveChanged += this.Command_IsActiveChanged; + activeAwareCommand.IsActiveChanged += Command_IsActiveChanged; } } } @@ -83,30 +83,29 @@ public virtual void UnregisterCommand(ICommand command) { if (command == null) throw new ArgumentNullException(nameof(command)); bool removed; - lock (this._registeredCommands) + lock (_registeredCommands) { - removed = this._registeredCommands.Remove(command); + removed = _registeredCommands.Remove(command); } if (removed) { - command.CanExecuteChanged -= this._onRegisteredCommandCanExecuteChangedHandler; - this.OnCanExecuteChanged(); + command.CanExecuteChanged -= _onRegisteredCommandCanExecuteChangedHandler; + OnCanExecuteChanged(); - if (this._monitorCommandActivity) + if (_monitorCommandActivity) { - var activeAwareCommand = command as IActiveAware; - if (activeAwareCommand != null) + if (command is IActiveAware activeAwareCommand) { - activeAwareCommand.IsActiveChanged -= this.Command_IsActiveChanged; + activeAwareCommand.IsActiveChanged -= Command_IsActiveChanged; } } } } - private void OnRegisteredCommandCanExecuteChanged(object sender, EventArgs e) + private void OnRegisteredCommandCanExecuteChanged(object? sender, EventArgs e) { - this.OnCanExecuteChanged(); + OnCanExecuteChanged(); } @@ -118,18 +117,18 @@ private void OnRegisteredCommandCanExecuteChanged(object sender, EventArgs e) /// If the command does not require data to be passed, this object can be set to . /// /// if all of the commands return ; otherwise, . - public virtual bool CanExecute(object parameter) + public virtual bool CanExecute(object? parameter) { bool hasEnabledCommandsThatShouldBeExecuted = false; ICommand[] commandList; - lock (this._registeredCommands) + lock (_registeredCommands) { - commandList = this._registeredCommands.ToArray(); + commandList = _registeredCommands.ToArray(); } foreach (ICommand command in commandList) { - if (this.ShouldExecute(command)) + if (ShouldExecute(command)) { if (!command.CanExecute(parameter)) { @@ -146,7 +145,7 @@ public virtual bool CanExecute(object parameter) /// /// Occurs when any of the registered commands raise . /// - public virtual event EventHandler CanExecuteChanged; + public virtual event EventHandler? CanExecuteChanged; /// /// Forwards to the registered commands. @@ -154,12 +153,12 @@ public virtual bool CanExecute(object parameter) /// Data used by the command. /// If the command does not require data to be passed, this object can be set to . /// - public virtual void Execute(object parameter) + public virtual void Execute(object? parameter) { Queue commands; - lock (this._registeredCommands) + lock (_registeredCommands) { - commands = new Queue(this._registeredCommands.Where(this.ShouldExecute).ToList()); + commands = new Queue(_registeredCommands.Where(ShouldExecute).ToList()); } while (commands.Count > 0) @@ -174,7 +173,7 @@ public virtual void Execute(object parameter) /// /// The command to evaluate. /// A value indicating whether the command should be used - /// when evaluating and . + /// when evaluating and . /// /// If this command is set to monitor command activity, and /// implements the interface, @@ -182,9 +181,7 @@ public virtual void Execute(object parameter) /// property is ; otherwise it always returns . protected virtual bool ShouldExecute(ICommand command) { - var activeAwareCommand = command as IActiveAware; - - if (this._monitorCommandActivity && activeAwareCommand != null) + if (_monitorCommandActivity && command is IActiveAware activeAwareCommand) { return activeAwareCommand.IsActive; } @@ -202,9 +199,9 @@ public IList RegisteredCommands get { IList commandList; - lock (this._registeredCommands) + lock (_registeredCommands) { - commandList = this._registeredCommands.ToList(); + commandList = _registeredCommands.ToList(); } return commandList; @@ -233,9 +230,9 @@ protected virtual void OnCanExecuteChanged() /// /// The sender. /// EventArgs to pass to the event. - private void Command_IsActiveChanged(object sender, EventArgs e) + private void Command_IsActiveChanged(object? sender, EventArgs e) { - this.OnCanExecuteChanged(); + OnCanExecuteChanged(); } } } diff --git a/src/Prism.Core/Commands/DelegateCommand.cs b/src/Prism.Core/Commands/DelegateCommand.cs index 52ad7c5331..cd98aac21b 100644 --- a/src/Prism.Core/Commands/DelegateCommand.cs +++ b/src/Prism.Core/Commands/DelegateCommand.cs @@ -4,6 +4,7 @@ using System.Windows.Input; using Prism.Properties; +#nullable enable namespace Prism.Commands { /// @@ -85,7 +86,7 @@ public bool CanExecute() /// Handle the internal invocation of /// /// Command Parameter - protected override void Execute(object parameter) + protected override void Execute(object? parameter) { Execute(); } @@ -95,7 +96,7 @@ protected override void Execute(object parameter) /// /// /// if the Command Can Execute, otherwise - protected override bool CanExecute(object parameter) + protected override bool CanExecute(object? parameter) { return CanExecute(); } diff --git a/src/Prism.Core/Commands/DelegateCommandBase.cs b/src/Prism.Core/Commands/DelegateCommandBase.cs index 69108eb1b5..24e4aaee54 100644 --- a/src/Prism.Core/Commands/DelegateCommandBase.cs +++ b/src/Prism.Core/Commands/DelegateCommandBase.cs @@ -7,6 +7,7 @@ using Prism.Common; using Prism.Mvvm; +#nullable enable namespace Prism.Commands { /// @@ -16,13 +17,13 @@ public abstract class DelegateCommandBase : BindableBase, ICommand, IActiveAware { private bool _isActive; - private SynchronizationContext _synchronizationContext; - private readonly HashSet _observedPropertiesExpressions = new HashSet(); + private SynchronizationContext? _synchronizationContext; + private readonly HashSet _observedPropertiesExpressions = new(); /// /// Provides an Exception Handler to register callbacks or handle encountered exceptions within /// - protected readonly MulticastExceptionHandler ExceptionHandler = new MulticastExceptionHandler(); + protected readonly MulticastExceptionHandler ExceptionHandler = new(); /// /// Creates a new instance of a , specifying both the execute action and the can execute function. @@ -35,7 +36,7 @@ protected DelegateCommandBase() /// /// Occurs when changes occur that affect whether or not the command should execute. /// - public virtual event EventHandler CanExecuteChanged; + public virtual event EventHandler? CanExecuteChanged; /// /// Raises so every @@ -64,12 +65,12 @@ public void RaiseCanExecuteChanged() OnCanExecuteChanged(); } - void ICommand.Execute(object parameter) + void ICommand.Execute(object? parameter) { Execute(parameter); } - bool ICommand.CanExecute(object parameter) + bool ICommand.CanExecute(object? parameter) { return CanExecute(parameter); } @@ -78,14 +79,14 @@ bool ICommand.CanExecute(object parameter) /// Handle the internal invocation of /// /// Command Parameter - protected abstract void Execute(object parameter); + protected abstract void Execute(object? parameter); /// /// Handle the internal invocation of /// /// /// if the Command Can Execute, otherwise - protected abstract bool CanExecute(object parameter); + protected abstract bool CanExecute(object? parameter); /// /// Observes a property that implements INotifyPropertyChanged, and automatically calls DelegateCommandBase.RaiseCanExecuteChanged on property changed notifications. @@ -96,7 +97,7 @@ protected internal void ObservesPropertyInternal(Expression> property { if (_observedPropertiesExpressions.Contains(propertyExpression.ToString())) { - throw new ArgumentException($"{propertyExpression.ToString()} is already being observed.", + throw new ArgumentException($"{propertyExpression} is already being observed.", nameof(propertyExpression)); } else @@ -121,10 +122,10 @@ public bool IsActive /// /// Fired if the property changes. /// - public virtual event EventHandler IsActiveChanged; + public virtual event EventHandler? IsActiveChanged; /// - /// This raises the event. + /// This raises the event. /// protected virtual void OnIsActiveChanged() { diff --git a/src/Prism.Core/Commands/DelegateCommand{T}.cs b/src/Prism.Core/Commands/DelegateCommand{T}.cs index 8d4bdf7ae8..e9ac660155 100644 --- a/src/Prism.Core/Commands/DelegateCommand{T}.cs +++ b/src/Prism.Core/Commands/DelegateCommand{T}.cs @@ -5,6 +5,7 @@ using System.Windows.Input; using Prism.Properties; +#nullable enable namespace Prism.Commands { /// @@ -121,13 +122,13 @@ public bool CanExecute(T parameter) /// Handle the internal invocation of /// /// Command Parameter - protected override void Execute(object parameter) + protected override void Execute(object? parameter) { try { // Note: We don't call Execute because we would potentially invoke the Try/Catch twice. // It is also needed here incase (T)parameter throws the exception - _executeMethod((T)parameter); + _executeMethod((T)parameter!); } catch (Exception ex) { @@ -143,13 +144,13 @@ protected override void Execute(object parameter) /// /// /// if the Command Can Execute, otherwise - protected override bool CanExecute(object parameter) + protected override bool CanExecute(object? parameter) { try { // Note: We don't call Execute because we would potentially invoke the Try/Catch twice. // It is also needed here incase (T)parameter throws the exception - return CanExecute((T)parameter); + return CanExecute((T)parameter!); } catch (Exception ex) { diff --git a/src/Prism.Core/Commands/PropertyObserver.cs b/src/Prism.Core/Commands/PropertyObserver.cs index 2457823e89..fad41099b9 100644 --- a/src/Prism.Core/Commands/PropertyObserver.cs +++ b/src/Prism.Core/Commands/PropertyObserver.cs @@ -4,6 +4,7 @@ using System.Linq.Expressions; using System.Reflection; +#nullable enable namespace Prism.Commands { /// @@ -20,16 +21,20 @@ private PropertyObserver(Expression propertyExpression, Action action) SubscribeListeners(propertyExpression); } - private void SubscribeListeners(Expression propertyExpression) + private void SubscribeListeners(Expression? propertyExpression) { var propNameStack = new Stack(); while (propertyExpression is MemberExpression temp) // Gets the root of the property chain. { propertyExpression = temp.Expression; - propNameStack.Push(temp.Member as PropertyInfo); // Records the member info as property info + + if (temp.Member is PropertyInfo propertyInfo) + { + propNameStack.Push(propertyInfo); // Records the member info as property info + } } - if (!(propertyExpression is ConstantExpression constantExpression)) + if (propertyExpression is not ConstantExpression constantExpression) throw new NotSupportedException("Operation not supported for the given expression type. " + "Only MemberExpression and ConstantExpression are currently supported."); @@ -42,9 +47,9 @@ private void SubscribeListeners(Expression propertyExpression) previousNode = currentNode; } - object propOwnerObject = constantExpression.Value; + object? propOwnerObject = constantExpression.Value; - if (!(propOwnerObject is INotifyPropertyChanged inpcObject)) + if (propOwnerObject is not INotifyPropertyChanged inpcObject) throw new InvalidOperationException("Trying to subscribe PropertyChanged listener in object that " + $"owns '{propObserverNodeRoot.PropertyInfo.Name}' property, but the object does not implements INotifyPropertyChanged."); diff --git a/src/Prism.Core/Commands/PropertyObserverNode.cs b/src/Prism.Core/Commands/PropertyObserverNode.cs index ba04476e3b..6bb4f6b1c3 100644 --- a/src/Prism.Core/Commands/PropertyObserverNode.cs +++ b/src/Prism.Core/Commands/PropertyObserverNode.cs @@ -2,6 +2,7 @@ using System.ComponentModel; using System.Reflection; +#nullable enable namespace Prism.Commands { /// @@ -11,10 +12,10 @@ namespace Prism.Commands internal class PropertyObserverNode { private readonly Action _action; - private INotifyPropertyChanged _inpcObject; + private INotifyPropertyChanged? _inpcObject; public PropertyInfo PropertyInfo { get; } - public PropertyObserverNode Next { get; set; } + public PropertyObserverNode? Next { get; set; } public PropertyObserverNode(PropertyInfo propertyInfo, Action action) { @@ -40,11 +41,11 @@ private void GenerateNextNode() { var nextProperty = PropertyInfo.GetValue(_inpcObject); if (nextProperty == null) return; - if (!(nextProperty is INotifyPropertyChanged nextInpcObject)) + if (nextProperty is not INotifyPropertyChanged nextInpcObject) throw new InvalidOperationException("Trying to subscribe PropertyChanged listener in object that " + - $"owns '{Next.PropertyInfo.Name}' property, but the object does not implements INotifyPropertyChanged."); + $"owns '{Next?.PropertyInfo.Name}' property, but the object does not implements INotifyPropertyChanged."); - Next.SubscribeListenerFor(nextInpcObject); + Next?.SubscribeListenerFor(nextInpcObject); } private void UnsubscribeListener() @@ -55,7 +56,7 @@ private void UnsubscribeListener() Next?.UnsubscribeListener(); } - private void OnPropertyChanged(object sender, PropertyChangedEventArgs e) + private void OnPropertyChanged(object? sender, PropertyChangedEventArgs e) { if (e?.PropertyName == PropertyInfo.Name || string.IsNullOrEmpty(e?.PropertyName)) {