Skip to content

Commit

Permalink
Merge pull request #47 from ravinderjangra/master
Browse files Browse the repository at this point in the history
Changes from comment
  • Loading branch information
jamesmontemagno authored Sep 12, 2019
2 parents fe14077 + bbe653d commit 4183bff
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 12 deletions.
4 changes: 2 additions & 2 deletions MvvmHelpers/Commands/AsyncCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public event EventHandler CanExecuteChanged
public void RaiseCanExecuteChanged() => weakEventManager.HandleEvent(this, EventArgs.Empty, nameof(CanExecuteChanged));

#region Explicit implementations
void ICommand.Execute(object parameter) => ExecuteAsync().SafeFireAndForgetAsync(onException, continueOnCapturedContext);
void ICommand.Execute(object parameter) => ExecuteAsync().SafeFireAndForget(onException, continueOnCapturedContext);
#endregion
}
/// <summary>
Expand Down Expand Up @@ -87,7 +87,7 @@ public event EventHandler CanExecuteChanged
void ICommand.Execute(object parameter)
{
if (CommandUtils.IsValidCommandParameter<T>(parameter))
ExecuteAsync((T)parameter).SafeFireAndForgetAsync(onException, continueOnCapturedContext);
ExecuteAsync((T)parameter).SafeFireAndForget(onException, continueOnCapturedContext);

}
#endregion
Expand Down
4 changes: 2 additions & 2 deletions MvvmHelpers/Commands/CommandUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ internal static bool IsValidCommandParameter<T>(object o)
valid = o is T;

if (!valid)
throw new InvalidCommandParameterException(typeof(T), null);
throw new InvalidCommandParameterException(typeof(T), o.GetType());

return valid;
}
Expand All @@ -32,7 +32,7 @@ internal static bool IsValidCommandParameter<T>(object o)
valid = !t.GetTypeInfo().IsValueType;

if (!valid)
throw new InvalidCommandParameterException(typeof(T), null);
throw new InvalidCommandParameterException(typeof(T));

return valid;
}
Expand Down
22 changes: 22 additions & 0 deletions MvvmHelpers/Exceptions/InvalidCommandParameterException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ public InvalidCommandParameterException(Type expectedType, Type actualType, Exce

}

/// <summary>
/// Initializes a new instance of the <see cref="T:TaskExtensions.MVVM.InvalidCommandParameterException"/> class.
/// </summary>
/// <param name="expectedType">Expected parameter type for AsyncCommand.Execute.</param>
/// <param name="innerException">Inner Exception</param>
public InvalidCommandParameterException(Type expectedType, Exception innerException) : base(CreateErrorMessage(expectedType), innerException)
{

}

/// <summary>
/// Initializes a new instance of the <see cref="T:MvvmHelpers.InvalidCommandParameterException"/> class.
/// </summary>
Expand All @@ -28,7 +38,19 @@ public InvalidCommandParameterException(Type expectedType, Type actualType) : ba

}

/// <summary>
/// Initializes a new instance of the <see cref="T:TaskExtensions.MVVM.InvalidCommandParameterException"/> class.
/// </summary>
/// <param name="expectedType">Expected parameter type for AsyncCommand.Execute.</param>
public InvalidCommandParameterException(Type expectedType) : base(CreateErrorMessage(expectedType))
{

}

static string CreateErrorMessage(Type expectedType, Type actualType) =>
$"Invalid type for parameter. Expected Type: {expectedType}, but received Type: {actualType}";

static string CreateErrorMessage(Type expectedType) =>
$"Invalid type for parameter. Expected Type {expectedType}";
}
}
4 changes: 4 additions & 0 deletions MvvmHelpers/MvvmHelpers.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard1.0'">
<PackageReference Include="NETStandard.Library" Version="2.0.0" />
</ItemGroup>


<Import Project="$(MSBuildSDKExtrasTargets)" Condition="Exists('$(MSBuildSDKExtrasTargets)')" />
</Project>
4 changes: 2 additions & 2 deletions MvvmHelpers/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ public static Task<T> WithTimeout<T>(this Task<T> task, TimeSpan timeout) =>
WithTimeout(task, (int)timeout.TotalMilliseconds);

#pragma warning disable RECS0165 // Asynchronous methods should return a Task instead of void
public static async void SafeFireAndForgetAsync(this Task task, Action<Exception> onException = null, bool continueOnCapturedContext = false)
public static async void SafeFireAndForget(this Task task, Action<Exception> onException = null, bool continueOnCapturedContext = false)
#pragma warning restore RECS0165 // Asynchronous methods should return a Task instead of void
{
try
{
await task.ConfigureAwait(continueOnCapturedContext);
}
catch (Exception ex)
catch (Exception ex) when (onException != null)
{
onException?.Invoke(ex);
}
Expand Down
12 changes: 6 additions & 6 deletions MvvmHelpers/WeakEventManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public void AddEventHandler<TEventArgs>(EventHandler<TEventArgs> handler, [Calle
if (IsNullOrEmpty(eventName))
throw new ArgumentNullException(nameof(eventName));

if (handler == null)
if (handler is null)
throw new ArgumentNullException(nameof(handler));

AddEventHandler(eventName, handler.Target, handler.GetMethodInfo());
Expand All @@ -28,7 +28,7 @@ public void AddEventHandler(EventHandler handler, [CallerMemberName]string event
if (IsNullOrEmpty(eventName))
throw new ArgumentNullException(nameof(eventName));

if (handler == null)
if (handler is null)
throw new ArgumentNullException(nameof(handler));

AddEventHandler(eventName, handler.Target, handler.GetMethodInfo());
Expand All @@ -54,7 +54,7 @@ public void HandleEvent(object sender, object args, string eventName)

var subscriber = subscription.Subscriber.Target;

if (subscriber == null)
if (subscriber is null)
// The subscriber was collected, so there's no need to keep this subscription around
toRemove.Add(subscription);
else
Expand All @@ -81,7 +81,7 @@ public void RemoveEventHandler<TEventArgs>(EventHandler<TEventArgs> handler, [Ca
if (IsNullOrEmpty(eventName))
throw new ArgumentNullException(nameof(eventName));

if (handler == null)
if (handler is null)
throw new ArgumentNullException(nameof(handler));

RemoveEventHandler(eventName, handler.Target, handler.GetMethodInfo());
Expand All @@ -92,7 +92,7 @@ public void RemoveEventHandler(EventHandler handler, [CallerMemberName]string ev
if (IsNullOrEmpty(eventName))
throw new ArgumentNullException(nameof(eventName));

if (handler == null)
if (handler is null)
throw new ArgumentNullException(nameof(handler));

RemoveEventHandler(eventName, handler.Target, handler.GetMethodInfo());
Expand All @@ -106,7 +106,7 @@ void AddEventHandler(string eventName, object handlerTarget, MethodInfo methodIn
eventHandlers.Add(eventName, targets);
}

if (handlerTarget == null)
if (handlerTarget is null)
{
// This event handler is a static method
targets.Add(new Subscription(null, methodInfo));
Expand Down

0 comments on commit 4183bff

Please sign in to comment.