|
1 | | -using System; |
| 1 | +using Microsoft.UI.Xaml.Data; |
| 2 | +using System; |
| 3 | +using System.Collections; |
2 | 4 | using System.Collections.Generic; |
3 | 5 | using System.Linq; |
4 | 6 |
|
@@ -38,4 +40,83 @@ public static void RemoveWhere<T>(this ICollection<T> collection, Predicate<T> p |
38 | 40 | collection.Remove(item); |
39 | 41 | } |
40 | 42 | } |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// Gets the index of the first occurrence of an item in the enumerable. |
| 46 | + /// </summary> |
| 47 | + /// <param name="enumerable">The enumerable to search.</param> |
| 48 | + /// <param name="item">The item to find.</param> |
| 49 | + /// <returns>The index of the item, or -1 if not found.</returns> |
| 50 | + public static int IndexOf(this IEnumerable enumerable, object? item) |
| 51 | + { |
| 52 | + if (enumerable is ICollection collection) |
| 53 | + return collection.IndexOf(item); |
| 54 | + |
| 55 | + if (enumerable is ICollectionView collectionView) |
| 56 | + return collectionView.IndexOf(item); |
| 57 | + |
| 58 | + |
| 59 | + var index = 0; |
| 60 | + foreach (var element in enumerable) |
| 61 | + { |
| 62 | + if (Equals(element, item)) |
| 63 | + return index; |
| 64 | + |
| 65 | + index++; |
| 66 | + } |
| 67 | + |
| 68 | + return -1; |
| 69 | + } |
| 70 | + |
| 71 | + /// <summary> |
| 72 | + /// Gets a value indicating whether the enumerable is read-only. |
| 73 | + /// </summary> |
| 74 | + /// <param name="enumerable">The enumerable to check.</param> |
| 75 | + /// <returns></returns> |
| 76 | + public static bool IsReadOnly(this IEnumerable enumerable) |
| 77 | + { |
| 78 | + if (enumerable is IList list) |
| 79 | + return list.IsReadOnly; |
| 80 | + |
| 81 | + if (enumerable is ICollectionView collectionView) |
| 82 | + return collectionView.IsReadOnly; |
| 83 | + |
| 84 | + return true; |
| 85 | + } |
| 86 | + |
| 87 | + public static void Add(this IEnumerable enumerable, object? item) |
| 88 | + { |
| 89 | + if (enumerable is ICollection collection) |
| 90 | + collection.Add(item); |
| 91 | + |
| 92 | + if (enumerable is ICollectionView collectionView) |
| 93 | + collectionView.Add(item); |
| 94 | + } |
| 95 | + |
| 96 | + public static void Insert(this IEnumerable enumerable, int index, object? item) |
| 97 | + { |
| 98 | + if (enumerable is ICollection collection) |
| 99 | + collection.Insert(index, item); |
| 100 | + |
| 101 | + if (enumerable is ICollectionView collectionView) |
| 102 | + collectionView.Insert(index, item); |
| 103 | + } |
| 104 | + |
| 105 | + public static void Remove(this IEnumerable enumerable, object? item) |
| 106 | + { |
| 107 | + if (enumerable is ICollection collection) |
| 108 | + collection.Remove(item); |
| 109 | + |
| 110 | + if (enumerable is ICollectionView collectionView) |
| 111 | + collectionView.Remove(item); |
| 112 | + } |
| 113 | + |
| 114 | + public static void Clear(this IEnumerable enumerable) |
| 115 | + { |
| 116 | + if (enumerable is ICollection collection) |
| 117 | + collection.Clear(); |
| 118 | + |
| 119 | + if (enumerable is ICollectionView collectionView) |
| 120 | + collectionView.Clear(); |
| 121 | + } |
41 | 122 | } |
0 commit comments