Retrieving ISymbol Documentation from MetadataReference

Recently, I began to study Roslyn Code analysis. I went through the provided sample codes. My question is this:

Is there a way to get the XML documentation comment for a character loaded from a reference library?

The sample code I worked with is FAQ(7). The goal is to get a comment on the documentation of, say, a function Console.Write.

    public void GetWriteXmlComment()
    {
        var project1Id = ProjectId.CreateNewId();
        var document1Id = DocumentId.CreateNewId(project1Id);

        var mscorlib = MetadataReference.CreateFromFile(typeof(object).Assembly.Location);

        var solution = new AdhocWorkspace().CurrentSolution
            .AddProject(project1Id, "Project1", "Project1", LanguageNames.CSharp)
            .AddMetadataReference(project1Id, mscorlib);

        var declarations = SymbolFinder.FindDeclarationsAsync(solution.Projects.First(), "Write", true).Result;

        var decFirst = declarations.First();
        var commentXml = decFirst.GetDocumentationCommentXml();
    }

The sample code works well for some methods - it gets the text of the documentation. But for methods such as Console.Write, it uses NullDocumentationProviderand therefore returns an empty string.

UPDATE

I found that I can load MetadataReferencewith the instance TestDocumentationProvideras follows:

        var mscorlib = MetadataReference.CreateFromFile(typeof(object).Assembly.Location,
            default(MetadataReferenceProperties), new TestDocumentationProvider());

where TestDocumentationProviderimplements an abstract class Microsoft.CodeAnalysis DocumentationProvider.

    private class TestDocumentationProvider : DocumentationProvider
    {
        protected override string GetDocumentationForSymbol(string documentationMemberID, CultureInfo preferredCulture, CancellationToken cancellationToken = default(CancellationToken))
        {
            // To-Be-Done
        }
    }

, documentationMemberID?

+4
1

. Roslyn 2.0 XmlDocumentationProvider.CreateFromFile.


, , - Reflection FileBasedXmlDocumentationProvider ( GitHub). , .

private static MetadataReference FromType(Type type)
{
    var path = type.Assembly.Location;
    return MetadataReference.CreateFromFile(path, documentation: GetDocumentationProvider(path));
}

private static string GetReferenceAssembliesPath()
{
    var programFiles =
        Environment.GetFolderPath(Environment.Is64BitOperatingSystem
            ? Environment.SpecialFolder.ProgramFilesX86
            : Environment.SpecialFolder.ProgramFiles);
    var path = Path.Combine(programFiles, @"Reference Assemblies\Microsoft\Framework\.NETFramework");
    if (Directory.Exists(path))
    {
        var directories = Directory.EnumerateDirectories(path).OrderByDescending(Path.GetFileName);
        return directories.FirstOrDefault();
    }
    return null;
}

private static DocumentationProvider GetDocumentationProvider(string location)
{
    var referenceLocation = Path.ChangeExtension(location, "xml");
    if (File.Exists(referenceLocation))
    {
        return GetXmlDocumentationProvider(referenceLocation);
    }
    var referenceAssembliesPath = GetReferenceAssembliesPath();
    if (referenceAssembliesPath != null)
    {
        var fileName = Path.GetFileName(location);
        referenceLocation = Path.ChangeExtension(Path.Combine(referenceAssembliesPath, fileName), "xml");
        if (File.Exists(referenceLocation))
        {
            return GetXmlDocumentationProvider(referenceLocation);
        }
    }
    return null;
}

private static DocumentationProvider GetXmlDocumentationProvider(string location)
{
    return (DocumentationProvider)Activator.CreateInstance(Type.GetType(
        "Microsoft.CodeAnalysis.FileBasedXmlDocumentationProvider, Microsoft.CodeAnalysis.Workspaces.Desktop"),
        location);
}

- RoslynPad.

+3

Source: https://habr.com/ru/post/1648916/


All Articles