How do I get all references to a class in C# source generators

Viewed 81

How do I get all references to a record using C# source generators? Or vice versa, how do I get the definition of the class via a reference? (and which is faster?)

[GenerateAttribute]
public record Test
{   
    public static string Property { get; }
}

This is a reference to it:

var _ = Test.Property;

I tried an approach: From the GeneratorSyntaxContext I analyse if the syntax node is a record with the GenerateAttribute. I remember these class names and when I encounter a InvocationExpressionSyntax, I check if the name identifier is in the remembered names. This approach is flawed because it's possible that the invocation is found earlier than the definition.

I think I have to get the symantic model somehow from each reference? Or is it easier to find all references from the semantic model of the class definition? Thanks in advance.

0 Answers
Related