/
CollectionMeta.cs
241 lines (186 loc) · 8.43 KB
/
CollectionMeta.cs
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#nullable enable
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Globalization;
using System.Reflection;
using JetBrains.Annotations;
using Nemesis.TextParsers.Runtime;
namespace Nemesis.TextParsers.Utils
{
/// <summary>
/// Aids in providing metadata for GUI applications
/// </summary>
[PublicAPI]
public readonly struct CollectionMeta : IEquatable<CollectionMeta>
{
public CollectionKind Kind { get; }
public Type ElementType { get; }
public bool IsValid => Kind != CollectionKind.Unknown && ElementType != null;
internal CollectionMeta(CollectionKind kind, Type elementType)
{
if (!Enum.IsDefined(typeof(CollectionKind), kind))
throw new InvalidEnumArgumentException(nameof(kind), (int)kind, typeof(CollectionKind));
Kind = kind;
ElementType = elementType;
}
public void Deconstruct(out CollectionKind kind, out Type elementType) { kind = Kind; elementType = ElementType; }
public override string ToString() => $"{Kind}, {ElementType.GetFriendlyName() ?? "<NoType>"}";
#region Equals
public bool Equals(CollectionMeta other) => Kind == other.Kind && ElementType == other.ElementType;
public override bool Equals(object? obj) => obj is CollectionMeta other && Equals(other);
public override int GetHashCode() => unchecked(((int) Kind * 397) ^ ElementType.GetHashCode());
public static bool operator ==(CollectionMeta left, CollectionMeta right) => left.Equals(right);
public static bool operator !=(CollectionMeta left, CollectionMeta right) => !left.Equals(right);
#endregion
public object? CreateCollection(IEnumerable sourceElements)
{
if (!IsValid) return null;
var createCollMethod = typeof(CollectionMeta).GetMethod(nameof(CreateCollectionGeneric), BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static) ??
throw new MissingMethodException(nameof(CollectionMeta), nameof(CreateCollectionGeneric));
createCollMethod = createCollMethod.MakeGenericMethod(ElementType);
return createCollMethod.Invoke(this, new object[] { sourceElements });
}
public IReadOnlyCollection<TDestElem?>? CreateCollectionGeneric<TDestElem>(IEnumerable sourceElements)
{
if (!IsValid) return null;
int? count = (sourceElements as ICollection)?.Count;
var input = new List<TDestElem?>(count ?? 8);
foreach (var element in sourceElements)
input.Add(ConvertElement<TDestElem>(element));
switch (Kind)
{
case CollectionKind.Array:
return input.ToArray();
case CollectionKind.List:
return input;
case CollectionKind.ReadOnlyCollection:
return input.AsReadOnly();
case CollectionKind.HashSet:
return new HashSet<TDestElem?>(input);
case CollectionKind.SortedSet:
return new SortedSet<TDestElem?>(input);
case CollectionKind.LinkedList:
return new LinkedList<TDestElem?>(input);
case CollectionKind.Stack:
return new Stack<TDestElem?>(input);
case CollectionKind.Queue:
return new Queue<TDestElem?>(input);
case CollectionKind.ObservableCollection:
return new ObservableCollection<TDestElem?>(input);
//case CollectionKind.Unknown:
default:
throw new NotSupportedException($@"{nameof(Kind)} = '{Kind}' is not supported");
}
}
private static TDest? ConvertElement<TDest>(object? element)
{
try
{
return element switch
{
TDest dest => dest,
null => default,
IFormattable formattable when typeof(TDest) == typeof(string) =>
(TDest)(object)formattable.ToString(null, CultureInfo.InvariantCulture),
_ => (TDest)TypeMeta.GetDefault(typeof(TDest))
};
}
catch (FormatException) { return default; }
}
}
internal static class CollectionMetaHelper
{
public static CollectionMeta GetCollectionMeta(Type collectionType)
{
if (collectionType != null)
{
if (collectionType.IsArray && collectionType.GetElementType() is { } arrayElementType)
return new(CollectionKind.Array, arrayElementType);
else if (IsTypeSupported(collectionType) &&
GetCollectionKind(collectionType) is { } kind &&
kind != CollectionKind.Unknown
)
{
Type elementType = GetElementType(collectionType);
return new(kind, elementType);
}
}
return default;
}
private static CollectionKind GetCollectionKind(Type collectionType)
{
if (collectionType.IsArray)
return CollectionKind.Array;
if (collectionType.IsGenericType && collectionType.GetGenericTypeDefinition() is { } definition)
{
if (definition == typeof(IEnumerable<>) ||
definition == typeof(ICollection<>) ||
definition == typeof(IList<>) ||
definition == typeof(List<>))
return CollectionKind.List;
else if (definition == typeof(IReadOnlyCollection<>) ||
definition == typeof(IReadOnlyList<>) ||
definition == typeof(ReadOnlyCollection<>))
return CollectionKind.ReadOnlyCollection;
else if (definition == typeof(ISet<>) ||
definition == typeof(HashSet<>))
return CollectionKind.HashSet;
else if (definition == typeof(SortedSet<>))
return CollectionKind.SortedSet;
else if (definition == typeof(LinkedList<>))
return CollectionKind.LinkedList;
else if (definition == typeof(Stack<>))
return CollectionKind.Stack;
else if (definition == typeof(Queue<>))
return CollectionKind.Queue;
else if (definition == typeof(ObservableCollection<>))
return CollectionKind.ObservableCollection;
}
return CollectionKind.Unknown;
}
private static Type GetElementType(Type collectionType)
{
var genericInterfaceType =
collectionType.IsGenericType && collectionType.GetGenericTypeDefinition() == typeof(IEnumerable<>)
? collectionType
: TypeMeta.GetGenericRealization(collectionType, typeof(IEnumerable<>))
?? throw new InvalidOperationException("Type has to be or implement IEnumerable<>");
return genericInterfaceType.GenericTypeArguments[0];
}
private static readonly HashSet<Type> _supportedCollectionTypes = new()
{
typeof(IEnumerable<>),
typeof(ICollection<>),
typeof(IList<>),
typeof(List<>),
typeof(IReadOnlyCollection<>),
typeof(IReadOnlyList<>),
typeof(ReadOnlyCollection<>),
typeof(ISet<>),
typeof(SortedSet<>),
typeof(HashSet<>),
typeof(LinkedList<>),
typeof(Stack<>),
typeof(Queue<>),
typeof(ObservableCollection<>),
};
public static bool IsTypeSupported(Type collectionType) =>
collectionType is {IsGenericType: true} && collectionType.GetGenericTypeDefinition() is { } definition && _supportedCollectionTypes.Contains(definition);
}
public enum CollectionKind : byte
{
Unknown,
Array,
List,
ReadOnlyCollection,
HashSet,
SortedSet,
LinkedList,
Stack,
Queue,
ObservableCollection
}
}