Repository navigation
Fixes and improvements to Semantic analysis Get started #4575
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,8 @@ | ||
| using System; | ||
| using static System.Console; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Text; | ||
| using Microsoft.CodeAnalysis; | ||
| using Microsoft.CodeAnalysis.CSharp; | ||
| using Microsoft.CodeAnalysis.CSharp.Symbols; | ||
| using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
| using Microsoft.CodeAnalysis.MSBuild; | ||
| using Microsoft.CodeAnalysis.Text; | ||
|
|
||
| namespace SemanticQuickStart | ||
| { | ||
|
|
@@ -37,22 +31,22 @@ static void Main(string[] args) | |
| // <Snippet2> | ||
| SyntaxTree tree = CSharpSyntaxTree.ParseText(programText); | ||
|
|
||
| var root = (CompilationUnitSyntax)tree.GetRoot(); | ||
| CompilationUnitSyntax root = tree.GetCompilationUnitRoot(); | ||
| // </Snippet2> | ||
|
|
||
| // <Snippet3> | ||
| var compilation = CSharpCompilation.Create("HelloWorld") | ||
| .AddReferences(MetadataReference.CreateFromFile( | ||
| typeof(string).Assembly.Location)) | ||
| .AddSyntaxTrees(tree); | ||
| .AddSyntaxTrees(tree); | ||
| // </Snippet3> | ||
|
|
||
| // <Snippet4> | ||
| SemanticModel model = compilation.GetSemanticModel(tree); | ||
| // </Snippet4> | ||
|
|
||
| // <Snippet5> | ||
| // Use the syntax tree to find "using System" | ||
| // Use the syntax tree to find "using System;" | ||
| UsingDirectiveSyntax usingSystem = root.Usings[0]; | ||
| NameSyntax systemName = usingSystem.Name; | ||
|
|
||
|
|
@@ -64,7 +58,7 @@ static void Main(string[] args) | |
| var systemSymbol = (INamespaceSymbol)nameInfo.Symbol; | ||
| foreach (INamespaceSymbol ns in systemSymbol.GetNamespaceMembers()) | ||
| { | ||
| Console.WriteLine(ns.Name); | ||
| Console.WriteLine(ns); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm curious why you made this change. I liked showing the distinction between
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that knowing how to get the full namespace name is more useful and less discoverable than getting just the scoped name. Also, I think it makes the output clearer: when I see the list For those reasons, I prefer it this way, but it's not a very strong preference either. |
||
| } | ||
| // </Snippet6> | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What changed here is added "and" in "assembly references, and compiler options".
(GitHub doesn't highlight this change.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @svick (BTW, the "rich diff" button at the top of each diff will highlight changes like these.