【问题标题】:How to find the value of an attribute constructor argument using Roslyn?如何使用 Roslyn 查找属性构造函数参数的值?
【发布时间】:2016-12-07 12:55:12
【问题描述】:

使用 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 类型的信息(例如属性、构造函数)?

【问题讨论】:

    标签: c# roslyn


    【解决方案1】:

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

    然后查看你想要的属性的ConstructorArguments

    如果参数是typeof 表达式,则其Value 将是INamedTypeSymbol

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多