Find all template parameters of a class for a C# source generator

54 Views Asked by At

I'm currently making a source generator in C# and I was wondering if there is a clear way to do this:

I want to find all template parameters used to invoke methods from a template static class, including nested calls from another template class.

For example, with this code:

static class TemplateClassToFind<T>
{
    void Method()
    {
    
    }
}

class AnotherTemplateClass<U>
{
    void AnotherMethod() => TemplateClassToFind<U>.Method();
}

static class Program
{
    public static void Main()
    {
        TemplateClassToFind<int>.Method();
        
        new AnotherTemplateClass<float>.AnotherMethod();    
    }
}

I would like to get that int and float were both used as template parameters for TemplateClassToFind.

I tried looking at https://github.com/amis92/csharp-source-generators for some utilities but could not find one.

Considering this is probably a common issue, I was wondering if someone had a good way to do it?

Thanks a lot!

0

There are 0 best solutions below