Finding a Type (class) in a submitted string...
-
2012年4月20日 9:16
Hi,
Firstly I think Roslyn is brilliant there are a lot of issues that it will be good to use for resolution.
I have two questions regarding using Roslyn.
1. I have apiece of software I am writing which supplies Roslyn the string contents of a .cs file. I know the 'type'/'class' that I want to find after compiling. It is not clear to me as the best approach to solving this problem. For example, the cs file may have multiple class definitions in it, some may be nested? What about namespaces? etc.
2. Once I have found the class I am interested I want to list all the public properties that the class is exposing?
Any help would be much appreciated!
regards
Richard
全部回复
-
2012年4月20日 22:02所有者:
Hi Richard - the below code example demonstrates a couple of ways to search for class declarations in a code file.
Also , if you are just getting started with Roslyn, I would recommend taking a look at some of the walkthroughs / docs / samples that ship with the CTP. Once you install the CTP, you should see a folder named "Microsoft Codename Roslyn CTP" under your start menu with a "Getting Started" link. Clicking on this link will open the Roslyn Getting Started Guide which is like an index / starting point for exploring the documentation / samples that are installed on your machine with the CTP.
I would recommend taking a look at the Roslyn Project Overview document which provides a high level overview of the APIs included in the CTP (including APIs for syntactic and semantic analysis). This document can also be accessed from your machine by clicking on the corresponding link in the aforementioned Getting Started Guide (see below image). I would also recommend taking a look at the highlighted walkthrough docs below - they should give you a good introduction for the task you asked about above. Hope this helps!
using System; using System.Linq; using Roslyn.Compilers; using Roslyn.Compilers.CSharp; class Program { static void Main() { var code = @" namespace NamespaceYouAreSearchingFor { class ClassYouAreSearchingFor { } class SomeOtherClass { } } namespace SomeOtherNamespace { class ClassYouAreSearchingFor { } }"; SyntaxTree tree = SyntaxTree.ParseCompilationUnit(code); var mscorlib = new AssemblyFileReference(typeof(object).Assembly.Location); Compilation compilation = Compilation.Create("MyCompilation") .AddSyntaxTrees(tree) .AddReferences(mscorlib); // Also check out -> compilation = Roslyn.Services.Solution.Load("<Path>").Projects.First().GetCompilation(); SemanticModel model = compilation.GetSemanticModel(tree); TypeSymbol typeYouAreSearchingFor = compilation.GetTypeByMetadataName("NamespaceYouAreSearchingFor.ClassYouAreSearchingFor"); Console.WriteLine("Find class declaration by searching under tree:"); foreach (var classDecl in tree.Root.DescendentNodes().OfType<ClassDeclarationSyntax>()) { TypeSymbol type = model.GetDeclaredSymbol(classDecl); if (type.Equals(typeYouAreSearchingFor)) { Console.WriteLine(" Success: Found " + type.ToDisplayString()); } else { Console.WriteLine(" Found " + type.ToDisplayString()); } } Console.WriteLine(); Console.WriteLine("Find class declaration using a SyntaxWalker"); var classFinder = new ClassFinder(model, typeYouAreSearchingFor); classFinder.Visit(tree.Root); } class ClassFinder : SyntaxWalker { private SemanticModel model; private TypeSymbol typeYouAreSearchingFor; public ClassFinder(SemanticModel model, TypeSymbol typeYouAreSearchingFor) { this.model = model; this.typeYouAreSearchingFor = typeYouAreSearchingFor; } protected override void VisitClassDeclaration(ClassDeclarationSyntax node) { TypeSymbol type = model.GetDeclaredSymbol(node); if (type.Equals(typeYouAreSearchingFor)) { Console.WriteLine(" Success: Found " + type.ToDisplayString()); } else { Console.WriteLine(" Found " + type.ToDisplayString()); } base.VisitClassDeclaration(node); } } }
Shyam Namboodiripad | Software Development Engineer in Test | Roslyn Compilers Team
- 已编辑 Shyam NamboodiripadMicrosoft Employee, Owner 2012年4月20日 22:04
- 已标记为答案 Richard Matthew Hill 2012年4月26日 9:52
-
2012年4月26日 9:52
Shyam,
Advice to read the docs noted. Your example reply is very much what I was after and I appreciate you spending the time to do this. :D
regards
Richard.
-
2012年4月26日 23:02所有者:Also regarding your 2nd question you should be able to find and list all public properties on the type by looking for symbols with .Kind == SymbolKind.Property under TypeSymbol.GetMembers()...
Shyam Namboodiripad | Software Development Engineer in Test | Roslyn Compilers Team

