|
1 | | -namespace BionicCode.Utilities.Net.Reflection |
2 | | -{ |
3 | | - using System; |
4 | | - using System.Reflection; |
| 1 | +namespace BionicCode.Utilities.Net.Reflection; |
| 2 | + |
| 3 | +using System; |
| 4 | +using System.Reflection; |
5 | 5 |
|
| 6 | +/// <summary> |
| 7 | +/// A descriptor that provides information about an anonymous constructor (where the caller does not have a direct <see cref="ConstructorInfo"/> representation of the constructor symbol). |
| 8 | +/// </summary> |
| 9 | +/// <remarks>The <see cref="AnonymousConstructorDescriptor"/> is used to provide information for anonymous constructor symbols, which is when the caller does not have the direct <see cref="ConstructorInfo"/> representation. |
| 10 | +/// <para/>When the caller has the direct <see cref="ConstructorInfo"/> representation and the constructor is well-known, use the <see cref="WellKnownConstructorDescriptor"/> instead. |
| 11 | +/// <para/>Important: well-known descriptors are preferred over anonymous descriptors when the <see cref="ConstructorInfo"/> is available to ensure maximum accuracy and performance. |
| 12 | +/// </remarks> |
| 13 | +internal readonly struct AnonymousConstructorDescriptor : IEquatable<AnonymousConstructorDescriptor> |
| 14 | +{ |
6 | 15 | /// <summary> |
7 | | - /// A descriptor that provides information about an anonymous constructor (where the caller does not have a direct <see cref="ConstructorInfo"/> representation of the constructor symbol). |
| 16 | + /// Initializes a new instance of the <see cref="AnonymousConstructorDescriptor"/> struct. |
8 | 17 | /// </summary> |
9 | | - /// <remarks>The <see cref="AnonymousConstructorDescriptor"/> is used to provide information for anonymous constructor symbols, which is when the caller does not have the direct <see cref="ConstructorInfo"/> representation. |
10 | | - /// <para/>When the caller has the direct <see cref="ConstructorInfo"/> representation and the constructor is well-known, use the <see cref="WellKnownConstructorDescriptor"/> instead. |
11 | | - /// <para/>Important: well-known descriptors are preferred over anonymous descriptors when the <see cref="ConstructorInfo"/> is available to ensure maximum accuracy and performance. |
| 18 | + /// <remarks>The <see cref="AnonymousConstructorDescriptor"/> is used to provide information about a constructor symbol of which the caller does not have a direct <see cref="ConstructorData"/> representation and instead only signature information is available. |
| 19 | + /// <para/>For best accuracy and performance always use the <see cref="WellKnownConstructorDescriptor"/>, which requires the caller to have direct access to the <see cref="MethodInfo"/> or <see cref="ConstructorInfo"/> representation of the method or constructor. |
12 | 20 | /// </remarks> |
13 | | - internal readonly struct AnonymousConstructorDescriptor : IEquatable<AnonymousConstructorDescriptor> |
| 21 | + /// <param name="declaringTypeHandle">The runtime type handle representing the declaring type of the anonymous constructor.</param> |
| 22 | + /// <param name="constructorParameters">The list of parameters for the anonymous method. Can be <see cref="MethodParameterInfoList.Empty"/> or <see langword="null"/> to indicate no parameters.</param> |
| 23 | + /// <returns>A new instance of <see cref="AnonymousConstructorDescriptor"/> representing the specified anonymous method.</returns> |
| 24 | + /// <exception cref="ArgumentNullException">Thrown when |
| 25 | + /// <list type="bullet"> |
| 26 | + /// <item><paramref name="declaringTypeHandle"/> is <see langword="default"/>.</item> |
| 27 | + /// <item>the provided <paramref name="declaringTypeHandle"/> does not resolve to a runtime type.</item> |
| 28 | + /// </list> |
| 29 | + /// </exception> |
| 30 | + public AnonymousConstructorDescriptor( |
| 31 | + RuntimeTypeHandle declaringTypeHandle, |
| 32 | + MethodParameterInfoList? constructorParameters) |
14 | 33 | { |
15 | | - /// <summary> |
16 | | - /// Initializes a new instance of the <see cref="AnonymousConstructorDescriptor"/> struct. |
17 | | - /// </summary> |
18 | | - /// <remarks>The <see cref="AnonymousConstructorDescriptor"/> is used to provide information about a constructor symbol of which the caller does not have a direct <see cref="ConstructorData"/> representation and instead only signature information is available. |
19 | | - /// <para/>For best accuracy and performance always use the <see cref="WellKnownConstructorDescriptor"/>, which requires the caller to have direct access to the <see cref="MethodInfo"/> or <see cref="ConstructorInfo"/> representation of the method or constructor. |
20 | | - /// </remarks> |
21 | | - /// <param name="declaringTypeHandle">The runtime type handle representing the declaring type of the anonymous constructor.</param> |
22 | | - /// <param name="constructorParameters">The list of parameters for the anonymous method. Can be <see cref="MethodParameterInfoList.Empty"/> or <see langword="null"/> to indicate no parameters.</param> |
23 | | - /// <returns>A new instance of <see cref="AnonymousConstructorDescriptor"/> representing the specified anonymous method.</returns> |
24 | | - /// <exception cref="ArgumentNullException">Thrown when |
25 | | - /// <list type="bullet"> |
26 | | - /// <item><paramref name="declaringTypeHandle"/> is <see langword="default"/>.</item> |
27 | | - /// <item>the provided <paramref name="declaringTypeHandle"/> does not resolve to a runtime type.</item> |
28 | | - /// </list> |
29 | | - /// </exception> |
30 | | - public AnonymousConstructorDescriptor( |
31 | | - RuntimeTypeHandle declaringTypeHandle, |
32 | | - MethodParameterInfoList? constructorParameters) |
33 | | - { |
34 | | - ArgumentNullExceptionAdvanced.ThrowIfDefault(declaringTypeHandle); |
35 | | - Type? declaringType = Type.GetTypeFromHandle(declaringTypeHandle); |
36 | | - ArgumentNullExceptionAdvanced.ThrowIfNull( |
37 | | - declaringType, |
38 | | - nameof(declaringTypeHandle), |
39 | | - $"Invalid argument '{nameof(declaringTypeHandle)}'. The provided declaring type handle does not resolve to a runtime type."); |
| 34 | + ArgumentNullExceptionAdvanced.ThrowIfDefault(declaringTypeHandle); |
| 35 | + var declaringType = Type.GetTypeFromHandle(declaringTypeHandle); |
| 36 | + ArgumentNullExceptionAdvanced.ThrowIfNull( |
| 37 | + declaringType, |
| 38 | + nameof(declaringTypeHandle), |
| 39 | + $"Invalid argument '{nameof(declaringTypeHandle)}'. The provided declaring type handle does not resolve to a runtime type."); |
40 | 40 |
|
41 | | - DeclaringTypeHandle = declaringTypeHandle; |
42 | | - ConstructorParameterList = constructorParameters.OrEmpty(); |
43 | | - IsAnonymous = true; |
44 | | - } |
| 41 | + DeclaringTypeHandle = declaringTypeHandle; |
| 42 | + ConstructorParameterList = constructorParameters.OrEmpty(); |
| 43 | + IsAnonymous = true; |
| 44 | + } |
45 | 45 |
|
46 | | - public RuntimeTypeHandle DeclaringTypeHandle { get; } |
47 | | - public MethodParameterInfoList ConstructorParameterList { get; } |
48 | | - public bool IsAnonymous { get; } |
| 46 | + public RuntimeTypeHandle DeclaringTypeHandle { get; } |
| 47 | + public MethodParameterInfoList ConstructorParameterList { get; } |
| 48 | + public bool IsAnonymous { get; } |
49 | 49 |
|
50 | | - public bool Equals(AnonymousConstructorDescriptor other) |
51 | | - => DeclaringTypeHandle.Equals(other.DeclaringTypeHandle) |
52 | | - && ConstructorParameterList.Equals(other.ConstructorParameterList) |
53 | | - && IsAnonymous == other.IsAnonymous; |
| 50 | + public bool Equals(AnonymousConstructorDescriptor other) |
| 51 | + => DeclaringTypeHandle.Equals(other.DeclaringTypeHandle) |
| 52 | + && ConstructorParameterList.Equals(other.ConstructorParameterList) |
| 53 | + && IsAnonymous == other.IsAnonymous; |
54 | 54 |
|
55 | | - public override int GetHashCode() |
56 | | - { |
57 | | - var hashCode = new HashCode(); |
58 | | - hashCode.Add(DeclaringTypeHandle); |
59 | | - hashCode.Add(ConstructorParameterList); |
60 | | - hashCode.Add(IsAnonymous); |
61 | | - hashCode.Add(DeclaringTypeHandle); |
| 55 | + public override int GetHashCode() |
| 56 | + { |
| 57 | + var hashCode = new HashCode(); |
| 58 | + hashCode.Add(DeclaringTypeHandle); |
| 59 | + hashCode.Add(ConstructorParameterList); |
| 60 | + hashCode.Add(IsAnonymous); |
| 61 | + hashCode.Add(DeclaringTypeHandle); |
62 | 62 |
|
63 | | - return hashCode.ToHashCode(); |
64 | | - } |
| 63 | + return hashCode.ToHashCode(); |
| 64 | + } |
65 | 65 |
|
66 | | - public static bool operator ==(AnonymousConstructorDescriptor left, AnonymousConstructorDescriptor right) |
67 | | - => left.Equals(right); |
68 | | - public static bool operator !=(AnonymousConstructorDescriptor left, AnonymousConstructorDescriptor right) |
69 | | - => !(left == right); |
| 66 | + public static bool operator ==(AnonymousConstructorDescriptor left, AnonymousConstructorDescriptor right) |
| 67 | + => left.Equals(right); |
| 68 | + public static bool operator !=(AnonymousConstructorDescriptor left, AnonymousConstructorDescriptor right) |
| 69 | + => !(left == right); |
70 | 70 |
|
71 | | - public override bool Equals(object obj) |
72 | | - => obj is AnonymousConstructorDescriptor other && Equals(other); |
73 | | - } |
| 71 | + public override bool Equals(object obj) |
| 72 | + => obj is AnonymousConstructorDescriptor other && Equals(other); |
74 | 73 | } |
0 commit comments