1+ using System . Reflection ;
12using System . Xml . Linq ;
23
34using Equatable . SourceGenerator . Models ;
@@ -200,11 +201,15 @@ private static (ComparerTypes? comparerType, string? comparerName, string? compa
200201
201202 private static ( ComparerTypes ? comparerType , string ? comparerName , string ? comparerInstance ) GetStringComparer ( AttributeData ? attribute )
202203 {
203- var argument = attribute ? . ConstructorArguments . FirstOrDefault ( ) ;
204- if ( argument == null || ! argument . HasValue )
204+ if ( attribute == null || attribute . ConstructorArguments . Length != 1 )
205+ return ( ComparerTypes . Default , null , null ) ;
206+
207+ var argument = attribute . ConstructorArguments [ 0 ] ;
208+
209+ if ( argument . Value is not int value )
205210 return ( ComparerTypes . String , "CurrentCulture" , null ) ;
206211
207- var comparerName = argument ? . Value switch
212+ var comparerName = value switch
208213 {
209214 0 => "CurrentCulture" ,
210215 1 => "CurrentCultureIgnoreCase" ,
@@ -220,30 +225,19 @@ private static (ComparerTypes? comparerType, string? comparerName, string? compa
220225
221226 private static ( ComparerTypes ? comparerType , string ? comparerName , string ? comparerInstance ) GetEqualityComparer ( AttributeData ? attribute )
222227 {
223- if ( attribute == null )
228+ if ( attribute == null || attribute . ConstructorArguments . Length != 2 )
224229 return ( ComparerTypes . Default , null , null ) ;
225230
226- // attribute constructor
227- var comparerType = attribute . ConstructorArguments . FirstOrDefault ( ) ;
228- if ( comparerType . Value is INamedTypeSymbol typeSymbol )
229- {
230- return ( ComparerTypes . Custom , typeSymbol . ToDisplayString ( ) , null ) ;
231- }
232-
233- // generic attribute
234- var attributeClass = attribute . AttributeClass ;
235- if ( attributeClass is { IsGenericType : true }
236- && attributeClass . TypeArguments . Length == attributeClass . TypeParameters . Length
237- && attributeClass . TypeArguments . Length == 1 )
238- {
239- var typeArgument = attributeClass . TypeArguments [ 0 ] ;
240- var comparerName = typeArgument . ToDisplayString ( SymbolDisplayFormat . FullyQualifiedFormat ) ;
231+ var comparerArgument = attribute . ConstructorArguments [ 0 ] ;
232+ if ( comparerArgument . Value is not INamedTypeSymbol typeSymbol )
233+ return ( ComparerTypes . Default , null , null ) ; // invalid syntax found
241234
242- return ( ComparerTypes . Custom , comparerName , null ) ;
243- }
235+ var comparerName = typeSymbol . ToDisplayString ( SymbolDisplayFormat . FullyQualifiedFormat ) ;
244236
237+ var instanceArgument = attribute . ConstructorArguments [ 1 ] ;
238+ var comparerInstance = instanceArgument . Value as string ;
245239
246- return ( ComparerTypes . Default , null , null ) ;
240+ return ( ComparerTypes . Custom , comparerName , comparerInstance ) ;
247241 }
248242
249243
0 commit comments