2
2
// The .NET Foundation licenses this file to you under the MIT license.
3
3
4
4
using System . Collections ;
5
- using System . Collections . Concurrent ;
6
5
using System . Diagnostics ;
7
6
using System . Diagnostics . CodeAnalysis ;
8
7
using System . Linq ;
9
- using System . Reflection . Metadata ;
10
8
using System . Runtime . CompilerServices ;
11
9
#if ! COMPONENTS
10
+ using System . Collections . Concurrent ;
11
+ using System . Reflection . Metadata ;
12
12
using Microsoft . AspNetCore . Http . Abstractions ;
13
13
using Microsoft . Extensions . Internal ;
14
14
using Microsoft . AspNetCore . Routing ;
15
- #else
16
- using Microsoft . AspNetCore . Components . Routing ;
17
- using Microsoft . AspNetCore . Components . Reflection ;
18
15
#endif
19
16
20
- #if COMPONENTS
21
- [ assembly: MetadataUpdateHandler ( typeof ( RouteValueDictionary . MetadataUpdateHandler ) ) ]
22
- #else
17
+ #if ! COMPONENTS
23
18
[ assembly: MetadataUpdateHandler ( typeof ( RouteValueDictionary . MetadataUpdateHandler ) ) ]
24
19
#endif
25
20
@@ -44,10 +39,14 @@ internal class RouteValueDictionary : IDictionary<string, object?>, IReadOnlyDic
44
39
{
45
40
// 4 is a good default capacity here because that leaves enough space for area/controller/action/id
46
41
private const int DefaultCapacity = 4 ;
42
+ #if ! COMPONENTS
47
43
private static readonly ConcurrentDictionary < Type , PropertyHelper [ ] > _propertyCache = new ConcurrentDictionary < Type , PropertyHelper [ ] > ( ) ;
44
+ #endif
48
45
49
46
internal KeyValuePair < string , object ? > [ ] _arrayStorage ;
47
+ #if ! COMPONENTS
50
48
internal PropertyStorage ? _propertyStorage ;
49
+ #endif
51
50
private int _count ;
52
51
53
52
#if ! COMPONENTS
@@ -232,6 +231,7 @@ private void Initialize(IEnumerable<KeyValuePair<string, object?>> keyValueEnume
232
231
[ MemberNotNull ( nameof ( _arrayStorage ) ) ]
233
232
private void Initialize ( RouteValueDictionary dictionary )
234
233
{
234
+ #if ! COMPONENTS
235
235
if ( dictionary . _propertyStorage != null )
236
236
{
237
237
// PropertyStorage is immutable so we can just copy it.
@@ -240,6 +240,7 @@ private void Initialize(RouteValueDictionary dictionary)
240
240
_arrayStorage = Array . Empty < KeyValuePair < string , object ? > > ( ) ;
241
241
return ;
242
242
}
243
+ #endif
243
244
244
245
var count = dictionary . _count ;
245
246
if ( count > 0 )
@@ -389,14 +390,15 @@ public void Clear()
389
390
return ;
390
391
}
391
392
393
+ #if ! COMPONENTS
392
394
if ( _propertyStorage != null )
393
395
{
394
396
_arrayStorage = Array . Empty < KeyValuePair < string , object ? > > ( ) ;
395
397
_propertyStorage = null ;
396
398
_count = 0 ;
397
399
return ;
398
400
}
399
-
401
+ #endif
400
402
Array . Clear ( _arrayStorage , 0 , _count ) ;
401
403
_count = 0 ;
402
404
}
@@ -421,12 +423,16 @@ public bool ContainsKey(string key)
421
423
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
422
424
private bool ContainsKeyCore ( string key )
423
425
{
426
+ #if ! COMPONENTS
424
427
if ( _propertyStorage == null )
425
428
{
426
429
return ContainsKeyArray ( key ) ;
427
430
}
428
431
429
432
return ContainsKeyProperties ( key ) ;
433
+ #else
434
+ return ContainsKeyArray ( key ) ;
435
+ #endif
430
436
}
431
437
432
438
/// <inheritdoc />
@@ -599,6 +605,9 @@ public bool TryGetValue(string key, out object? value)
599
605
ThrowArgumentNullExceptionForKey ( ) ;
600
606
}
601
607
608
+ #if COMPONENTS
609
+ return TryFindItem ( key , out value ) ;
610
+ #else
602
611
if ( _propertyStorage == null )
603
612
{
604
613
return TryFindItem ( key , out value ) ;
@@ -627,6 +636,7 @@ private bool TryGetValueSlow(string key, out object? value)
627
636
628
637
value = default ;
629
638
return false ;
639
+ #endif
630
640
}
631
641
632
642
[ DoesNotReturn ]
@@ -638,17 +648,25 @@ private static void ThrowArgumentNullExceptionForKey()
638
648
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
639
649
private void EnsureCapacity ( int capacity )
640
650
{
651
+ #if ! COMPONENTS
641
652
if ( _propertyStorage != null || _arrayStorage . Length < capacity )
642
653
{
643
654
EnsureCapacitySlow ( capacity ) ;
644
655
}
656
+ #else
657
+ if ( _arrayStorage . Length < capacity )
658
+ {
659
+ EnsureCapacitySlow ( capacity ) ;
660
+ }
661
+ #endif
645
662
}
646
663
647
664
[ UnconditionalSuppressMessage ( "Trimming" , "IL2026" ,
648
665
Justification = "The constructor that would result in _propertyStorage being non-null is annotated with RequiresUnreferencedCodeAttribute. " +
649
666
"We do not need to additionally produce an error in this method since it is shared by trimmer friendly code paths." ) ]
650
667
private void EnsureCapacitySlow ( int capacity )
651
668
{
669
+ #if ! COMPONENTS
652
670
if ( _propertyStorage != null )
653
671
{
654
672
var storage = _propertyStorage ;
@@ -668,7 +686,7 @@ private void EnsureCapacitySlow(int capacity)
668
686
_propertyStorage = null ;
669
687
return ;
670
688
}
671
-
689
+ #endif
672
690
if ( _arrayStorage . Length < capacity )
673
691
{
674
692
capacity = _arrayStorage . Length == 0 ? DefaultCapacity : _arrayStorage . Length * 2 ;
@@ -745,6 +763,7 @@ private bool ContainsKeyArray(string key)
745
763
return false ;
746
764
}
747
765
766
+ #if ! COMPONENTS
748
767
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
749
768
private bool ContainsKeyProperties ( string key )
750
769
{
@@ -761,6 +780,7 @@ private bool ContainsKeyProperties(string key)
761
780
762
781
return false ;
763
782
}
783
+ #endif
764
784
765
785
/// <inheritdoc />
766
786
public struct Enumerator : IEnumerator < KeyValuePair < string , object ? > >
@@ -801,13 +821,22 @@ public bool MoveNext()
801
821
{
802
822
var dictionary = _dictionary ;
803
823
824
+ #if ! COMPONENTS
804
825
// The uncommon case is that the propertyStorage is in use
805
826
if ( dictionary . _propertyStorage == null && ( ( uint ) _index < ( uint ) dictionary . _count ) )
806
827
{
807
828
Current = dictionary . _arrayStorage [ _index ] ;
808
829
_index ++ ;
809
830
return true ;
810
831
}
832
+ #else
833
+ if ( ( ( uint ) _index < ( uint ) dictionary . _count ) )
834
+ {
835
+ Current = dictionary . _arrayStorage [ _index ] ;
836
+ _index ++ ;
837
+ return true ;
838
+ }
839
+ #endif
811
840
812
841
return MoveNextRare ( ) ;
813
842
}
@@ -818,6 +847,7 @@ public bool MoveNext()
818
847
private bool MoveNextRare ( )
819
848
{
820
849
var dictionary = _dictionary ;
850
+ #if ! COMPONENTS
821
851
if ( dictionary . _propertyStorage != null && ( ( uint ) _index < ( uint ) dictionary . _count ) )
822
852
{
823
853
var storage = dictionary . _propertyStorage ;
@@ -826,6 +856,7 @@ private bool MoveNextRare()
826
856
_index ++ ;
827
857
return true ;
828
858
}
859
+ #endif
829
860
830
861
_index = dictionary . _count ;
831
862
Current = default ;
@@ -840,6 +871,7 @@ public void Reset()
840
871
}
841
872
}
842
873
874
+ #if ! COMPONENTS
843
875
[ RequiresUnreferencedCode ( "This API is not trim safe - from PropertyHelper" ) ]
844
876
internal sealed class PropertyStorage
845
877
{
@@ -889,6 +921,7 @@ internal static void ClearCache(Type[]? _)
889
921
_propertyCache . Clear ( ) ;
890
922
}
891
923
}
924
+ #endif
892
925
893
926
private sealed class RouteValueDictionaryDebugView ( RouteValueDictionary dictionary )
894
927
{
0 commit comments