@@ -85,29 +85,29 @@ public RestMethodInfoInternal(
85
85
DetermineIfResponseMustBeDisposed ( ) ;
86
86
87
87
// Exclude cancellation token parameters from this list
88
- var parameterList = methodInfo
88
+ var parameterArray = methodInfo
89
89
. GetParameters ( )
90
- . Where ( p => p . ParameterType != typeof ( CancellationToken ) )
91
- . ToList ( ) ;
92
- ParameterInfoMap = parameterList
90
+ . Where ( static p => p . ParameterType != typeof ( CancellationToken ) )
91
+ . ToArray ( ) ;
92
+ ParameterInfoMap = parameterArray
93
93
. Select ( ( parameter , index ) => new { index , parameter } )
94
94
. ToDictionary ( x => x . index , x => x . parameter ) ;
95
- ParameterMap = BuildParameterMap ( RelativePath , parameterList ) ;
96
- BodyParameterInfo = FindBodyParameter ( parameterList , IsMultipart , hma . Method ) ;
97
- AuthorizeParameterInfo = FindAuthorizationParameter ( parameterList ) ;
95
+ ParameterMap = BuildParameterMap ( RelativePath , parameterArray ) ;
96
+ BodyParameterInfo = FindBodyParameter ( parameterArray , IsMultipart , hma . Method ) ;
97
+ AuthorizeParameterInfo = FindAuthorizationParameter ( parameterArray ) ;
98
98
99
99
Headers = ParseHeaders ( methodInfo ) ;
100
- HeaderParameterMap = BuildHeaderParameterMap ( parameterList ) ;
100
+ HeaderParameterMap = BuildHeaderParameterMap ( parameterArray ) ;
101
101
HeaderCollectionParameterMap = RestMethodInfoInternal . BuildHeaderCollectionParameterMap (
102
- parameterList
102
+ parameterArray
103
103
) ;
104
- PropertyParameterMap = BuildRequestPropertyMap ( parameterList ) ;
104
+ PropertyParameterMap = BuildRequestPropertyMap ( parameterArray ) ;
105
105
106
106
// get names for multipart attachments
107
107
AttachmentNameMap = [ ] ;
108
108
if ( IsMultipart )
109
109
{
110
- for ( var i = 0 ; i < parameterList . Count ; i ++ )
110
+ for ( var i = 0 ; i < parameterArray . Length ; i ++ )
111
111
{
112
112
if (
113
113
ParameterMap . ContainsKey ( i )
@@ -119,19 +119,19 @@ public RestMethodInfoInternal(
119
119
continue ;
120
120
}
121
121
122
- var attachmentName = GetAttachmentNameForParameter ( parameterList [ i ] ) ;
122
+ var attachmentName = GetAttachmentNameForParameter ( parameterArray [ i ] ) ;
123
123
if ( attachmentName == null )
124
124
continue ;
125
125
126
126
AttachmentNameMap [ i ] = Tuple . Create (
127
127
attachmentName ,
128
- GetUrlNameForParameter ( parameterList [ i ] )
128
+ GetUrlNameForParameter ( parameterArray [ i ] )
129
129
) ;
130
130
}
131
131
}
132
132
133
133
QueryParameterMap = [ ] ;
134
- for ( var i = 0 ; i < parameterList . Count ; i ++ )
134
+ for ( var i = 0 ; i < parameterArray . Length ; i ++ )
135
135
{
136
136
if (
137
137
ParameterMap . ContainsKey ( i )
@@ -145,21 +145,21 @@ public RestMethodInfoInternal(
145
145
continue ;
146
146
}
147
147
148
- QueryParameterMap . Add ( i , GetUrlNameForParameter ( parameterList [ i ] ) ) ;
148
+ QueryParameterMap . Add ( i , GetUrlNameForParameter ( parameterArray [ i ] ) ) ;
149
149
}
150
150
151
- var ctParams = methodInfo
151
+ var ctParamEnumerable = methodInfo
152
152
. GetParameters ( )
153
153
. Where ( p => p . ParameterType == typeof ( CancellationToken ) )
154
- . ToList ( ) ;
155
- if ( ctParams . Count > 1 )
154
+ . TryGetSingle ( out var ctParam ) ;
155
+ if ( ctParamEnumerable == EnumerablePeek . Many )
156
156
{
157
157
throw new ArgumentException (
158
158
$ "Argument list to method \" { methodInfo . Name } \" can only contain a single CancellationToken"
159
159
) ;
160
160
}
161
161
162
- CancellationToken = ctParams . FirstOrDefault ( ) ;
162
+ CancellationToken = ctParam ;
163
163
164
164
IsApiResponse =
165
165
ReturnResultType ! . GetTypeInfo ( ) . IsGenericType
@@ -170,31 +170,30 @@ public RestMethodInfoInternal(
170
170
|| ReturnResultType == typeof ( IApiResponse ) ;
171
171
}
172
172
173
- static HashSet < int > BuildHeaderCollectionParameterMap ( List < ParameterInfo > parameterList )
173
+ static HashSet < int > BuildHeaderCollectionParameterMap ( ParameterInfo [ ] parameterArray )
174
174
{
175
175
var headerCollectionMap = new HashSet < int > ( ) ;
176
176
177
- for ( var i = 0 ; i < parameterList . Count ; i ++ )
177
+ for ( var i = 0 ; i < parameterArray . Length ; i ++ )
178
178
{
179
- var param = parameterList [ i ] ;
179
+ var param = parameterArray [ i ] ;
180
180
var headerCollection = param
181
181
. GetCustomAttributes ( true )
182
182
. OfType < HeaderCollectionAttribute > ( )
183
183
. FirstOrDefault ( ) ;
184
184
185
- if ( headerCollection != null )
185
+ if ( headerCollection == null ) continue ;
186
+
187
+ //opted for IDictionary<string, string> semantics here as opposed to the looser IEnumerable<KeyValuePair<string, string>> because IDictionary will enforce uniqueness of keys
188
+ if ( param . ParameterType . IsAssignableFrom ( typeof ( IDictionary < string , string > ) ) )
186
189
{
187
- //opted for IDictionary<string, string> semantics here as opposed to the looser IEnumerable<KeyValuePair<string, string>> because IDictionary will enforce uniqueness of keys
188
- if ( param . ParameterType . IsAssignableFrom ( typeof ( IDictionary < string , string > ) ) )
189
- {
190
- headerCollectionMap . Add ( i ) ;
191
- }
192
- else
193
- {
194
- throw new ArgumentException (
195
- $ "HeaderCollection parameter of type { param . ParameterType . Name } is not assignable from IDictionary<string, string>"
196
- ) ;
197
- }
190
+ headerCollectionMap . Add ( i ) ;
191
+ }
192
+ else
193
+ {
194
+ throw new ArgumentException (
195
+ $ "HeaderCollection parameter of type { param . ParameterType . Name } is not assignable from IDictionary<string, string>"
196
+ ) ;
198
197
}
199
198
}
200
199
@@ -209,13 +208,13 @@ static HashSet<int> BuildHeaderCollectionParameterMap(List<ParameterInfo> parame
209
208
public RestMethodInfo ToRestMethodInfo ( ) =>
210
209
new ( Name , Type , MethodInfo , RelativePath , ReturnType ) ;
211
210
212
- static Dictionary < int , string > BuildRequestPropertyMap ( List < ParameterInfo > parameterList )
211
+ static Dictionary < int , string > BuildRequestPropertyMap ( ParameterInfo [ ] parameterArray )
213
212
{
214
213
var propertyMap = new Dictionary < int , string > ( ) ;
215
214
216
- for ( var i = 0 ; i < parameterList . Count ; i ++ )
215
+ for ( var i = 0 ; i < parameterArray . Length ; i ++ )
217
216
{
218
- var param = parameterList [ i ] ;
217
+ var param = parameterArray [ i ] ;
219
218
var propertyAttribute = param
220
219
. GetCustomAttributes ( true )
221
220
. OfType < PropertyAttribute > ( )
@@ -233,12 +232,11 @@ static Dictionary<int, string> BuildRequestPropertyMap(List<ParameterInfo> param
233
232
return propertyMap ;
234
233
}
235
234
236
- static PropertyInfo [ ] GetParameterProperties ( ParameterInfo parameter )
235
+ static IEnumerable < PropertyInfo > GetParameterProperties ( ParameterInfo parameter )
237
236
{
238
237
return parameter
239
238
. ParameterType . GetProperties ( BindingFlags . Public | BindingFlags . Instance )
240
- . Where ( p => p . CanRead && p . GetMethod ? . IsPublic == true )
241
- . ToArray ( ) ;
239
+ . Where ( static p => p . CanRead && p . GetMethod ? . IsPublic == true ) ;
242
240
}
243
241
244
242
static void VerifyUrlPathIsSane ( string relativePath )
@@ -254,7 +252,7 @@ static void VerifyUrlPathIsSane(string relativePath)
254
252
255
253
static Dictionary < int , RestMethodParameterInfo > BuildParameterMap (
256
254
string relativePath ,
257
- List < ParameterInfo > parameterInfo
255
+ ParameterInfo [ ] parameterInfo
258
256
)
259
257
{
260
258
var ret = new Dictionary < int , RestMethodParameterInfo > ( ) ;
@@ -311,11 +309,11 @@ List<ParameterInfo> parameterInfo
311
309
} ;
312
310
#if NET6_0_OR_GREATER
313
311
ret . TryAdd (
314
- parameterInfo . IndexOf ( restMethodParameterInfo . ParameterInfo ) ,
312
+ Array . IndexOf ( parameterInfo , restMethodParameterInfo . ParameterInfo ) ,
315
313
restMethodParameterInfo
316
314
) ;
317
315
#else
318
- var idx = parameterInfo . IndexOf ( restMethodParameterInfo . ParameterInfo ) ;
316
+ var idx = Array . IndexOf ( parameterInfo , restMethodParameterInfo . ParameterInfo ) ;
319
317
if ( ! ret . ContainsKey ( idx ) )
320
318
{
321
319
ret . Add ( idx , restMethodParameterInfo ) ;
@@ -329,7 +327,7 @@ List<ParameterInfo> parameterInfo
329
327
)
330
328
{
331
329
var property = value1 ;
332
- var parameterIndex = parameterInfo . IndexOf ( property . Item1 ) ;
330
+ var parameterIndex = Array . IndexOf ( parameterInfo , property . Item1 ) ;
333
331
//If we already have this parameter, add additional ParameterProperty
334
332
if ( ret . TryGetValue ( parameterIndex , out var value2 ) )
335
333
{
@@ -355,12 +353,12 @@ List<ParameterInfo> parameterInfo
355
353
) ;
356
354
#if NET6_0_OR_GREATER
357
355
ret . TryAdd (
358
- parameterInfo . IndexOf ( restMethodParameterInfo . ParameterInfo ) ,
356
+ Array . IndexOf ( parameterInfo , restMethodParameterInfo . ParameterInfo ) ,
359
357
restMethodParameterInfo
360
358
) ;
361
359
#else
362
360
// Do the contains check
363
- var idx = parameterInfo . IndexOf ( restMethodParameterInfo . ParameterInfo ) ;
361
+ var idx = Array . IndexOf ( parameterInfo , restMethodParameterInfo . ParameterInfo ) ;
364
362
if ( ! ret . ContainsKey ( idx ) )
365
363
{
366
364
ret . Add ( idx , restMethodParameterInfo ) ;
@@ -421,7 +419,7 @@ HttpMethod method
421
419
// 2) POST/PUT/PATCH: Reference type other than string
422
420
// 3) If there are two reference types other than string, without the body attribute, throw
423
421
424
- var bodyParams = parameterList
422
+ var bodyParamEnumerable = parameterList
425
423
. Select (
426
424
x =>
427
425
new
@@ -433,12 +431,12 @@ HttpMethod method
433
431
}
434
432
)
435
433
. Where ( x => x . BodyAttribute != null )
436
- . ToList ( ) ;
434
+ . TryGetSingle ( out var bodyParam ) ;
437
435
438
436
// multipart requests may not contain a body, implicit or explicit
439
437
if ( isMultipart )
440
438
{
441
- if ( bodyParams . Count > 0 )
439
+ if ( bodyParamEnumerable != EnumerablePeek . Empty )
442
440
{
443
441
throw new ArgumentException (
444
442
"Multipart requests may not contain a Body parameter"
@@ -447,19 +445,18 @@ HttpMethod method
447
445
return null ;
448
446
}
449
447
450
- if ( bodyParams . Count > 1 )
448
+ if ( bodyParamEnumerable == EnumerablePeek . Many )
451
449
{
452
450
throw new ArgumentException ( "Only one parameter can be a Body parameter" ) ;
453
451
}
454
452
455
453
// #1, body attribute wins
456
- if ( bodyParams . Count == 1 )
454
+ if ( bodyParamEnumerable == EnumerablePeek . Single )
457
455
{
458
- var ret = bodyParams [ 0 ] ;
459
456
return Tuple . Create (
460
- ret . BodyAttribute ! . SerializationMethod ,
461
- ret . BodyAttribute . Buffered ?? RefitSettings . Buffered ,
462
- parameterList . IndexOf ( ret . Parameter )
457
+ bodyParam ! . BodyAttribute ! . SerializationMethod ,
458
+ bodyParam . BodyAttribute . Buffered ?? RefitSettings . Buffered ,
459
+ parameterList . IndexOf ( bodyParam . Parameter )
463
460
) ;
464
461
}
465
462
@@ -475,7 +472,7 @@ HttpMethod method
475
472
476
473
// see if we're a post/put/patch
477
474
// explicitly skip [Query], [HeaderCollection], and [Property]-denoted params
478
- var refParams = parameterList
475
+ var refParamEnumerable = parameterList
479
476
. Where (
480
477
pi =>
481
478
! pi . ParameterType . GetTypeInfo ( ) . IsValueType
@@ -484,22 +481,22 @@ HttpMethod method
484
481
&& pi . GetCustomAttribute < HeaderCollectionAttribute > ( ) == null
485
482
&& pi . GetCustomAttribute < PropertyAttribute > ( ) == null
486
483
)
487
- . ToList ( ) ;
484
+ . TryGetSingle ( out var refParam ) ;
488
485
489
486
// Check for rule #3
490
- if ( refParams . Count > 1 )
487
+ if ( refParamEnumerable == EnumerablePeek . Many )
491
488
{
492
489
throw new ArgumentException (
493
490
"Multiple complex types found. Specify one parameter as the body using BodyAttribute"
494
491
) ;
495
492
}
496
493
497
- if ( refParams . Count == 1 )
494
+ if ( refParamEnumerable == EnumerablePeek . Single )
498
495
{
499
496
return Tuple . Create (
500
497
BodySerializationMethod . Serialized ,
501
498
RefitSettings . Buffered ,
502
- parameterList . IndexOf ( refParams [ 0 ] )
499
+ parameterList . IndexOf ( refParam ! )
503
500
) ;
504
501
}
505
502
@@ -508,7 +505,7 @@ HttpMethod method
508
505
509
506
static Tuple < string , int > ? FindAuthorizationParameter ( IList < ParameterInfo > parameterList )
510
507
{
511
- var authorizeParams = parameterList
508
+ var authorizeParamsEnumerable = parameterList
512
509
. Select (
513
510
x =>
514
511
new
@@ -520,19 +517,18 @@ HttpMethod method
520
517
}
521
518
)
522
519
. Where ( x => x . AuthorizeAttribute != null )
523
- . ToList ( ) ;
520
+ . TryGetSingle ( out var authorizeParam ) ;
524
521
525
- if ( authorizeParams . Count > 1 )
522
+ if ( authorizeParamsEnumerable == EnumerablePeek . Many )
526
523
{
527
524
throw new ArgumentException ( "Only one parameter can be an Authorize parameter" ) ;
528
525
}
529
526
530
- if ( authorizeParams . Count == 1 )
527
+ if ( authorizeParamsEnumerable == EnumerablePeek . Single )
531
528
{
532
- var ret = authorizeParams [ 0 ] ;
533
529
return Tuple . Create (
534
- ret . AuthorizeAttribute ! . Scheme ,
535
- parameterList . IndexOf ( ret . Parameter )
530
+ authorizeParam ! . AuthorizeAttribute ! . Scheme ,
531
+ parameterList . IndexOf ( authorizeParam . Parameter )
536
532
) ;
537
533
}
538
534
@@ -582,13 +578,13 @@ HttpMethod method
582
578
return ret ;
583
579
}
584
580
585
- static Dictionary < int , string > BuildHeaderParameterMap ( List < ParameterInfo > parameterList )
581
+ static Dictionary < int , string > BuildHeaderParameterMap ( ParameterInfo [ ] parameterArray )
586
582
{
587
583
var ret = new Dictionary < int , string > ( ) ;
588
584
589
- for ( var i = 0 ; i < parameterList . Count ; i ++ )
585
+ for ( var i = 0 ; i < parameterArray . Length ; i ++ )
590
586
{
591
- var header = parameterList [ i ]
587
+ var header = parameterArray [ i ]
592
588
. GetCustomAttributes ( true )
593
589
. OfType < HeaderAttribute > ( )
594
590
. Select ( ha => ha . Header )
0 commit comments