NRefactory 属性与类

本文关键字:属性 NRefactory | 更新日期: 2023-09-27 18:34:41

我试图用 NRefactory 找到这个类上面的所有类和属性,但不幸的是我(还(无法做到这一点。

解决这个问题的最佳方法是什么?我能够找到属性,但我如何确定它属于某个类?

NRefactory 属性与类

以下代码做到了:

        StreamReader reader = new StreamReader(@"..'..'demo.cs");
        var tex = reader.ReadToEnd();
        var syntaxTree = new CSharpParser().Parse(tex, tex);
        var testClass = syntaxTree.Descendants.OfType<TypeDeclaration>().Single(x => x.ClassType == ClassType.Class);
        var testClassAttributes = testClass.Attributes.SelectMany(x => x.Attributes).ToArray();
您可以使用

以下方法:

IEnumerable<ICSharpCode.NRefactory.CSharp.Attribute> GetAttributes(TypeDeclaration typeDeclaration)
{
    return typeDeclaration.Members
        .SelectMany(member => member
            .Attributes
            .SelectMany(attr => attr.Attributes));
}