1

我正在编写一个 ReSharper 7.1 Generator 插件,需要获取当前项目中声明的所有类型(类、接口和结构 - IDeclaredType-s)的列表GeneratorProviderBase<CSharpGeneratorContext>.Populate

使用常规反射,它会像 一样简单Assembly.GetTypes(),但在这里它被证明是一个相当大的挑战。有没有办法做到这一点?

我搜索了高低,文档和示例没有帮助,然后查看了每个*Extensions课程*Util,但找不到任何有用的东西......

4

2 回答 2

0

我设法做了我需要的事情,但我不确定这是否是正确/最好的方法:

[GeneratorElementProvider("MyGeneratorProvider", typeof(CSharpLanguage))]
public class MyGeneratorProvider : GeneratorProviderBase<CSharpGeneratorContext>
{
    public override double Priority
    {
        get { return 0; }
    }

    public override void Populate(CSharpGeneratorContext context)
    {
        var projectCsFiles = GetAllCSharpFilesInProject(context.PsiModule);
        var declarations = projectCsFiles.SelectMany(GetDeclarationsFromCSharpFile).ToList();
        context.ProvidedElements.AddRange(declarations.Select(d => new GeneratorDeclarationElement(d)));
    }

    private static IEnumerable<ICSharpFile> GetAllCSharpFilesInProject(IPsiModule projectModule)
    {
        PsiManager psiManager = projectModule.GetPsiServices().PsiManager;
        return projectModule.SourceFiles.SelectMany(f => psiManager.GetPsiFiles<CSharpLanguage>(f).OfType<ICSharpFile>());
    }

    private static IEnumerable<ITypeDeclaration> GetDeclarationsFromCSharpFile(ICSharpFile file)
    {
        return file.NamespaceDeclarationNodes.SelectMany(GetDeclarationsFromCSharpNamespace);
    }

    private static IEnumerable<ITypeDeclaration> GetDeclarationsFromCSharpNamespace(ICSharpNamespaceDeclaration namespaceDeclaration)
    {
        foreach (var namespaceChild in namespaceDeclaration.Body.Children())
        {
            var classDeclaration = namespaceChild as IClassDeclaration;
            if (classDeclaration != null)
            {
                yield return classDeclaration;
            }
            else
            {
                var childNamespace = namespaceChild as ICSharpNamespaceDeclaration;
                if (childNamespace != null)
                {
                    foreach (var declaration in GetDeclarationsFromCSharpNamespace(childNamespace))
                    {
                        yield return declaration;
                    }
                }
            }
        }
    }
}

有任何评论或更简单(甚至可能是内置)的方法吗?

于 2015-01-18T18:31:02.593 回答
0

这是我想出的使用缓存的方法。它似乎仍然不正确,但我希望它比以前的方法更好。这仍然是从 a 调用的GeneratorProvider.Populate

public static IEnumerable<ICSharpTypeDeclaration> GetAllPublicTypeDeclarations(this IPsiModule module)
{
    var declarationCache = module.GetPsiServices().CacheManager.GetDeclarationsCache(module, false, true);
    var declarations = new List<ICSharpTypeDeclaration>();

    foreach (var shortName in declarationCache.GetAllShortNames())
    {
        var declaredElements = declarationCache.GetElementsByShortName(shortName).OfType<ITypeElement>().Where(e => 
        {
            var elementType = e.GetElementType();
            return elementType == CLRDeclaredElementType.CLASS || elementType == CLRDeclaredElementType.INTERFACE || elementType == CLRDeclaredElementType.ENUM;
        });

        foreach (ITypeElement declaredElement in declaredElements)
        {
            var declaration = declaredElement.GetDeclarations().OfType<ICSharpTypeDeclaration>().FirstOrDefault(d => d.GetAccessRights() == AccessRights.PUBLIC);
            if (declaration != null)
            {
                declarations.Add(declaration);
            }
        }
    }

    return declarations;
}

注意:结果与第一个答案不同,因为我更改了一些限制。此外,在这两种情况下,我都不关心部分类。

于 2015-01-24T21:29:57.260 回答