-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtensions.cs
More file actions
39 lines (37 loc) · 2.29 KB
/
Copy pathExtensions.cs
File metadata and controls
39 lines (37 loc) · 2.29 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
namespace Kritjara.Collections;
/// <summary>Класс расширений для объектов сборки Notify</summary>
public static class Extensions
{
/// <summary>Возвращает коллекцию только для чтения с сохранением уведомлений.</summary>
/// <returns>Объект <see cref="IReadOnlyNotifyCollection{T}"/>. Может вернуть ссылку на <paramref name="source"/>, если та уже является <see cref="IReadOnlyNotifyCollection{T}"/>.</returns>
public static IReadOnlyNotifyCollection<T> AsReadOnlyNotifyCollection<T>(this INotifyCollection<T> source)
{
if (source is IReadOnlyNotifyCollection<T> r_o_coll)
{
return r_o_coll;
}
return new ReadOnlyNotifyCollection<T>(source);
}
/// <summary>Возвращает коллекцию только для чтения с сохранением уведомлений.</summary>
/// <remarks>
/// Создание будет успешным в случае, если <paramref name="source"/> реализует:
/// <list type="bullet">
/// <item><see cref="IList{T}"/> или <see cref="IReadOnlyList{T}"/> (в кастомных производных коллекциях рекомендуется реализовать)</item>
/// <item><see cref="ICollection{T}"/> или <see cref="IReadOnlyCollection{T}"/>/>
/// (может быть медленным при извлечении элементов по индексу, если не реализован ни один из выше указанных интерфейсов)</item>
/// </list>
/// </remarks>
/// <returns>Объект <see cref="IReadOnlyNotifyCollection{T}"/>. Может вернуть ссылку на <paramref name="source"/>, если та уже является <see cref="IReadOnlyNotifyCollection{T}"/>.</returns>
public static IReadOnlyNotifyCollection<T> TryCreateAsReadOnly<T>(this INotify<T> source)
{
if (source is IReadOnlyNotifyCollection<T> r_o_coll)
{
return r_o_coll;
}
if (source is INotifyCollection<T> coll)
{
return coll.AsReadOnlyNotifyCollection();
}
return new NotifyShell<T>(source);
}
}