<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>prakashb</title><link>http://blogs.msdn.com/b/prakashb/</link><description /><dc:language>en-US</dc:language><generator>Telligent Community 5.6.583.20496 (Build: 5.6.583.20496)</generator><item><title>Semantic Information – Part 1</title><link>http://blogs.msdn.com/b/prakashb/archive/2012/02/06/semantic-information-part-1.aspx</link><pubDate>Mon, 06 Feb 2012 23:40:15 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10264664</guid><dc:creator>Prakash B</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/prakashb/rsscomments.aspx?WeblogPostID=10264664</wfw:commentRss><comments>http://blogs.msdn.com/b/prakashb/archive/2012/02/06/semantic-information-part-1.aspx#comments</comments><description>&lt;p&gt;&lt;strong&gt;NOTE&lt;/strong&gt;: This blog post contains code samples that work with &lt;a href="http://msdn.microsoft.com/en-us/roslyn"&gt;Microsoft “Roslyn” October 2011 CTP&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;Roslyn provides a mechanism by which you can get semantic information that the compiler has about the source&amp;#160; code. This blog post explains how you can get the semantic information for certain kinds of syntax nodes and what types of semantic information is provided by Roslyn. &lt;/p&gt;  &lt;p&gt;Roslyn API has a method called &lt;em&gt;&lt;strong&gt;GetSemanticModel&lt;/strong&gt;&lt;/em&gt; on the &lt;strong&gt;&lt;em&gt;Compilation&lt;/em&gt;&lt;/strong&gt; object which takes the syntax tree for which semantic information is required and returns an instance of an object of type &lt;em&gt;&lt;strong&gt;SemanticModel&lt;/strong&gt;&lt;/em&gt; that has a bunch of members which provides answers for the semantic questions about specific syntax nodes in the tree. In this blog post we will be exploring in detail the results of the &lt;strong&gt;&lt;em&gt;GetSemanticInfo&lt;/em&gt;&lt;/strong&gt; method on the &lt;strong&gt;&lt;em&gt;SemanticModel&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;em&gt;GetSemanticInfo&lt;/em&gt;&lt;/strong&gt; takes an &lt;strong&gt;&lt;em&gt;ExpressionSyntax&lt;/em&gt;&lt;/strong&gt; node as an argument (C# has another overload which takes a &lt;strong&gt;&lt;em&gt;ConstructorInitializerSyntax&lt;/em&gt;&lt;/strong&gt; node as an argument) and returns an &lt;strong&gt;&lt;em&gt;SemanticInfo&lt;/em&gt;&lt;/strong&gt; object which contains semantic information such as type, symbols and diagnostics information pertaining to that specific node.&lt;/p&gt;  &lt;p&gt;The following is the list of properties that are available on the &lt;strong&gt;&lt;em&gt;SemanticInfo&lt;/em&gt;&lt;/strong&gt; object:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;strong&gt;CandidateReason&lt;/strong&gt;: Specifies the reason in case of failure to resolve the node to a symbol. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;CandidateSymbols&lt;/strong&gt;: Contains the list of Symbols that were considered for the given node. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;ConstantValue&lt;/strong&gt;: Contains the value in case the node is an expression which is a compile time constant. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;ConvertedType&lt;/strong&gt;: Returns the type of the expression node if it underwent an implicit conversion. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;ImplicitConversion&lt;/strong&gt;: Returns information about the conversion if the expression node underwent an implicit conversion. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;IsCompileTimeConstant&lt;/strong&gt;: Returns true if the expression is a compile-time constant. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;MethodGroup (C#) / MemberGroup (VB)&lt;/strong&gt;: List of symbols that were considered when a symbol resolves to a method group for C# and method or property group for VB. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Symbol&lt;/strong&gt;: Symbol to which the node is resolved to. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Type&lt;/strong&gt;: Type of the expression represented by the syntax node. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Let us consider the source code below for the purpose of illustration:&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;C#:&lt;/strong&gt;&lt;/p&gt;  &lt;blockquote&gt;   &lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;class&lt;/span&gt; Program
{
    &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Main(&lt;span class="kwrd"&gt;string&lt;/span&gt;[] args)
    {
        &lt;span class="kwrd"&gt;int&lt;/span&gt; i1 = 10;
        &lt;span class="kwrd"&gt;long&lt;/span&gt; i2 = i1 + (34 + 30);
    }
}
  &lt;/pre&gt;
