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

标签 c# roslyn

使用 Roslyn,我如何找到属性构造函数的值?因此给定以下具有属性的类:

[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

关于c# - 如何使用 Roslyn 查找属性构造函数参数的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41018052/

相关文章:

c# - 使用 Roslyn 修改方法体

c# - Roslyn 分析器代码修复 - 禁用预览选项

c# - 哪些设计模式对此有用?

c# - 记录接口(interface)及其实现

c# - 请告诉我一个为委托(delegate)(或)函数指针显示 `need` 的情况

c# - 使用类从 asp.net 中的数据库填充下拉列表的方法是什么?

c# - 在解决方案中使用 Roslyn 检索所有类型

c# - 我可以相信一旦获得 HttpContext.Current.Cache 将永远有效吗?

c# - 使用 Rosyln 将 switch block 重写为 if/else

reference - Roslyn CSharpScript 和 DateTime 引用错误