@@ -17,8 +17,8 @@ namespace UnitsNet;
17
17
public readonly record struct UnitKey
18
18
{
19
19
// apparently, on netstandard, the use of auto-properties is significantly slower
20
- private readonly Type _unitType ;
21
- private readonly int _unitValue ;
20
+ private readonly Type _unitEnumType ;
21
+ private readonly int _unitEnumValue ;
22
22
23
23
/// <summary>
24
24
/// Represents a unique key for a unit type and its corresponding value.
@@ -27,10 +27,10 @@ public readonly record struct UnitKey
27
27
/// This key is particularly useful when using an enum-based unit in a hash-based collection,
28
28
/// as it avoids the boxing that would normally occur when casting the enum to <see cref="Enum" />.
29
29
/// </remarks>
30
- public UnitKey ( Type UnitType , int UnitValue )
30
+ internal UnitKey ( Type UnitEnumType , int UnitEnumValue )
31
31
{
32
- _unitType = UnitType ;
33
- _unitValue = UnitValue ;
32
+ _unitEnumType = UnitEnumType ;
33
+ _unitEnumValue = UnitEnumValue ;
34
34
}
35
35
36
36
/// <summary>
@@ -40,9 +40,9 @@ public UnitKey(Type UnitType, int UnitValue)
40
40
/// This property holds the <see cref="Type" /> of the unit enumeration associated with this key.
41
41
/// It is particularly useful for identifying the unit type in scenarios where multiple unit types are used.
42
42
/// </remarks>
43
- public Type UnitType
43
+ public Type UnitEnumType
44
44
{
45
- get => _unitType ;
45
+ get => _unitEnumType ;
46
46
}
47
47
48
48
/// <summary>
@@ -52,9 +52,9 @@ public Type UnitType
52
52
/// This property represents the unique value of the unit within its type, typically corresponding to the underlying
53
53
/// integer value of an enumeration.
54
54
/// </remarks>
55
- public int UnitValue
55
+ public int UnitEnumValue
56
56
{
57
- get => _unitValue ;
57
+ get => _unitEnumValue ;
58
58
}
59
59
60
60
/// <summary>
@@ -81,6 +81,36 @@ public static UnitKey Create<TUnit>(int unitValue)
81
81
return new UnitKey ( typeof ( TUnit ) , unitValue ) ;
82
82
}
83
83
84
+ /// <summary>
85
+ /// Creates a new instance of the <see cref="UnitKey" /> struct for the specified unit type and value.
86
+ /// </summary>
87
+ /// <param name="unitType">The type of the unit, which must be an enumeration.</param>
88
+ /// <param name="unitValue">The integer value representing the unit.</param>
89
+ /// <returns>A new instance of the <see cref="UnitKey" /> struct.</returns>
90
+ /// <exception cref="ArgumentNullException">
91
+ /// Thrown if <paramref name="unitType" /> is <c>null</c>.
92
+ /// </exception>
93
+ /// <exception cref="ArgumentException">
94
+ /// Thrown if <paramref name="unitType" /> is not an enumeration type.
95
+ /// </exception>
96
+ /// <remarks>
97
+ /// This method is useful for creating a <see cref="UnitKey" /> when the unit type and value are known.
98
+ /// </remarks>
99
+ public static UnitKey Create ( Type unitType , int unitValue )
100
+ {
101
+ if ( unitType is null )
102
+ {
103
+ throw new ArgumentNullException ( nameof ( unitType ) ) ;
104
+ }
105
+
106
+ if ( ! unitType . IsEnum )
107
+ {
108
+ throw new ArgumentException ( $ "Unit type must be an enumeration, but was { unitType . FullName } .") ;
109
+ }
110
+
111
+ return new UnitKey ( unitType , unitValue ) ;
112
+ }
113
+
84
114
/// <summary>
85
115
/// Implicitly converts an enumeration value to a <see cref="UnitKey" />.
86
116
/// </summary>
@@ -111,7 +141,7 @@ public static implicit operator UnitKey(Enum unit)
111
141
/// </remarks>
112
142
public static explicit operator Enum ( UnitKey unitKey )
113
143
{
114
- return ( Enum ) Enum . ToObject ( unitKey . _unitType , unitKey . _unitValue ) ;
144
+ return ( Enum ) Enum . ToObject ( unitKey . _unitEnumType , unitKey . _unitEnumValue ) ;
115
145
}
116
146
117
147
/// <summary>
@@ -129,25 +159,25 @@ public static explicit operator Enum(UnitKey unitKey)
129
159
/// </remarks>
130
160
public TUnit ToUnit < TUnit > ( ) where TUnit : struct , Enum
131
161
{
132
- if ( typeof ( TUnit ) != _unitType )
162
+ if ( typeof ( TUnit ) != _unitEnumType )
133
163
{
134
- throw new InvalidOperationException ( $ "Cannot convert UnitKey of type { _unitType } to { typeof ( TUnit ) } .") ;
164
+ throw new InvalidOperationException ( $ "Cannot convert UnitKey of type { _unitEnumType } to { typeof ( TUnit ) } .") ;
135
165
}
136
166
137
- var unitValue = _unitValue ;
167
+ var unitValue = _unitEnumValue ;
138
168
return Unsafe . As < int , TUnit > ( ref unitValue ) ;
139
169
}
140
170
141
171
private string GetDebuggerDisplay ( )
142
172
{
143
173
try
144
174
{
145
- var unitName = Enum . GetName ( _unitType , _unitValue ) ;
146
- return string . IsNullOrEmpty ( unitName ) ? $ "{ nameof ( UnitType ) } : { _unitType } , { nameof ( UnitValue ) } = { _unitValue } " : $ "{ _unitType . Name } .{ unitName } ";
175
+ var unitName = Enum . GetName ( _unitEnumType , _unitEnumValue ) ;
176
+ return string . IsNullOrEmpty ( unitName ) ? $ "{ nameof ( UnitEnumType ) } : { _unitEnumType } , { nameof ( UnitEnumValue ) } = { _unitEnumValue } " : $ "{ _unitEnumType . Name } .{ unitName } ";
147
177
}
148
178
catch
149
179
{
150
- return $ "{ nameof ( UnitType ) } : { _unitType } , { nameof ( UnitValue ) } = { _unitValue } ";
180
+ return $ "{ nameof ( UnitEnumType ) } : { _unitEnumType } , { nameof ( UnitEnumValue ) } = { _unitEnumValue } ";
151
181
}
152
182
}
153
183
@@ -162,8 +192,8 @@ private string GetDebuggerDisplay()
162
192
/// </remarks>
163
193
public void Deconstruct ( out Type unitType , out int unitValue )
164
194
{
165
- unitType = _unitType ;
166
- unitValue = _unitValue ;
195
+ unitType = _unitEnumType ;
196
+ unitValue = _unitEnumValue ;
167
197
}
168
198
169
199
#region Equality members
@@ -172,21 +202,21 @@ public void Deconstruct(out Type unitType, out int unitValue)
172
202
public bool Equals ( UnitKey other )
173
203
{
174
204
// implementing the Equality members on net48 is 5x faster than the default
175
- return _unitType == other . _unitType && _unitValue == other . _unitValue ;
205
+ return _unitEnumType == other . _unitEnumType && _unitEnumValue == other . _unitEnumValue ;
176
206
}
177
207
178
208
/// <inheritdoc />
179
209
public override int GetHashCode ( )
180
210
{
181
211
// implementing the Equality members on net48 is 5x faster than the default
182
- if ( _unitType == null )
212
+ if ( _unitEnumType == null )
183
213
{
184
- return _unitValue ;
214
+ return _unitEnumValue ;
185
215
}
186
216
187
217
unchecked
188
218
{
189
- return ( _unitType . GetHashCode ( ) * 397 ) ^ _unitValue ;
219
+ return ( _unitEnumType . GetHashCode ( ) * 397 ) ^ _unitEnumValue ;
190
220
}
191
221
}
192
222
0 commit comments