如何使用 Roslyn 查找属性构造函数参数的值?

How to find the value of an attribute constructor argument using Roslyn?

使用 Roslyn,我如何找到属性构造函数的值?因此给定以下 class 和一个属性:

[Example(typeof(ClassFromAnotherDll))]
public class ExampleClass
{
    public int JustANumber { get; set; }
}

ExampleAttribute 看起来像这样(尽管源代码与上面的解决方案不同):

public class ExampleAttribute : Attribute
{
    private readonly Type _type;

    public ExampleAttribute(Type type)
    {
        _type = type;
    }
}

如何获取有关 ClassFromAnotherDll 类型的信息(例如属性、构造函数)?

对任何符号(来自语义模型)调用 GetAttributes() 以获取所有应用属性的列表。

然后看你要的属性ConstructorArguments

如果参数是一个 typeof 表达式,它的 Value 将是一个 INamedTypeSymbol.