Repository navigation
Expand file tree
/
Copy pathCollectionHelper.cs
More file actions
executable file
·210 lines (198 loc) · 8.57 KB
/
Copy pathCollectionHelper.cs
File metadata and controls
executable file
·210 lines (198 loc) · 8.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
//
// This is the source code of Telegram for Windows Phone v. 3.x.x.
// It is licensed under GNU GPL v. 2 or later.
// You should have received a copy of the license in this archive (see LICENSE).
//
// Copyright Evgeny Nadymov, 2013-present.
//
using System.Collections;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
using System;
using TelegramClient.Extensions;
namespace TelegramClient.Helpers
{
/// <summary>
/// A set of extension methods for manipulating collections.
/// </summary>
/// <QualityBand>Experimental</QualityBand>
internal static class CollectionHelper
{
/// <summary>
/// Returns a value indicating whether a collection is read-only.
/// </summary>
/// <param name="collection">The collection to examine.</param>
/// <returns>A value indicating whether a collection is read-only.</returns>
public static bool IsReadOnly(this IEnumerable collection)
{
return
collection.GetType().IsArray
|| CollectionExtensions
.Iterate(collection.GetType(), type => type.BaseType)
.TakeWhile(type => type != null)
.Any(type => type.FullName.StartsWith("System.Collections.ObjectModel.ReadOnlyCollection`1", StringComparison.Ordinal));
}
/// <summary>
/// Returns a value Indicating whether an item can be inserted in a
/// collection.
/// </summary>
/// <param name="collection">The collection.</param>
/// <param name="item">The item to be inserted.</param>
/// <returns>A value Indicating whether an item can be inserted in a
/// collection.</returns>
public static bool CanInsert(this IEnumerable collection, object item)
{
ICollectionView collectionView = collection as ICollectionView;
if (collectionView != null)
{
return CanInsert(collectionView.SourceCollection, item);
}
if (IsReadOnly(collection))
{
return false;
}
Type genericListType = collection.GetType().GetInterfaces().Where(interfaceType => interfaceType.FullName.StartsWith("System.Collections.Generic.IList`1", StringComparison.Ordinal)).FirstOrDefault();
if (genericListType != null)
{
return genericListType.GetGenericArguments()[0] == item.GetType();
}
if (collection is IList)
{
return true;
}
return false;
}
/// <summary>
/// Inserts an item into the collection at an index.
/// </summary>
/// <param name="collection">The collection.</param>
/// <param name="index">The index at which to insert the item.</param>
/// <param name="item">The item to be inserted.</param>
public static void Insert(this IEnumerable collection, int index, object item)
{
ICollectionView collectionView = collection as ICollectionView;
if (collectionView != null)
{
Insert(collectionView.SourceCollection, index, item);
}
else
{
Type genericListType = collection.GetType().GetInterfaces().Where(interfaceType => interfaceType.FullName.StartsWith("System.Collections.Generic.IList`1", StringComparison.Ordinal)).FirstOrDefault();
if (genericListType != null)
{
genericListType.GetMethod("Insert").Invoke(collection, new object[] { index, item });
}
else
{
IList list = collection as IList;
list.Insert(index, item);
}
}
}
/// <summary>
/// Gets the number of items in the collection.
/// </summary>
/// <param name="collection">The collection.</param>
/// <returns>The number of items in the collection.</returns>
public static int Count(this IEnumerable collection)
{
ICollectionView collectionView = collection as ICollectionView;
if (collectionView != null)
{
return collectionView.SourceCollection.Count();
}
else
{
Type genericListType = collection.GetType().GetInterfaces().Where(interfaceType => interfaceType.FullName.StartsWith("System.Collections.Generic.ICollection`1", StringComparison.Ordinal)).FirstOrDefault();
if (genericListType != null)
{
return (int)genericListType.GetProperty("Count").GetValue(collection, new object[] { });
}
IList list = collection as IList;
if (list != null)
{
return list.Count;
}
return System.Linq.Enumerable.Count(collection.OfType<object>());
}
}
/// <summary>
/// Adds an item to the collection.
/// </summary>
/// <param name="collection">The collection.</param>
/// <param name="item">The item to be added.</param>
public static void Add(this IEnumerable collection, object item)
{
ICollectionView collectionView = collection as ICollectionView;
if (collectionView != null)
{
Add(collectionView.SourceCollection, item);
}
else
{
PropertyInfo countProperty = collection.GetType().GetProperty("Count");
int count = (int)countProperty.GetValue(collection, new object[] { });
Insert(collection, count, item);
}
}
/// <summary>
/// Removes an item from the collection.
/// </summary>
/// <param name="collection">The collection.</param>
/// <param name="item">The item to be removed.</param>
public static void Remove(this IEnumerable collection, object item)
{
ICollectionView collectionView = collection as ICollectionView;
if (collectionView != null)
{
collectionView.SourceCollection.Remove(item);
}
else
{
Type genericListType = collection.GetType().GetInterfaces().Where(interfaceType => interfaceType.FullName.StartsWith("System.Collections.Generic.IList`1", StringComparison.Ordinal)).FirstOrDefault();
if (genericListType != null)
{
int index = (int)genericListType.GetMethod("IndexOf").Invoke(collection, new object[] { item });
if (index != -1)
{
genericListType.GetMethod("RemoveAt").Invoke(collection, new object[] { index });
}
}
else
{
IList list = collection as IList;
list.Remove(item);
}
}
}
/// <summary>
/// Removes an item at a given index from the collection.
/// </summary>
/// <param name="collection">The collection.</param>
/// <param name="index">The index of the item to be removed.</param>
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "Code is linked into multiple projects.")]
public static void RemoveAt(this IEnumerable collection, int index)
{
ICollectionView collectionView = collection as ICollectionView;
if (collectionView != null)
{
collectionView.SourceCollection.RemoveAt(index);
}
else
{
Type genericListType = collection.GetType().GetInterfaces().Where(interfaceType => interfaceType.FullName.StartsWith("System.Collections.Generic.IList`1", StringComparison.Ordinal)).FirstOrDefault();
if (genericListType != null)
{
genericListType.GetMethod("RemoveAt").Invoke(collection, new object[] { index });
}
else
{
IList list = collection as IList;
list.RemoveAt(index);
}
}
}
}
}