Modify TryGetAttributes to return a IReadOnlyList

This commit is contained in:
Pranav K 2015-12-28 13:12:39 -08:00
parent dbe4ce06f2
commit 77b74b0c96
2 changed files with 11 additions and 6 deletions

View File

@ -4,7 +4,6 @@
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
namespace Microsoft.AspNet.Razor.TagHelpers namespace Microsoft.AspNet.Razor.TagHelpers
{ {
@ -17,6 +16,13 @@ namespace Microsoft.AspNet.Razor.TagHelpers
public class ReadOnlyTagHelperAttributeList<TAttribute> : IReadOnlyList<TAttribute> public class ReadOnlyTagHelperAttributeList<TAttribute> : IReadOnlyList<TAttribute>
where TAttribute : IReadOnlyTagHelperAttribute where TAttribute : IReadOnlyTagHelperAttribute
{ {
private static readonly IReadOnlyList<TAttribute> EmptyList =
#if NET451
new TAttribute[0];
#else
Array.Empty<TAttribute>();
#endif
/// <summary> /// <summary>
/// Instantiates a new instance of <see cref="ReadOnlyTagHelperAttributeList{TAttribute}"/> with an empty /// Instantiates a new instance of <see cref="ReadOnlyTagHelperAttributeList{TAttribute}"/> with an empty
/// collection. /// collection.
@ -190,12 +196,11 @@ namespace Microsoft.AspNet.Razor.TagHelpers
/// <param name="name">The <see cref="IReadOnlyTagHelperAttribute.Name"/> of the /// <param name="name">The <see cref="IReadOnlyTagHelperAttribute.Name"/> of the
/// <typeparamref name="TAttribute"/>s to get.</param> /// <typeparamref name="TAttribute"/>s to get.</param>
/// <param name="attributes">When this method returns, the <typeparamref name="TAttribute"/>s with /// <param name="attributes">When this method returns, the <typeparamref name="TAttribute"/>s with
/// <see cref="IReadOnlyTagHelperAttribute.Name"/> matching <paramref name="name"/>, if at least one is /// <see cref="IReadOnlyTagHelperAttribute.Name"/> matching <paramref name="name"/>.</param>
/// found; otherwise, <c>null</c>.</param>
/// <returns><c>true</c> if at least one <typeparamref name="TAttribute"/> with the same /// <returns><c>true</c> if at least one <typeparamref name="TAttribute"/> with the same
/// <see cref="IReadOnlyTagHelperAttribute.Name"/> exists in the collection; otherwise, <c>false</c>.</returns> /// <see cref="IReadOnlyTagHelperAttribute.Name"/> exists in the collection; otherwise, <c>false</c>.</returns>
/// <remarks><paramref name="name"/> is compared case-insensitively.</remarks> /// <remarks><paramref name="name"/> is compared case-insensitively.</remarks>
public bool TryGetAttributes(string name, out IEnumerable<TAttribute> attributes) public bool TryGetAttributes(string name, out IReadOnlyList<TAttribute> attributes)
{ {
if (name == null) if (name == null)
{ {
@ -216,7 +221,7 @@ namespace Microsoft.AspNet.Razor.TagHelpers
matchedAttributes.Add(Attributes[i]); matchedAttributes.Add(Attributes[i]);
} }
} }
attributes = matchedAttributes ?? Enumerable.Empty<TAttribute>(); attributes = matchedAttributes ?? EmptyList;
return matchedAttributes != null; return matchedAttributes != null;
} }

View File

@ -587,7 +587,7 @@ namespace Microsoft.AspNet.Razor.TagHelpers
{ {
// Arrange // Arrange
var attributes = new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>(initialAttributes); var attributes = new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>(initialAttributes);
IEnumerable<IReadOnlyTagHelperAttribute> resolvedAttributes; IReadOnlyList<IReadOnlyTagHelperAttribute> resolvedAttributes;
// Act // Act
var result = attributes.TryGetAttributes(nameToLookup, out resolvedAttributes); var result = attributes.TryGetAttributes(nameToLookup, out resolvedAttributes);