Roslyn: How to check if a Class or a Method is a Partial Class or Partial Method

Created on 10 May 2017  路  1Comment  路  Source: dotnet/roslyn

Version Used:
2.0.1

Steps to Reproduce:

  1. For any Partial Class/Method, the corresponding ClassDeclarationSyntax node or MethodDeclarationSyntax node data does not give any information if the given class is Partial class/method.
  2. We dont get this information even from any property of INamedTypeSymbol for Class or IMethodSymbol for Method.
  3. We can get this information only using node.Modifiers.Any(m => m.IsKind(SyntaxKind.PartialKeyword)).

Expected Behavior:
There has to be some property in ClassDeclarationSyntax node or MethodDeclarationSyntax node or in INamedTypeSymbol for Class or IMethodSymbol for Method which gives us the information that the node is a partial type.

Actual Behavior:
There isnt any property currently in ClassDeclarationSyntax node or MethodDeclarationSyntax node or in INamedTypeSymbol for Class or IMethodSymbol for Method which gives us the information that the node is a partial type.

Area-Compilers Concept-API Question

Most helpful comment

Do you care if hte symbol was declared partial? Or actually has multiple partial types. For example, you might have:

```c#
partial class C { }

partial class D { }
partial class D { }
```

Do you want C and D to report that they're partial? Or just D?

If the former, then you can do this just by checking the modifiers of these class declarations.
If you only care about the latter, then just get the INamedTypeSymbol for the type, and check. ISymbol.Locations.Length or INamedTypeSymbol.DeclaringSyntaxReferences.Length to see if hey are > 1.

>All comments

Do you care if hte symbol was declared partial? Or actually has multiple partial types. For example, you might have:

```c#
partial class C { }

partial class D { }
partial class D { }
```

Do you want C and D to report that they're partial? Or just D?

If the former, then you can do this just by checking the modifiers of these class declarations.
If you only care about the latter, then just get the INamedTypeSymbol for the type, and check. ISymbol.Locations.Length or INamedTypeSymbol.DeclaringSyntaxReferences.Length to see if hey are > 1.

Was this page helpful?
0 / 5 - 0 ratings