1010using Microsoft . CodeAnalysis . Diagnostics ;
1111using Microsoft . CodeAnalysis . Operations ;
1212
13+ using WellKnownType = Microsoft . AspNetCore . App . Analyzers . Infrastructure . WellKnownTypeData . WellKnownType ;
14+
1315namespace Microsoft . AspNetCore . Analyzers . WebApplicationBuilder ;
1416
1517[ DiagnosticAnalyzer ( LanguageNames . CSharp ) ]
@@ -34,7 +36,7 @@ public override void Initialize(AnalysisContext context)
3436 var targetMethod = invocation . TargetMethod ;
3537
3638 // Check if this is WebHost.CreateDefaultBuilder
37- if ( IsWebHostCreateDefaultBuilderCall ( targetMethod ) )
39+ if ( IsWebHostCreateDefaultBuilderCall ( targetMethod , wellKnownTypes ) )
3840 {
3941 var diagnostic = Diagnostic . Create (
4042 DiagnosticDescriptors . UseCreateHostBuilderInsteadOfCreateWebHostBuilder ,
@@ -51,7 +53,7 @@ public override void Initialize(AnalysisContext context)
5153 var symbol = semantic . GetDeclaredSymbol ( methodDeclaration ) ;
5254
5355 // Check if this method returns IWebHostBuilder
54- if ( symbol != null && IsWebHostBuilderReturnType ( symbol ) )
56+ if ( symbol != null && IsWebHostBuilderReturnType ( symbol , wellKnownTypes ) )
5557 {
5658 // Check if the method body contains WebHost.CreateDefaultBuilder
5759 if ( ContainsWebHostCreateDefaultBuilder ( methodDeclaration ) )
@@ -67,25 +69,23 @@ public override void Initialize(AnalysisContext context)
6769 } ) ;
6870 }
6971
70- private static bool IsWebHostCreateDefaultBuilderCall ( IMethodSymbol method )
72+ private static bool IsWebHostCreateDefaultBuilderCall ( IMethodSymbol method , WellKnownTypes wellKnownTypes )
7173 {
7274 // Check if this is WebHost.CreateDefaultBuilder (not other WebHost methods)
73- if ( method . Name == "CreateDefaultBuilder" &&
74- method . ContainingType ? . Name == "WebHost" &&
75- method . ContainingType ? . ContainingNamespace ? . ToDisplayString ( ) == "Microsoft.AspNetCore" )
75+ if ( method . Name == "CreateDefaultBuilder" &&
76+ SymbolEqualityComparer . Default . Equals ( method . ContainingType , wellKnownTypes . Get ( WellKnownType . Microsoft_AspNetCore_WebHost ) ) )
7677 {
7778 return true ;
7879 }
7980
8081 return false ;
8182 }
8283
83- private static bool IsWebHostBuilderReturnType ( IMethodSymbol method )
84+ private static bool IsWebHostBuilderReturnType ( IMethodSymbol method , WellKnownTypes wellKnownTypes )
8485 {
8586 // Check if the return type is IWebHostBuilder
8687 var returnType = method . ReturnType ;
87- return returnType . Name == "IWebHostBuilder" &&
88- returnType . ContainingNamespace ? . ToDisplayString ( ) == "Microsoft.AspNetCore.Hosting" ;
88+ return SymbolEqualityComparer . Default . Equals ( returnType , wellKnownTypes . Get ( WellKnownType . Microsoft_AspNetCore_Hosting_IWebHostBuilder ) ) ;
8989 }
9090
9191 private static bool ContainsWebHostCreateDefaultBuilder ( MethodDeclarationSyntax methodDeclaration )
0 commit comments