Fixes #1296 - ItemCollection should be ICollection

A little bit more kindness for consumers.

I didn't add any tests because these are all just trivial forwards.
This commit is contained in:
Ryan Nowak 2017-06-03 18:55:24 -07:00
parent d5c1c63d19
commit 6664efb6d9
5 changed files with 170 additions and 1 deletions

View File

@ -39,5 +39,68 @@ namespace Microsoft.AspNetCore.Razor.Language
_items[key] = value;
}
}
public override int Count => _items.Count;
public override bool IsReadOnly => false;
public override void Add(KeyValuePair<object, object> item)
{
if (item.Key == null)
{
throw new ArgumentException(Resources.KeyMustNotBeNull, nameof(item));
}
((ICollection<KeyValuePair<object, object>>)_items).Add(item);
}
public override void Clear()
{
_items.Clear();
}
public override bool Contains(KeyValuePair<object, object> item)
{
if (item.Key == null)
{
throw new ArgumentException(Resources.KeyMustNotBeNull, nameof(item));
}
return ((ICollection<KeyValuePair<object, object>>)_items).Contains(item);
}
public override void CopyTo(KeyValuePair<object, object>[] array, int arrayIndex)
{
if (array == null)
{
throw new ArgumentNullException(nameof(array));
}
if (arrayIndex < 0 || arrayIndex > array.Length)
{
throw new ArgumentOutOfRangeException(nameof(arrayIndex));
}
else if (array.Length - arrayIndex < Count)
{
throw new ArgumentOutOfRangeException(nameof(arrayIndex));
}
((ICollection<KeyValuePair<object, object>>)_items).CopyTo(array, arrayIndex);
}
public override IEnumerator<KeyValuePair<object, object>> GetEnumerator()
{
return _items.GetEnumerator();
}
public override bool Remove(KeyValuePair<object, object> item)
{
if (item.Key == null)
{
throw new ArgumentException(Resources.KeyMustNotBeNull, nameof(item));
}
return ((ICollection<KeyValuePair<object, object>>)_items).Remove(item);
}
}
}

View File

@ -1,10 +1,34 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections;
using System.Collections.Generic;
namespace Microsoft.AspNetCore.Razor.Language
{
public abstract class ItemCollection
public abstract class ItemCollection : ICollection<KeyValuePair<object, object>>
{
public abstract object this[object key] { get; set; }
public abstract int Count { get; }
public abstract bool IsReadOnly { get; }
public abstract void Add(KeyValuePair<object, object> item);
public abstract void Clear();
public abstract bool Contains(KeyValuePair<object, object> item);
public abstract void CopyTo(KeyValuePair<object, object>[] array, int arrayIndex);
public abstract IEnumerator<KeyValuePair<object, object>> GetEnumerator();
public abstract bool Remove(KeyValuePair<object, object> item);
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}

View File

@ -444,6 +444,20 @@ namespace Microsoft.AspNetCore.Razor.Language
internal static string FormatDirectiveMustAppearAtStartOfLine(object p0)
=> string.Format(CultureInfo.CurrentCulture, GetString("DirectiveMustAppearAtStartOfLine"), p0);
/// <summary>
/// The key must not be null.
/// </summary>
internal static string KeyMustNotBeNull
{
get => GetString("KeyMustNotBeNull");
}
/// <summary>
/// The key must not be null.
/// </summary>
internal static string FormatKeyMustNotBeNull()
=> GetString("KeyMustNotBeNull");
private static string GetString(string name, params string[] formatterNames)
{
var value = _resourceManager.GetString(name);

View File

@ -2,6 +2,8 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Linq;
namespace Microsoft.AspNetCore.Razor.Language
{
@ -14,5 +16,68 @@ namespace Microsoft.AspNetCore.Razor.Language
get => null;
set => throw new NotSupportedException();
}
public override int Count => 0;
public override bool IsReadOnly => true;
public override void Add(KeyValuePair<object, object> item)
{
if (item.Key == null)
{
throw new ArgumentException(Resources.KeyMustNotBeNull, nameof(item));
}
throw new NotSupportedException();
}
public override void Clear()
{
throw new NotSupportedException();
}
public override bool Contains(KeyValuePair<object, object> item)
{
if (item.Key == null)
{
throw new ArgumentException(Resources.KeyMustNotBeNull, nameof(item));
}
return false;
}
public override void CopyTo(KeyValuePair<object, object>[] array, int arrayIndex)
{
if (array == null)
{
throw new ArgumentNullException(nameof(array));
}
if (arrayIndex < 0 || arrayIndex > array.Length)
{
throw new ArgumentOutOfRangeException(nameof(arrayIndex));
}
else if (array.Length - arrayIndex < Count)
{
throw new ArgumentOutOfRangeException(nameof(arrayIndex));
}
// Do nothing.
}
public override IEnumerator<KeyValuePair<object, object>> GetEnumerator()
{
return Enumerable.Empty<KeyValuePair<object, object>>().GetEnumerator();
}
public override bool Remove(KeyValuePair<object, object> item)
{
if (item.Key == null)
{
throw new ArgumentException(Resources.KeyMustNotBeNull, nameof(item));
}
throw new NotSupportedException();
}
}
}

View File

@ -210,4 +210,7 @@
<data name="DirectiveMustAppearAtStartOfLine" xml:space="preserve">
<value>The '{0}` directive must appear at the start of the line.</value>
</data>
<data name="KeyMustNotBeNull" xml:space="preserve">
<value>The key must not be null.</value>
</data>
</root>