List
Provides various extensions for IList<T> interface.
using System;
using System.Collections.Generic;
using System.Reflection;
namespace DotNext.Collections.Generic
{
public static class List
{
private static class Indexer<C, T> where C : class, IEnumerable<T>
{
internal static readonly Func<C, int, T> Getter;
internal static readonly Action<C, int, T> Setter;
static Indexer()
{
MemberInfo[] defaultMembers = typeof(C).GetDefaultMembers();
for (int i = 0; i < defaultMembers.Length; i++) {
PropertyInfo propertyInfo = defaultMembers[i] as PropertyInfo;
if ((object)propertyInfo != null) {
Getter = DelegateHelpers.CreateDelegate<Func<C, int, T>>(propertyInfo.GetMethod, (object)null);
MethodInfo setMethod = propertyInfo.SetMethod;
Setter = (((object)setMethod != null) ? DelegateHelpers.CreateDelegate<Action<C, int, T>>(setMethod, (object)null) : null);
return;
}
}
throw new MissingMemberException();
}
}
public static class Indexer<T>
{
public static Func<IReadOnlyList<T>, int, T> ReadOnly => Indexer<IReadOnlyList<T>, T>.Getter;
public static Func<IList<T>, int, T> Getter => Indexer<IList<T>, T>.Getter;
public static Action<IList<T>, int, T> Setter => Indexer<IList<T>, T>.Setter;
}
public static Func<int, T> IndexerGetter<T>(this IReadOnlyList<T> list)
{
return DelegateHelpers.CreateDelegate<Func<int, T>>(Indexer<T>.ReadOnly.Method, list);
}
public static Func<int, T> IndexerGetter<T>(this IList<T> list)
{
return DelegateHelpers.CreateDelegate<Func<int, T>>(Indexer<T>.Getter.Method, list);
}
public static Action<int, T> IndexerSetter<T>(this IList<T> list)
{
return DelegateHelpers.CreateDelegate<Action<int, T>>(Indexer<T>.Setter.Method, list);
}
public static O[] ToArray<I, O>(this IList<I> input, Converter<I, O> mapper)
{
O[] array = OneDimensionalArray.New<O>(((ICollection<I>)input).Count);
for (int i = 0; i < ((ICollection<I>)input).Count; i++) {
array[i] = mapper(input[i]);
}
return array;
}
public static O[] ToArray<I, O>(this IList<I> input, Func<int, I, O> mapper)
{
O[] array = OneDimensionalArray.New<O>(((ICollection<I>)input).Count);
for (int i = 0; i < ((ICollection<I>)input).Count; i++) {
array[i] = mapper(i, input[i]);
}
return array;
}
public static ReadOnlyListView<T> AsReadOnlyView<T>(this IList<T> list)
{
return new ReadOnlyListView<T>(list);
}
public static ReadOnlyListView<I, O> Convert<I, O>(this IReadOnlyList<I> list, Converter<I, O> converter)
{
return new ReadOnlyListView<I, O>(list, converter);
}
public static IReadOnlyList<T> Singleton<T>(T item)
{
SingletonList<T> singletonList = default(SingletonList<T>);
singletonList.Item1 = item;
return singletonList;
}
}
}