Question

How do I find out what keyword has been used for declaring a local variable? how do I differentiate between the following:

var hi = "asdf";
string hi = "asdf";

Also, is there a comprehensive API documentation available for writing Roslyn diagnostics? Has anyone written a book on this yet?

Was it helpful?

Solution

Look at the Type property of a LocalVariableDeclarationSyntax. In one case it will be a PredefinedTypeSyntax, and in the other a NamedTypeSyntax where the name happens to be "var".

Remember, "var" is a contextual keyword, and isn't known to be special in the syntax layer.

Note: I highly recommend installing the SyntaxVisualizer extension that is in the Roslyn SDK zip to see what the parser does with various bits of text.

OTHER TIPS

There is IsVar property on TypeSyntax class.

public bool IsVar
{
    get
    {
        var ts = this.Green as InternalSyntax.IdentifierNameSyntax;
        return ts != null && ts.Identifier.ToString() == "var";
    }
}

Regarding your second question around documentation, check out the Documentation section of the Roslyn CodePlex site. Also for writing custom Roslyn diagnostics, check out the following walkthrough links under the Samples and Walkthroughs section on the same site.

enter image description here

You may also find the FAQ section useful as you learn your way around the Roslyn APIs. The code in the answers for these FAQs is included in the form of unit tests under the Samples folder in the Roslyn SDK Preview. The Samples folder also contains some examples of writing custom Roslyn diagnostics that you may find useful.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top