@@ -40,14 +40,14 @@ public ExtensionInfo(MetadataModule module, ITypeDefinition extensionContainer)
4040
4141 var metadata = module . MetadataFile . Metadata ;
4242
43- foreach ( var extGroup in extensionContainer . NestedTypes )
43+ foreach ( var nestedType in extensionContainer . NestedTypes )
4444 {
45- if ( TryEncodingV1 ( extGroup ) )
45+ if ( TryEncodingV1 ( nestedType ) )
4646 {
4747 continue ;
4848 }
4949
50- TryEncodingV2 ( extGroup ) ;
50+ TryEncodingV2 ( nestedType ) ;
5151 }
5252
5353 bool TryEncodingV1 ( ITypeDefinition extGroup )
@@ -90,43 +90,52 @@ bool TryEncodingV1(ITypeDefinition extGroup)
9090 return true ;
9191 }
9292
93- bool TryEncodingV2 ( ITypeDefinition extGroup )
93+ bool TryEncodingV2 ( ITypeDefinition extensionGroupsContainer )
9494 {
95- if ( ! ( extGroup is { Kind : TypeKind . Class , IsSealed : true }
96- && extGroup . Name . StartsWith ( "<G>$" , StringComparison . Ordinal ) ) )
95+ // there exists one nested type per extension target type
96+ if ( ! ( extensionGroupsContainer is { Kind : TypeKind . Class , IsSealed : true }
97+ && extensionGroupsContainer . Name . StartsWith ( "<G>$" , StringComparison . Ordinal ) ) )
9798 {
9899 return false ;
99100 }
100101
101- var markerType = extGroup . NestedTypes . SingleOrDefault ( t => t . Name . StartsWith ( "<M>$" , StringComparison . Ordinal ) && t . IsStatic ) ;
102- var marker = markerType ? . Methods . SingleOrDefault ( m => m . Name == "<Extension>$" && m . IsStatic && m . Parameters . Count == 1 ) ;
102+ // if there are multiple extension-blocks with the same target type,
103+ // but different names for the extension parameter,
104+ // there is a separate markerType, so there are multiple marker types per
105+ // target type
106+ foreach ( var markerType in extensionGroupsContainer . NestedTypes )
107+ {
108+ if ( ! ( markerType . Name . StartsWith ( "<M>$" , StringComparison . Ordinal ) && markerType . IsStatic ) )
109+ continue ;
110+ var marker = markerType . Methods . SingleOrDefault ( m => m . Name == "<Extension>$" && m . IsStatic && m . Parameters . Count == 1 ) ;
111+ if ( marker == null )
112+ continue ;
103113
104- if ( markerType == null || marker == null )
105- return false ;
114+ TypeDefinition td = metadata . GetTypeDefinition ( ( TypeDefinitionHandle ) extensionGroupsContainer . MetadataToken ) ;
115+ List < IMethod > extensionMethods = [ ] ;
116+ ITypeParameter [ ] extensionGroupTypeParameters = new ITypeParameter [ extensionGroupsContainer . TypeParameterCount ] ;
106117
107- TypeDefinition td = metadata . GetTypeDefinition ( ( TypeDefinitionHandle ) extGroup . MetadataToken ) ;
108- List < IMethod > extensionMethods = [ ] ;
109- ITypeParameter [ ] extensionGroupTypeParameters = new ITypeParameter [ extGroup . TypeParameterCount ] ;
118+ // For easier access to accessors we use SRM
119+ foreach ( var h in td . GetMethods ( ) )
120+ {
121+ var method = module . GetDefinition ( h ) ;
110122
111- // For easier access to accessors we use SRM
112- foreach ( var h in td . GetMethods ( ) )
113- {
114- var method = module . GetDefinition ( h ) ;
123+ if ( method . SymbolKind is SymbolKind . Constructor )
124+ continue ;
115125
116- if ( method . SymbolKind is SymbolKind . Constructor )
117- continue ;
126+ var attribute = method . GetAttribute ( KnownAttribute . ExtensionMarker ) ;
127+ if ( attribute == null )
128+ continue ;
118129
119- var attribute = method . GetAttribute ( KnownAttribute . ExtensionMarker ) ;
120- if ( attribute == null )
121- continue ;
130+ if ( attribute . FixedArguments [ 0 ] . Value ? . ToString ( ) != markerType . Name )
131+ continue ;
122132
123- if ( attribute . FixedArguments [ 0 ] . Value ? . ToString ( ) != markerType . Name )
124- continue ;
133+ extensionMethods . Add ( method ) ;
134+ }
125135
126- extensionMethods . Add ( method ) ;
136+ CollectImplementationMethods ( extensionGroupsContainer , marker , extensionMethods , extensionGroupTypeParameters ) ;
127137 }
128138
129- CollectImplementationMethods ( extGroup , marker , extensionMethods , extensionGroupTypeParameters ) ;
130139 return true ;
131140 }
132141
0 commit comments