Roslyn analyzer: Is class marked as partial
Asked Answered
F

3

7

Is there a way to find out whether class is partial inside Roslyn analyzer? There is a PartialImplementationPart in IMethodSymbol, but nothing similar for INamedTypeSymbol.

I'm writing a Source Generator, and I want to generate second part of the class only if it's possible (if first part is partial).

Fukuoka answered 24/8, 2021 at 11:2 Comment(0)
P
11

You could use the Modifier List to check if the class is partial.

var isPartial = classDeclaration.Modifiers
                                .Any(m => m.IsKind(SyntaxKind.PartialKeyword));
Papillose answered 24/8, 2021 at 17:5 Comment(2)
This does not answer the question: how to get this from an INamedTypeSymbol?Buster
I got errors about lambda expressions. I needed to use classDeclaration.Modifiers.Any(SyntaxKind.PartialKeyword);. You didn't specify above but in my case classDeclaration is a ClassDeclarationSyntax.Flog
F
1

If you have an INamedTypeSymbol, you need to check its declarations in syntax:

typeSymbol.DeclaringSyntaxReferences.Any(syntax => syntax.GetSyntax() is BaseTypeDeclarationSyntax declaration && declaration.Modifiers.Any(modifier => modifier.IsKind(SyntaxKind.PartialKeyword))))
Flashgun answered 6/3, 2024 at 13:29 Comment(0)
K
0

You can also look at ISymbol.Locations if you only have access to the ISymbol - more than one location is a pretty good indicator that it's a partial class.

Koralle answered 14/4, 2023 at 1:27 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.