&lt;/blockquote&gt;


&lt;p&gt;&lt;strong&gt;VB:&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;Module&lt;/span&gt; Program
    &lt;span class="kwrd"&gt;Sub&lt;/span&gt; Main(args &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;String&lt;/span&gt;())
        &lt;span class="kwrd"&gt;Dim&lt;/span&gt; i1 &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Integer&lt;/span&gt; = 10
        &lt;span class="kwrd"&gt;Dim&lt;/span&gt; i2 &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Long&lt;/span&gt; = i1 + (34 + 30)
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt;
&lt;span class="kwrd"&gt;End&lt;/span&gt; Module&lt;/pre&gt;
&lt;/blockquote&gt;


&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;In the above source code, if you would like to get the semantic information for the &lt;strong&gt;NumericLiteralExpression&lt;/strong&gt; node “&lt;strong&gt;34&lt;/strong&gt;”. The code using the Roslyn API will look like this:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;C#:&lt;/strong&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;class&lt;/span&gt; Program
{
    &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Main(&lt;span class="kwrd"&gt;string&lt;/span&gt;[] args)
    {
        &lt;span class="kwrd"&gt;string&lt;/span&gt; code = &lt;span class="str"&gt;@&amp;quot;

class Program
{
    static void Main(string[] args)
    {
        int i1 = 10;
        long i2 = i1 + (34 + 30);
    }
}&amp;quot;&lt;/span&gt;;

        var tree = SyntaxTree.ParseCompilationUnit(code);

        var comp = Compilation.Create(
                        &lt;span class="str"&gt;&amp;quot;Test&amp;quot;&lt;/span&gt;,
                        syntaxTrees: &lt;span class="kwrd"&gt;new&lt;/span&gt;[] { tree },
                        references: &lt;span class="kwrd"&gt;new&lt;/span&gt;[] { &lt;span class="kwrd"&gt;new&lt;/span&gt; AssemblyFileReference(&lt;span class="kwrd"&gt;typeof&lt;/span&gt;(&lt;span class="kwrd"&gt;object&lt;/span&gt;).Assembly.Location) }
                        );

        var model = comp.GetSemanticModel(tree);
        var node = tree.Root.FindToken(code.IndexOf(&lt;span class="str"&gt;&amp;quot;34&amp;quot;&lt;/span&gt;)).Parent &lt;span class="kwrd"&gt;as&lt;/span&gt; ExpressionSyntax;
        var semanticInfo = model.GetSemanticInfo(node);
    }
}&lt;/pre&gt;

&lt;pre class="csharpcode"&gt;&amp;#160;&lt;/pre&gt;


&lt;p&gt;&lt;strong&gt;VB&lt;/strong&gt;:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;Module&lt;/span&gt; Module1
    &lt;span class="kwrd"&gt;Sub&lt;/span&gt; Main()
        &lt;span class="kwrd"&gt;Dim&lt;/span&gt; code &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;String&lt;/span&gt; =
&amp;lt;a&amp;gt;&lt;/pre&gt;

