diff --git a/src/Microsoft.AspNetCore.Blazor/Components/ParameterCollection.cs b/src/Microsoft.AspNetCore.Blazor/Components/ParameterCollection.cs index 09fff59113..162518b524 100644 --- a/src/Microsoft.AspNetCore.Blazor/Components/ParameterCollection.cs +++ b/src/Microsoft.AspNetCore.Blazor/Components/ParameterCollection.cs @@ -64,6 +64,27 @@ namespace Microsoft.AspNetCore.Blazor.Components return false; } + /// + /// Gets the value of the parameter with the specified name, or a default value + /// if no such parameter exists in the collection. + /// + /// The type of the value. + /// The name of the parameter. + /// The parameter value if found; otherwise the default value for the specified type. + public T GetValueOrDefault(string parameterName) + => GetValueOrDefault(parameterName, default); + + /// + /// Gets the value of the parameter with the specified name, or a specified default value + /// if no such parameter exists in the collection. + /// + /// The type of the value. + /// The name of the parameter. + /// The default value to return if no such parameter exists in the collection. + /// The parameter value if found; otherwise . + public T GetValueOrDefault(string parameterName, T defaultValue) + => TryGetValue(parameterName, out T result) ? result : defaultValue; + /// /// Returns a dictionary populated with the contents of the . /// diff --git a/test/Microsoft.AspNetCore.Blazor.Test/ParameterCollectionTest.cs b/test/Microsoft.AspNetCore.Blazor.Test/ParameterCollectionTest.cs index 415e8aab29..769c7dfa4e 100644 --- a/test/Microsoft.AspNetCore.Blazor.Test/ParameterCollectionTest.cs +++ b/test/Microsoft.AspNetCore.Blazor.Test/ParameterCollectionTest.cs @@ -121,6 +121,60 @@ namespace Microsoft.AspNetCore.Blazor.Test Assert.Equal("hello", value); } + [Fact] + public void CanGetValueOrDefault_WithExistingValue() + { + // Arrange + var myEntryValue = new object(); + var parameterCollection = new ParameterCollection(new[] + { + RenderTreeFrame.Element(0, "some element").WithElementSubtreeLength(2), + RenderTreeFrame.Attribute(1, "my entry", myEntryValue), + RenderTreeFrame.Attribute(1, "my other entry", new object()) + }, 0); + + // Act + var result = parameterCollection.GetValueOrDefault("my entry"); + + // Assert + Assert.Same(myEntryValue, result); + } + + [Fact] + public void CanGetValueOrDefault_WithNonExistingValue() + { + // Arrange + var parameterCollection = new ParameterCollection(new[] + { + RenderTreeFrame.Element(0, "some element").WithElementSubtreeLength(2), + RenderTreeFrame.Attribute(1, "some other entry", new object()) + }, 0); + + // Act + var result = parameterCollection.GetValueOrDefault("nonexisting entry"); + + // Assert + Assert.Equal(default, result); + } + + [Fact] + public void CanGetValueOrDefault_WithNonExistingValueAndExplicitDefault() + { + // Arrange + var explicitDefaultValue = new DateTime(2018, 3, 20); + var parameterCollection = new ParameterCollection(new[] + { + RenderTreeFrame.Element(0, "some element").WithElementSubtreeLength(2), + RenderTreeFrame.Attribute(1, "some other entry", new object()) + }, 0); + + // Act + var result = parameterCollection.GetValueOrDefault("nonexisting entry", explicitDefaultValue); + + // Assert + Assert.Equal(explicitDefaultValue, result); + } + [Fact] public void ThrowsIfTryGetExistingValueWithIncorrectType() {