This project is read-only.

Getting comments

Topics: APIs
Sep 18, 2015 at 1:30 PM
Hi,

I am very new in Roslyn, I want to get the comment information through Roslyn.

For example :
// Summary:
//     Audio Player
public static class Audio
{
    // Summary:
    //     Turn audio on or off.
    public static bool Enabled { get; set; }
    //
    // Summary:
    //     Gets or sets the master volume. Valid range is [0 - 1]
    public static float MasterVolume { get; set; }
}
I am getting the list of Properties but without Comment information.

My Snippet Code :

var parseOptions = ParseOptions.Default.WithParseDocumentationComments(true);
        var tree = Roslyn.Compilers.CSharp.SyntaxTree.ParseText(code, options: parseOptions);

        var mscorlib = MetadataReference.CreateAssemblyReference(typeof(object).Assembly.FullName);

        var compilation = Roslyn.Compilers.CSharp.Compilation.Create(
            "MyCompilation",
            syntaxTrees: new[] { tree },
            references: new[] { mscorlib }
            );

        var semanticModel = compilation.GetSemanticModel(tree);

        ReadOnlyArray<Symbol> symbols = new ReadOnlyArray<Symbol>();
        var p = tree.GetRoot().FindToken(position).Parent;

        ExpressionSyntax identifier = p as ExpressionSyntax;
        if (p is MemberAccessExpressionSyntax)
            identifier = p.ChildNodes().FirstOrDefault() as ExpressionSyntax;
        if (identifier == null)
            identifier = p as LiteralExpressionSyntax;
        if (identifier == null)
            identifier = p as ParenthesizedExpressionSyntax;
        if (identifier == null)
            identifier = p.Parent as InvocationExpressionSyntax;
        if (identifier == null)
            identifier = p.Parent as ObjectCreationExpressionSyntax;
        if (identifier == null)
            return;

        var semanticInfo = semanticModel.GetTypeInfo(identifier);
        var type = semanticInfo.Type;
        symbols = semanticModel.LookupSymbols(position, container: type, options: LookupOptions.IncludeExtensionMethods | LookupOptions.Default);

        if (symbols == null)
            return;

        foreach (var symbol in symbols)
        {
            DocumentationComment docComment = null;
            try
            {
                docComment = symbol.GetDocumentationComment();
                string sDocComment = docComment.SummaryTextOpt; // I am getting null
            }
            catch (Exception ex)
            {
            }
        }
If i pass "Audio." It should return method with the comments (that i am looking for).

Thanks.
Sep 19, 2015 at 10:01 AM
These discussions have moved to http://stackoverflow.com/