&lt;p&gt;Module Program 
  &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Sub Main(args As String()) 

  &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Dim i1 As Integer = 10 

  &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Dim i2 As Long = i1 + (34 + 30) 

  &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; End Sub 

  &lt;br /&gt;End Module&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&amp;lt;/a&amp;gt;.Value

        &lt;span class="kwrd"&gt;Dim&lt;/span&gt; tree = SyntaxTree.ParseCompilationUnit(code)
        &lt;span class="kwrd"&gt;Dim&lt;/span&gt; comp = Compilation.Create(
            &lt;span class="str"&gt;&amp;quot;Test&amp;quot;&lt;/span&gt;,
            syntaxTrees:=&lt;span class="kwrd"&gt;New&lt;/span&gt; SyntaxTree() {tree},
            references:=&lt;span class="kwrd"&gt;New&lt;/span&gt; MetadataReference() {&lt;span class="kwrd"&gt;New&lt;/span&gt; AssemblyFileReference(&lt;span class="kwrd"&gt;GetType&lt;/span&gt;(&lt;span class="kwrd"&gt;Object&lt;/span&gt;).&lt;span class="kwrd"&gt;Assembly&lt;/span&gt;.Location)})
        &lt;span class="kwrd"&gt;Dim&lt;/span&gt; model = comp.GetSemanticModel(tree)
        &lt;span class="kwrd"&gt;Dim&lt;/span&gt; node = &lt;span class="kwrd"&gt;DirectCast&lt;/span&gt;(tree.Root.FindToken(code.IndexOf(&lt;span class="str"&gt;&amp;quot;34&amp;quot;&lt;/span&gt;)).Parent, ExpressionSyntax)
        &lt;span class="kwrd"&gt;Dim&lt;/span&gt; semanticInfo = model.GetSemanticInfo(node)
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt;
&lt;span class="kwrd"&gt;End&lt;/span&gt; Module&lt;/pre&gt;


&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;Since the expression is an &lt;strong&gt;NumericLiteralExpression&lt;/strong&gt; syntax node “&lt;strong&gt;34&lt;/strong&gt;”, the semantic info returned would have the following information:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;ConstantValue&lt;/strong&gt;: 34 &lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;ConvertedType&lt;/strong&gt;: Integer (No implicit conversion) &lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;ImplicitConversion&lt;/strong&gt;: Identity (No implicit conversion) &lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;IsCompileTimeConstant&lt;/strong&gt;: True &lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;Type&lt;/strong&gt;: Integer &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The rest of the semantic info are not applicable for the &lt;strong&gt;NumericLiteralExpression&lt;/strong&gt; syntax node.&lt;/p&gt;

&lt;p&gt;Now lets get the semantic info for the &lt;strong&gt;AddExpression&lt;/strong&gt; Syntax node “&lt;strong&gt;34 + 30&lt;/strong&gt;” in the above code. You can get to the &lt;strong&gt;AddExpression&lt;/strong&gt; node by replacing the code for declaring the “&lt;strong&gt;node&lt;/strong&gt;” variable in the above sample Roslyn code with the one below:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;C#:&lt;/strong&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;        var node = tree.Root.FindToken(code.IndexOf(&lt;span class="str"&gt;&amp;quot;34&amp;quot;&lt;/span&gt;)).Parent.Parent &lt;span class="kwrd"&gt;as&lt;/span&gt; ExpressionSyntax;&lt;/pre&gt;
&lt;strong&gt;VB&lt;/strong&gt;: 

&lt;pre class="csharpcode"&gt;        &lt;span class="kwrd"&gt;Dim&lt;/span&gt; node = &lt;span class="kwrd"&gt;DirectCast&lt;/span&gt;(tree.Root.FindToken(code.IndexOf(&lt;span class="str"&gt;&amp;quot;34&amp;quot;&lt;/span&gt;)).Parent.Parent, ExpressionSyntax)&lt;/pre&gt;


&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;The semantic info returned would have the following information:&lt;/p&gt;

&lt;ul&gt;&lt;!--StartFragment--&gt;
  &lt;li&gt;&lt;strong&gt;ConstantValue&lt;/strong&gt;: 64 &lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;ConvertedType&lt;/strong&gt;: Integer(No implicit conversion) &lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;ImplicitConversion&lt;/strong&gt;: Identity (No implicit conversion) &lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;IsCompileTimeConstant&lt;/strong&gt;: True &lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;Type&lt;/strong&gt;: Integer &lt;!--EndFragment--&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Since the compiler evaluates the expression “&lt;strong&gt;34 + 30&lt;/strong&gt;” as a compile-time constant the &lt;strong&gt;&lt;em&gt;ConstantValue&lt;/em&gt;&lt;/strong&gt; returned in this case would be “64”.&lt;/p&gt;

