public static async Task<List<InterfaceDeclarationSyntax>> GetImplementedInterfaces(string interfaceProjectName, string interfaceName) { var interfaceProject = Solution.Projects.First(x => x.Name == interfaceProjectName); var projectCompilation = await interfaceProject.GetCompilationAsync(); var interfaceVisitor = new InterfaceVirtualizationVisitor(); foreach (var syntaxTree in projectCompilation.SyntaxTrees) { interfaceVisitor.Visit(syntaxTree.GetRoot()); } var interfaces = interfaceVisitor.interfaces; var resultList = new List<InterfaceDeclarationSyntax>(); foreach (var inf in interfaces.Where(inf => inf.BaseList != null)) { foreach (var type in inf.BaseList.Types) { if (type.ToString() == interfaceName) { resultList.Add(inf); } } } return resultList; }
public static async Task<IEnumerable<InterfaceDeclarationSyntax>> GetImplementedInterfaces(string interfacesProjectName, ClassDeclarationSyntax repositoryClass) { var interfaceVisitor = new InterfaceVirtualizationVisitor(); var project = Solution.Projects.First(x => x.Name == interfacesProjectName); var tree = repositoryClass.SyntaxTree; var cls = tree.GetRoot().DescendantNodes().OfType<ClassDeclarationSyntax>().Last(); var compilation = CSharpCompilation.Create("comp").AddSyntaxTrees(tree); var projectCompilation = await project.GetCompilationAsync(); var names = compilation.GetSemanticModel(tree).GetDeclaredSymbol(cls).Interfaces; foreach (var syntaxTree in projectCompilation.SyntaxTrees) { interfaceVisitor.Visit(syntaxTree.GetRoot()); } return interfaceVisitor.interfaces.Where(x => names.Any(a => a.Name == x.Identifier.ToString())); }