|
1 | | -namespace BionicCode.Utilities.Net.Reflection |
| 1 | +namespace BionicCode.Utilities.Net.Reflection; |
| 2 | + |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Linq; |
| 6 | +using System.Reflection; |
| 7 | + |
| 8 | +internal interface IConstructorListBuilder |
| 9 | +{ |
| 10 | + TypeData DeclaringType { get; } |
| 11 | + IConstructorListBuilder Add(ConstructorData constructorData); |
| 12 | + ConstructorList Build(); |
| 13 | +} |
| 14 | + |
| 15 | +internal class ConstructorListBuilder : SymbolDataListBuilder<ConstructorData>, IConstructorListBuilder |
2 | 16 | { |
3 | | - using System; |
4 | | - using System.Collections.Generic; |
5 | | - using System.Linq; |
6 | | - using System.Reflection; |
| 17 | + private ConstructorList? _builderResult; |
| 18 | + private readonly TypeData _declaringType; |
7 | 19 |
|
8 | | - internal interface IConstructorListBuilder |
| 20 | + private ConstructorListBuilder(TypeData declaringType) : base(declaringType.Handle) => _declaringType = declaringType; |
| 21 | + |
| 22 | + public static IConstructorListBuilder New(TypeData declaringType) |
9 | 23 | { |
10 | | - IConstructorListBuilder Add(ConstructorData constructorData); |
11 | | - ConstructorList Build(); |
| 24 | + ArgumentNullException.ThrowIfNull(declaringType); |
| 25 | + |
| 26 | + var builder = new ConstructorListBuilder(declaringType); |
| 27 | + return builder; |
12 | 28 | } |
13 | 29 |
|
14 | | - internal class ConstructorListBuilder : SymbolDataListBuilder<ConstructorData>, IConstructorListBuilder |
| 30 | + internal static ConstructorList Create(IEnumerable<ConstructorInfo>? items) |
15 | 31 | { |
16 | | - private ConstructorList? _builderResult; |
17 | | - |
18 | | - private ConstructorListBuilder(RuntimeTypeHandle declaringTypeHandle) : base(declaringTypeHandle) |
| 32 | + var constructorInfoList = items?.ToList(); |
| 33 | + if (constructorInfoList is null || constructorInfoList.IsEmpty()) |
19 | 34 | { |
| 35 | + return ConstructorList.Empty; |
20 | 36 | } |
21 | 37 |
|
22 | | - public static IConstructorListBuilder New(RuntimeTypeHandle declaringTypeHandle) |
| 38 | + var constructors = new List<ConstructorData>(constructorInfoList.Count); |
| 39 | + TypeData? declaringType = null; |
| 40 | + foreach (ConstructorInfo constructorInfo in constructorInfoList) |
23 | 41 | { |
24 | | - var builder = new ConstructorListBuilder(declaringTypeHandle); |
25 | | - return builder; |
26 | | - } |
| 42 | + ConstructorData constructorData = GetOrCreateCacheEntry(constructorInfo); |
27 | 43 |
|
28 | | - internal static ConstructorList Create(IEnumerable<ConstructorInfo>? items) |
29 | | - { |
30 | | - List<ConstructorInfo>? constructorInfoList = items?.ToList(); |
31 | | - if (constructorInfoList is null || constructorInfoList.IsEmpty()) |
32 | | - { |
33 | | - return ConstructorList.Empty; |
34 | | - } |
| 44 | + declaringType ??= constructorData.DeclaringTypeData; |
35 | 45 |
|
36 | | - List<ConstructorData> constructors = new List<ConstructorData>(constructorInfoList.Count); |
37 | | - RuntimeTypeHandle declaringTypeHandle = default; |
38 | | - foreach (ConstructorInfo constructorInfo in constructorInfoList) |
| 46 | + if (!ReferenceEquals(constructorData.DeclaringTypeData, declaringType)) |
39 | 47 | { |
40 | | - ConstructorData constructorData = SymbolReflectionInfoCache.GetOrCreateSymbolReflectionInfoCacheEntry(constructorInfo); |
41 | | - |
42 | | - if (declaringTypeHandle.Equals(default)) |
43 | | - { |
44 | | - declaringTypeHandle = constructorData.DeclaringTypeHandle; |
45 | | - } |
46 | | - |
47 | | - if (!constructorData.DeclaringTypeHandle.Equals(declaringTypeHandle)) |
48 | | - { |
49 | | - throw new ArgumentException($"The argument '{nameof(items)}' contains invalid items. Reason: All '{nameof(ConstructorInfo)}' items must belong to the same declaring type."); |
50 | | - } |
51 | | - |
52 | | - constructors.Add(constructorData); |
| 48 | + throw new ArgumentException($"The argument '{nameof(items)}' contains invalid items. Reason: All '{nameof(ConstructorInfo)}' items must belong to the same declaring type."); |
53 | 49 | } |
54 | 50 |
|
55 | | - return constructors.ToConstructorList(); |
| 51 | + constructors.Add(constructorData); |
56 | 52 | } |
57 | 53 |
|
58 | | - internal static ConstructorList Create(TypeData declaringTypeData) |
59 | | - { |
60 | | - ArgumentNullException.ThrowIfNull(declaringTypeData); |
61 | | - return CreateInternal(declaringTypeData.Type); |
62 | | - } |
| 54 | + return constructors.ToConstructorList(declaringType); |
| 55 | + } |
63 | 56 |
|
64 | | - internal static ConstructorList Create(Type declaringType) |
65 | | - { |
66 | | - ArgumentNullException.ThrowIfNull(declaringType); |
67 | | - return CreateInternal(declaringType); |
68 | | - } |
| 57 | + internal static ConstructorList Create(TypeData declaringTypeData) |
| 58 | + { |
| 59 | + ArgumentNullException.ThrowIfNull(declaringTypeData); |
69 | 60 |
|
70 | | - private static ConstructorList CreateInternal(Type declaringType) |
71 | | - { |
72 | | - ConstructorInfo[] constructorInfoList = declaringType.GetConstructors(ReflectionHelperExtensions.AllMembersFullHierarchyFlags); |
73 | | - if (constructorInfoList.IsEmpty()) |
74 | | - { |
75 | | - return ConstructorList.Empty; |
76 | | - } |
| 61 | + return CreateInternal(declaringTypeData); |
| 62 | + } |
77 | 63 |
|
78 | | - IEnumerable<ConstructorData> constructors = constructorInfoList.Select(SymbolReflectionInfoCache.GetOrCreateSymbolReflectionInfoCacheEntry); |
| 64 | + internal static ConstructorList Create(Type declaringType) |
| 65 | + { |
| 66 | + ArgumentNullException.ThrowIfNull(declaringType); |
79 | 67 |
|
80 | | - return constructors.ToConstructorList(); |
81 | | - } |
| 68 | + TypeData declaringTypeData = GetOrCreateCacheEntry(declaringType); |
| 69 | + return CreateInternal(declaringTypeData); |
| 70 | + } |
82 | 71 |
|
83 | | - IConstructorListBuilder IConstructorListBuilder.Add(ConstructorData constructorData) |
84 | | - { |
85 | | - Add(constructorData); |
86 | | - return this; |
87 | | - } |
| 72 | + private static ConstructorList CreateInternal(TypeData declaringType) => declaringType.EnumerateConstructors().ToConstructorList(declaringType); |
88 | 73 |
|
89 | | - ConstructorList IConstructorListBuilder.Build() |
90 | | - => _builderResult ??= new ConstructorList(Build(), isIntegrityValidationEnabled: false); |
91 | | - } |
| 74 | + TypeData IConstructorListBuilder.DeclaringType => _declaringType; |
92 | 75 |
|
93 | | - internal static class ConstructorListBuilderExtensions |
| 76 | + IConstructorListBuilder IConstructorListBuilder.Add(ConstructorData constructorData) |
94 | 77 | { |
| 78 | + Add(constructorData); |
| 79 | + return this; |
| 80 | + } |
95 | 81 |
|
96 | | - internal static ConstructorList ToConstructorList(this IEnumerable<ConstructorData> items) |
97 | | - => items is null || items.IsEmpty() ? ConstructorList.Empty : new ConstructorList(items); |
| 82 | + ConstructorList IConstructorListBuilder.Build() |
| 83 | + => _builderResult ??= new ConstructorList(Build(), ((IConstructorListBuilder)this).DeclaringType, isIntegrityValidationEnabled: false); |
| 84 | +} |
98 | 85 |
|
99 | | - /// <summary> |
100 | | - /// Returns an empty <see cref="ConstructorList"/> if the provided instance is <see langword="null"/>. |
101 | | - /// </summary> |
102 | | - /// <param name="items"></param> |
103 | | - /// <returns>A <see cref="ConstructorList"/> that is empty if the provided instance is <see langword="null"/>. Otherwise, returns the original instance.</returns> |
104 | | - public static ConstructorList OrEmpty(this ConstructorList items) |
105 | | - => items ?? ConstructorList.Empty; |
106 | | - } |
| 86 | +internal static class ConstructorListBuilderExtensions |
| 87 | +{ |
| 88 | + internal static ConstructorList ToConstructorList(this IEnumerable<ConstructorData> items, TypeData? declaringType) |
| 89 | + => items is null || items.IsEmpty() ? ConstructorList.Empty : new ConstructorList(items, declaringType); |
| 90 | + |
| 91 | + /// <summary> |
| 92 | + /// Returns an empty <see cref="ConstructorList"/> if the provided instance is <see langword="null"/>. |
| 93 | + /// </summary> |
| 94 | + /// <param name="items"></param> |
| 95 | + /// <returns>A <see cref="ConstructorList"/> that is empty if the provided instance is <see langword="null"/>. Otherwise, returns the original instance.</returns> |
| 96 | + public static ConstructorList OrEmpty(this ConstructorList items) |
| 97 | + => items ?? ConstructorList.Empty; |
107 | 98 | } |
0 commit comments