&lt;p&gt;Now lets get the semantic info for the expression “&lt;strong&gt;i1 + (34 + 30)&lt;/strong&gt;”. You can get to this expression node by replacing the code for declaring the “&lt;strong&gt;node&lt;/strong&gt;” variable in the above sample Roslyn code with the one below:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;C#:&lt;/strong&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;  var node = tree.Root.FindToken(code.IndexOf(&lt;span class="str"&gt;&amp;quot;34&amp;quot;&lt;/span&gt;)).Parent.Parent.Parent.Parent &lt;span class="kwrd"&gt;as&lt;/span&gt; ExpressionSyntax;&lt;/pre&gt;


&lt;p&gt;&lt;strong&gt;VB:&lt;/strong&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;  Dim&lt;/span&gt; node = &lt;span class="kwrd"&gt;DirectCast&lt;/span&gt;(tree.Root.FindToken(code.IndexOf(&lt;span class="str"&gt;&amp;quot;34&amp;quot;&lt;/span&gt;)).Parent.Parent.Parent.Parent, ExpressionSyntax)&lt;/pre&gt;


&lt;p&gt;The semantic info returned would have the following information:&lt;/p&gt;

&lt;ul&gt;&lt;!--StartFragment--&gt;
  &lt;li&gt;&lt;strong&gt;ConstantValue&lt;/strong&gt;: null (C#) / Nothing (VB) &lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;ConvertedType&lt;/strong&gt;: Long (Implicit conversion to long) &lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;ImplicitConversion&lt;/strong&gt;: ImplicitNumeric (C#) / WideningNumeric (VB) &lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;IsCompileTimeConstant&lt;/strong&gt;: False &lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;Type&lt;/strong&gt;: Integer &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Note that the type of this expression is &lt;strong&gt;&lt;em&gt;integer&lt;/em&gt;&lt;/strong&gt; but there is an implicit conversion to type &lt;strong&gt;&lt;em&gt;long&lt;/em&gt;&lt;/strong&gt; since the expression is assigned to a variable of type &lt;strong&gt;&lt;em&gt;long&lt;/em&gt;&lt;/strong&gt;. This is reflected in the information returned by the &lt;strong&gt;&lt;em&gt;ConvertedType&lt;/em&gt;&lt;/strong&gt; and the &lt;strong&gt;&lt;em&gt;ImplicitConversion&lt;/em&gt;&lt;/strong&gt; property in the semantic info.&lt;/p&gt;

&lt;p&gt;Now lets get the semantic info for the &lt;strong&gt;IdentifierNameSyntax&lt;/strong&gt; node “&lt;strong&gt;i1&lt;/strong&gt;” which is part of the expression “&lt;strong&gt;i1 + (34 + 30)&lt;/strong&gt;”. You can get to this &lt;strong&gt;IdentifierNameSyntax&lt;/strong&gt; node by replacing the code for declaring the “&lt;strong&gt;node&lt;/strong&gt;” variable in the above sample Roslyn code with the one below:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;C#:&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="csharpcode"&gt;var node = tree.Root.FindToken(code.IndexOf(&lt;span class="str"&gt;&amp;quot;i1 + &amp;quot;&lt;/span&gt;)).Parent &lt;span class="kwrd"&gt;as&lt;/span&gt; ExpressionSyntax;&lt;/pre&gt;
&lt;/blockquote&gt;


&lt;p&gt;&lt;strong&gt;VB:&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;Dim&lt;/span&gt; node = &lt;span class="kwrd"&gt;DirectCast&lt;/span&gt;(tree.Root.FindToken(code.IndexOf(&lt;span class="str"&gt;&amp;quot;i1 + &amp;quot;&lt;/span&gt;)).Parent, ExpressionSyntax)&lt;/pre&gt;
&lt;/blockquote&gt;


&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;The semantic info returned would have the following information:&lt;/p&gt;

&lt;ul&gt;&lt;!--StartFragment--&gt;
  &lt;li&gt;&lt;strong&gt;CandidateReason&lt;/strong&gt;: None (Since the node successfully resolves to a symbol). &lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;CandidateSymbols&lt;/strong&gt;: Empty (Since the node successfully resolves to a symbol). &lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;ConstantValue&lt;/strong&gt;: null (C#) / Nothing (VB) &lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;ConvertedType&lt;/strong&gt;: Integer (No implicit conversion). &lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;ImplicitConversion&lt;/strong&gt;: Identity (No implicit conversion). &lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;IsCompileTimeConstant&lt;/strong&gt;: False. &lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;MethodGroup (C#) / MemberGroup(VB)&lt;/strong&gt;: Empty. &lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;Symbol&lt;/strong&gt;: Local Symbol for variable “&lt;strong&gt;i1&lt;/strong&gt;” &lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;Type&lt;/strong&gt;: Integer. &lt;!--EndFragment--&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Note that the symbol returned contains the symbol of the local variable “&lt;strong&gt;i1&lt;/strong&gt;” since the compiler successfully resolves the &lt;strong&gt;IdentifierNameSyntax&lt;/strong&gt; node. &lt;/p&gt;

&lt;p&gt;In this blog post we learned how to get the semantic information of your source code via the Roslyn API. We also explored the semantic information returned for certain kinds of nodes. &lt;/p&gt;

&lt;p&gt;Next blog post will focus on some scenarios where the node does not successfully resolve to a symbol and the semantic information the Roslyn API provides for such scenarios.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10264664" width="1" height="1"&gt;</description></item><item><title>Operating on specific kind of syntax nodes</title><link>http://blogs.msdn.com/b/prakashb/archive/2011/12/02/operating-on-specific-kind-of-syntax-nodes.aspx</link><pubDate>Sat, 03 Dec 2011 01:13:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10243883</guid><dc:creator>Prakash B</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/prakashb/rsscomments.aspx?WeblogPostID=10243883</wfw:commentRss><comments>http://blogs.msdn.com/b/prakashb/archive/2011/12/02/operating-on-specific-kind-of-syntax-nodes.aspx#comments</comments><description>&lt;p&gt;&lt;strong&gt;NOTE&lt;/strong&gt;: This blog post contains code samples that work with &lt;a href="http://msdn.microsoft.com/en-us/roslyn"&gt;Microsoft &amp;ldquo;Roslyn&amp;rdquo; October 2011 CTP&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;While working with Roslyn, you might often come across situations where you would like to analyze and/or process certain kinds of syntax elements in the source code. This blog post gives you a couple of ways you can go about doing it.&lt;/p&gt;
&lt;p&gt;One approach is to call the DescendentNodes method on the SyntaxNode, this returns all the nodes that descend from the current syntax node. You can call this method on the root node of the tree and then use LINQ queries to filter them based on your requirements.&lt;/p&gt;
&lt;p&gt;For Example: If you want to obtain a list of all syntax nodes corresponding to the &amp;ldquo;do&amp;rdquo; statement in C#, the code will look like:&lt;/p&gt;
&lt;h6&gt;C#:&lt;/h6&gt;
&lt;div align="left"&gt;
&lt;pre class="csharpcode"&gt;    var tree = SyntaxTree.ParseCompilationUnit(code);
    var doStatements = tree.Root.DescendentNodes().
        Where(x =&amp;gt; x.Kind == SyntaxKind.DoStatement);&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Similarly, if you wish to obtain a list of syntax nodes corresponding to the &amp;ldquo;do&amp;rdquo; loop in Visual Basic which has the condition expression at either the top or the bottom of the loop, the code will look like:&lt;/p&gt;
&lt;h6&gt;VB:&lt;/h6&gt;
&lt;pre class="csharpcode"&gt;    &lt;span class="kwrd"&gt;Dim&lt;/span&gt; tree = SyntaxTree.ParseCompilationUnit(code)
    &lt;span class="kwrd"&gt;Dim&lt;/span&gt; doStatements = tree.Root.DescendentNodes().
        Where(&lt;span class="kwrd"&gt;Function&lt;/span&gt;(x)
                 &lt;span class="kwrd"&gt;Return&lt;/span&gt; x.Kind = SyntaxKind.DoLoopBottomTestBlock &lt;span class="kwrd"&gt;OrElse&lt;/span&gt;
                      x.Kind = SyntaxKind.DoLoopTopTestBlock
              &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt;)&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;You can then iterate over the list of nodes and the perform the analysis and/or process them.&lt;/p&gt;
&lt;p&gt;Depending on the kind of processing you intend on doing on the syntax nodes, another option is to implement a SyntaxWalker or SyntaxRewriter and override the methods that visit the syntax nodes that you would like to process.&lt;/p&gt;
&lt;p&gt;If you are interesting in performing updates to a node or rewriting syntax nodes then implementing SyntaxRewriter might be more appropriate.&lt;/p&gt;
&lt;p&gt;The code sample below show the syntax rewriter implementation for updating &amp;ldquo;do&amp;rdquo; statement syntax in C#:&lt;/p&gt;
&lt;h6&gt;C#:&lt;/h6&gt;
&lt;pre class="csharpcode"&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Rewriter:SyntaxRewriter
 {
     &lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; SyntaxNode VisitDoStatement(DoStatementSyntax node)
     {
         &lt;span class="rem"&gt;// Operate on the current node and return the updated node&lt;/span&gt;
     }
 }&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The code sample below show the syntax rewriter implementation for updating &amp;ldquo;do&amp;rdquo; loops in Visual Basic:&lt;/p&gt;
&lt;h6&gt;VB:&lt;/h6&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Class&lt;/span&gt; Rewriter
    &lt;span class="kwrd"&gt;Inherits&lt;/span&gt; SyntaxRewriter
    &lt;span class="kwrd"&gt;Protected&lt;/span&gt; &lt;span class="kwrd"&gt;Overrides&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt; VisitDoLoopBlock(node &lt;span class="kwrd"&gt;As&lt;/span&gt; DoLoopBlockSyntax) &lt;span class="kwrd"&gt;As&lt;/span&gt; SyntaxNode
        &lt;span class="rem"&gt;' Operate on the current node and return the updated node&lt;/span&gt;
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt;
&lt;span class="kwrd"&gt;End&lt;/span&gt; Class&lt;/pre&gt;
&lt;p&gt;&lt;br /&gt;You can then instantiate the writer and call the visit method with the root of the tree as parameter and get back the updated root node.&lt;/p&gt;
&lt;h6&gt;C#:&lt;/h6&gt;
&lt;blockquote&gt;
&lt;pre class="csharpcode"&gt;        var doRewriter = &lt;span class="kwrd"&gt;new&lt;/span&gt; Rewriter();
        var newRoot = doRewriter.Visit(tree.Root);&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;h6&gt;VB:&lt;/h6&gt;
&lt;blockquote&gt;
&lt;pre class="csharpcode"&gt;        &lt;span class="kwrd"&gt;Dim&lt;/span&gt; doRewriter = &lt;span class="kwrd"&gt;New&lt;/span&gt; Rewriter()
        &lt;span class="kwrd"&gt;Dim&lt;/span&gt; newRoot = doRewriter.Visit(tree.Root)&amp;nbsp; &lt;/pre&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Using SyntaxRewriter you can either update the existing info on the node being processed, remove the node by returning a null or return an entirely new node that replaces the node being processed.&lt;/p&gt;
&lt;p&gt;The code sample below illustrates how you can use SyntaxRewriter to transform a &amp;ldquo;do&amp;rdquo; statement in C# to a &amp;ldquo;while&amp;rdquo; statement by replacing the &amp;ldquo;do&amp;rdquo; node with a newly created &amp;ldquo;while&amp;rdquo; syntax node:&lt;/p&gt;
&lt;h6&gt;C#:&lt;/h6&gt;
&lt;p&gt;&lt;span style="font-family: Segoe UI Light;" face="Segoe UI Light"&gt;&lt;/span&gt;&lt;/p&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;class&lt;/span&gt; Rewriter : SyntaxRewriter
{
    &lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; SyntaxNode VisitDoStatement(DoStatementSyntax node)
    {
        &lt;span class="rem"&gt;// Operate on the current node and return the updated node&lt;/span&gt;
        node = (DoStatementSyntax)&lt;span class="kwrd"&gt;base&lt;/span&gt;.VisitDoStatement(node);

        &lt;span class="rem"&gt;// Get the different syntax nodes components of the Do Statement&lt;/span&gt;
        var doKeyword = node.DoKeyword;
        var doStatement = node.Statement;
        var whileKeyword = node.WhileKeyword;
        var condition = node.Condition;
        var openParen = node.OpenParenToken;
        var closeParen = node.CloseParenToken;
        var semicolon = node.SemicolonToken;

        &lt;span class="rem"&gt;// Preserve some level of trivia that was in the original Do keyword node.&lt;/span&gt;
        var newWhileKeyword = Syntax.Token(doKeyword.LeadingTrivia, 
            SyntaxKind.WhileKeyword, 
            whileKeyword.TrailingTrivia);

        &lt;span class="rem"&gt;// Preserve some level of trivia that was in the original Do keyword node and the &lt;/span&gt;
        &lt;span class="rem"&gt;// original CloseParen token.&lt;/span&gt;
        var newCloseParenTrivias = closeParen.TrailingTrivia.ToList();
        newCloseParenTrivias.AddRange(doKeyword.TrailingTrivia.ToList());
        var newCloseParenTriviaList = Syntax.TriviaList(newCloseParenTrivias);
        var newCloseParen = Syntax.Token(closeParen.LeadingTrivia, 
            SyntaxKind.CloseParenToken, 
            newCloseParenTriviaList);

        var newTrailingTrivias = doStatement.GetTrailingTrivia().ToList();
        newTrailingTrivias.AddRange(semicolon.TrailingTrivia.ToList());
        var newWhileStatement = doStatement.WithTrailingTrivia(newTrailingTrivias);

        &lt;span class="kwrd"&gt;return&lt;/span&gt; Syntax.WhileStatement(newWhileKeyword, openParen, 
            condition, newCloseParen, newWhileStatement);
    }
}&lt;/pre&gt;
&lt;p&gt;&lt;span style="font-family: Segoe UI Light;" face="Segoe UI Light"&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Similarly, the code sample below illustrates how you can use SyntaxRewriter to transform a do-while loop which has the test condition on the top of the block in Visual Basic to the corresponding while loop:&lt;/p&gt;
&lt;h6&gt;VB:&lt;/h6&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Class&lt;/span&gt; Rewriter
    &lt;span class="kwrd"&gt;Inherits&lt;/span&gt; SyntaxRewriter
    &lt;span class="kwrd"&gt;Protected&lt;/span&gt; &lt;span class="kwrd"&gt;Overrides&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt; VisitDoLoopBlock(node &lt;span class="kwrd"&gt;As&lt;/span&gt; DoLoopBlockSyntax) &lt;span class="kwrd"&gt;As&lt;/span&gt; SyntaxNode
        node = &lt;span class="kwrd"&gt;DirectCast&lt;/span&gt;(&lt;span class="kwrd"&gt;MyBase&lt;/span&gt;.VisitDoLoopBlock(node), DoLoopBlockSyntax)
        &lt;span class="kwrd"&gt;Dim&lt;/span&gt; beginLoop = node.Begin
        &lt;span class="kwrd"&gt;Dim&lt;/span&gt; endLoop = node.&lt;span class="kwrd"&gt;End&lt;/span&gt;

        &lt;span class="kwrd"&gt;If&lt;/span&gt; (node.Kind = SyntaxKind.DoLoopTopTestBlock) &lt;span class="kwrd"&gt;AndAlso&lt;/span&gt;
            (beginLoop.WhileUntilClauseOpt.WhileOrUntilKeyword.Kind = SyntaxKind.WhileKeyword) &lt;span class="kwrd"&gt;Then&lt;/span&gt;

            &lt;span class="kwrd"&gt;Dim&lt;/span&gt; endKeyword = Syntax.Token(endLoop.LoopKeyword.LeadingTrivia,
                                          SyntaxKind.EndKeyword)

            &lt;span class="kwrd"&gt;Dim&lt;/span&gt; endWhileKeyword = Syntax.Token(Syntax.Whitespace(&lt;span class="str"&gt;" "&lt;/span&gt;),
                                               SyntaxKind.WhileKeyword,
                                               endLoop.LoopKeyword.TrailingTrivia)

            &lt;span class="kwrd"&gt;Dim&lt;/span&gt; endWhile = Syntax.EndWhileStatement(endKeyword, endWhileKeyword)
            &lt;span class="kwrd"&gt;Dim&lt;/span&gt; beginWhileKeyword = Syntax.Token(beginLoop.DoKeyword.LeadingTrivia,
                                                 SyntaxKind.WhileKeyword,
                                                 beginLoop.DoKeyword.TrailingTrivia)

            &lt;span class="kwrd"&gt;Dim&lt;/span&gt; whileStatement = Syntax.WhileStatement(beginWhileKeyword,
                                                       beginLoop.WhileUntilClauseOpt.Condition)

            &lt;span class="kwrd"&gt;Return&lt;/span&gt; Syntax.WhileBlock(whileStatement,
                                     node.BeginTerminator,
                                     node.Statements,
                                     endWhile)
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;If&lt;/span&gt;

        &lt;span class="kwrd"&gt;Return&lt;/span&gt; node
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt;
&lt;span class="kwrd"&gt;End&lt;/span&gt; Class&lt;/pre&gt;
&lt;p&gt;If you are interested in more syntax tree transformations similar to this, please do take a look at the Tree Transformation Samples that ships with the Roslyn CTP. You should be able to access the sample via the &amp;ldquo;Getting Started&amp;rdquo; link once you have installed the Roslyn CTP.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10243883" width="1" height="1"&gt;</description></item><item><title>About me and the blog</title><link>http://blogs.msdn.com/b/prakashb/archive/2011/11/21/about-me-and-the-blog.aspx</link><pubDate>Tue, 22 Nov 2011 01:13:54 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10239388</guid><dc:creator>Prakash B</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/prakashb/rsscomments.aspx?WeblogPostID=10239388</wfw:commentRss><comments>http://blogs.msdn.com/b/prakashb/archive/2011/11/21/about-me-and-the-blog.aspx#comments</comments><description>&lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;h1&gt;ME:&lt;/h1&gt;  &lt;p&gt;Hello, I am Prakash Balasubramanian, a Software Development Engineer in Test in the Visual Studio team at Microsoft. For the last 5 years, I have been working on testing the new language features in the compilers for C# and Visual Basic. I joined the languages team at the beginning of C# 3.0 and LINQ, which were released as part of Visual Studio 2008. I continued to work on the C# Compiler team for the next release of C# 4.0 as part of Visual Studio 2010. Since then I moved on to a new project code-named Roslyn which involves rewriting the compilers for C# and VB in managed code. Nowadays I spend my time testing the new managed compilers for both C# and Visual Basic. You can read more about the Roslyn project &lt;a href="http://msdn.com/roslyn"&gt;here&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h1&gt;BLOG:&lt;/h1&gt;  &lt;p&gt;In this blog, I’ll discuss more about the &lt;a href="http://msdn.microsoft.com/en-us/roslyn"&gt;Roslyn&lt;/a&gt; project and provide some tips on how to use some of the Roslyn APIs. I’ll try to cover some of the most frequently asked questions that we have been seeing in the &lt;a href="http://social.msdn.microsoft.com/forums/en-us/roslyn"&gt;Roslyn forum&lt;/a&gt;. Occasionally I might post on topics related to software testing as well.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10239388" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/prakashb/archive/tags/Roslyn/">Roslyn</category></item></channel></rss>