add OfType<T> extension method to IList

This commit is contained in:
Michael Becker 2020-11-20 22:19:43 -05:00
parent 10290a037b
commit c507fe4f83
No known key found for this signature in database
GPG Key ID: 506F54899E2BFED7
2 changed files with 59 additions and 0 deletions

View File

@ -0,0 +1,58 @@
//
// ExtensionMethods.cs - implements generic-typed extension methods for System.Collections classes
//
// Author:
// Michael Becker <alcexhim@gmail.com>
//
// Copyright (c) 2020 Mike Becker's Software
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using System;
using System.Collections.Generic;
namespace MBS.Framework.Collections.Generic
{
public static class ExtensionMethods
{
private static Dictionary<System.Collections.IList, Dictionary<Type, System.Collections.IList>> cacheOfT = new Dictionary<System.Collections.IList, Dictionary<Type, System.Collections.IList>>();
/// <summary>
/// Returns an array of type <typeparamref name="T" /> which consists of all elements in the specified <see cref="System.Collections.IList" /> that are of type
/// <typeparamref name="T" />.
/// </summary>
/// <returns>An array of type <typeparamref name="T" /> which consists of all elements in the specified <see cref="System.Collections.IList" /> that are of type
/// <typeparamref name="T" />.</returns>
/// <param name="obj">A <see cref="System.Collections.IList" /> containing the elements to search.</param>
/// <typeparam name="T">A type parameter indicating the type of elements to retrieve.</typeparam>
public static T[] OfType<T>(this System.Collections.IList obj)
{
if (!cacheOfT.ContainsKey(obj))
{
cacheOfT[obj] = new Dictionary<Type, System.Collections.IList>();
}
if (!cacheOfT[obj].ContainsKey(typeof(T)))
{
List<T> list = new List<T>();
for (int i = 0; i < obj.Count; i++)
{
if (obj[i] is T)
{
list.Add((T)obj[i]);
}
}
cacheOfT[obj][typeof(T)] = list;
}
return ((List<T>)(cacheOfT[obj][typeof(T)])).ToArray();
}
}
}

View File

@ -86,6 +86,7 @@
<Compile Include="CommandItem.cs" /> <Compile Include="CommandItem.cs" />
<Compile Include="StockType.cs" /> <Compile Include="StockType.cs" />
<Compile Include="ContextChangedEvent.cs" /> <Compile Include="ContextChangedEvent.cs" />
<Compile Include="Collections\Generic\ExtensionMethods.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Folder Include="Logic\" /> <Folder Include="Logic\" />