HAL Media Type / HalMediaType / ResourceList.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HalMediaType
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1710:IdentifiersShouldHaveCorrectSuffix", Justification = "Custom class created to represent List")]
    public class ResourceList<T> : HalResource, IEnumerable<T> where T : HalResource
    {
        private List<T> resources;

        public ResourceList()
        {
            this.resources = new List<T>();
        }

        public ResourceList(List<T> res)
        {
            this.resources = res == null ? new List<T>() : res;
        }

        public T this[int index]
        {
            get
            {
                return this.resources[index];
            }
        }

        public T this[string name]
        {
            get
            {
                return this.resources.SingleOrDefault(x => x.LinkName == name);
            }
        }

        /// <summary>
        /// This method was added solely for the purpose of supporting XMLSerializer. 
        /// Exception thrown - To be XML serializable, types which inherit from IEnumerable must have an implementation of Add(System.Object) 
        /// </summary>
        /// <param name="item">Object to be added</param>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "item", Justification = "This method was added solely for the purpose of supporting XMLSerializer. ")]
        public void Add(T item)
        {
            throw new InvalidOperationException("Cannot add to the list");
        }

        public IEnumerator<T> GetEnumerator()
        {
            return this.resources.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }
    }
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.