/// <summary> /// Sets the given <paramref name="property"/> if the <paramref name="action"/> does not throw a <see cref="COMException"/> for 0x80070490. /// </summary> /// <typeparam name="T">The type of the property to set.</typeparam> /// <param name="property">A reference to the property to set.</param> /// <param name="propertyName">The name of the property for diagnostic purposes.</param> /// <param name="action">A method that returns the value of the property to set.</param> /// <param name="error">Optional error handler that accepts the name of the property.</param> /// <exception cref="ArgumentException"><paramref name="propertyName"/> is an empty string.</exception> /// <exception cref="ArgumentNullException"><paramref name="propertyName"/> or <paramref name="action"/> is null.</exception> public static void TrySet <T>(ref T property, string propertyName, Func <T> action, Action <string> error = null) { Validate.NotNullOrEmpty(propertyName, nameof(propertyName)); Validate.NotNull(action, nameof(action)); try { property = action.Invoke(); } catch (COMException ex) when(ex.ErrorCode == NativeMethods.E_NOTFOUND) { error?.Invoke(propertyName); } }
/// <summary> /// Sets the given package reference collection <paramref name="property"/> if the <paramref name="action"/> does not throw a <see cref="COMException"/> for 0x80070490. /// </summary> /// <typeparam name="T">The type of the package reference to adapt.</typeparam> /// <typeparam name="R">The adapted type of the package reference.</typeparam> /// <param name="property">A reference to the property to set.</param> /// <param name="propertyName">The name of the property for diagnostic purposes.</param> /// <param name="action">A method that returns the value of the property to set.</param> /// <param name="creator">A method that creates the adapted reference type.</param> /// <param name="error">Optional error handler that accepts the name of the property.</param> /// <returns>A <see cref="ReadOnlyCollection{T}"/> containing the adapted package references. This collection may be empty.</returns> /// <exception cref="ArgumentException"><paramref name="propertyName"/> is an empty string.</exception> /// <exception cref="ArgumentNullException">One or more parameters is null.</exception> public static ReadOnlyCollection <R> TrySetCollection <T, R>( ref IList <R> property, string propertyName, Func <IEnumerable <T> > action, Func <T, R> creator, Action <string> error = null) where T : ISetupPackageReference where R : PackageReference { Validate.NotNullOrEmpty(propertyName, nameof(propertyName)); Validate.NotNull(action, nameof(action)); Validate.NotNull(creator, nameof(creator)); var packages = GetAdaptedPackages(action, creator); TrySet(ref property, propertyName, packages.ToList, error); if (property != null && property.Any()) { return(new ReadOnlyCollection <R>(property)); } return(EmptyReadOnlyCollection <R>()); }
public void NotNullOrEmpty_Empty_Throws() { Assert.Throws <ArgumentException>("test", () => Validate.NotNullOrEmpty(string.Empty, "test")); }
public void NotNullOrEmpty() { Validate.NotNullOrEmpty("test", "test"); }
public void NotNullOrEmpty_Null_Throws() { Assert.Throws <ArgumentNullException>("test", () => Validate.NotNullOrEmpty(null, "test")); }