Created
June 9, 2016 20:18
-
-
Save LordJZ/6ac31c667034bc3049ba989b13aeeba2 to your computer and use it in GitHub Desktop.
ReadOnlyListProxy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace Redacted | |
{ | |
public static class ReadOnlyListProxy | |
{ | |
public static ReadOnlyListProxy<T, R> From<T, R>( | |
IReadOnlyList<T> list, Func<T, R> selector) | |
{ | |
return new ReadOnlyListProxy<T, R>(list, selector); | |
} | |
} | |
public class ReadOnlyListProxy<T, R> : IReadOnlyList<R> | |
{ | |
public IReadOnlyList<T> List | |
{ | |
get { return _list; } | |
} | |
readonly IReadOnlyList<T> _list; | |
readonly Func<T, R> _selector; | |
public ReadOnlyListProxy(IReadOnlyList<T> list, Func<T, R> selector) | |
{ | |
_list = list; | |
_selector = selector; | |
} | |
public struct Enumerator : IEnumerator<R> | |
{ | |
readonly IEnumerator<T> _enumerator; | |
readonly Func<T, R> _selector; | |
public Enumerator(IEnumerator<T> enumerator, Func<T, R> selector) | |
{ | |
_enumerator = enumerator; | |
_selector = selector; | |
} | |
public void Dispose() | |
{ | |
_enumerator.Dispose(); | |
} | |
public bool MoveNext() | |
{ | |
return _enumerator.MoveNext(); | |
} | |
public void Reset() | |
{ | |
_enumerator.Reset(); | |
} | |
public R Current | |
{ | |
get { return _selector(_enumerator.Current); } | |
} | |
object IEnumerator.Current | |
{ | |
get { return Current; } | |
} | |
} | |
public Enumerator GetEnumerator() | |
{ | |
return new Enumerator(_list.GetEnumerator(), _selector); | |
} | |
IEnumerator<R> IEnumerable<R>.GetEnumerator() | |
{ | |
return GetEnumerator(); | |
} | |
IEnumerator IEnumerable.GetEnumerator() | |
{ | |
return GetEnumerator(); | |
} | |
public int Count | |
{ | |
get { return _list.Count; } | |
} | |
public R this[int index] | |
{ | |
get { return _selector(_list[index]); } | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment