查找 C# 源生成器的类的所有模板参数

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

提问人:Slyp 提问时间:9/27/2023 更新时间:9/27/2023 访问量:32

问:

我目前正在用 C# 制作一个源代码生成器,我想知道是否有明确的方法可以做到这一点:

我想找到用于从模板静态类调用方法的所有模板参数,包括来自另一个模板类的嵌套调用。

例如,使用以下代码:

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();    
    }
}

我想得到 intfloat 都用作 TemplateClassToFind 的模板参数。

我尝试在 https://github.com/amis92/csharp-source-generators 中查找一些实用程序,但找不到。

考虑到这可能是一个常见问题,我想知道是否有人有好办法做到这一点?

多谢!

C# 源生成器 csharp-source-generator

评论

0赞 Community 9/27/2023
请修剪您的代码,以便更轻松地找到您的问题。请遵循以下准则,创建一个最小的可重现示例

答: 暂无答案