skip to Main Content

I want to read at runtime the summary text from the class

/// <summary>
/// Text I want to get 1
/// </summary>

class Hello
{
   /// <summary>
   /// Text I want to get 2
   /// </summary>
   string name {get;set;}
}
static void Main(string[] args)
{
}

is that possible ?

I expect to have a dictionary or something else that contains the name of class or the property
and his summary
something like this.

Dictionary<string, string> Dic ; 

for example : {("Hello", "Text i want to get 1"),("name", "Text i want to get 2")}

2

Answers


  1. Not a full answer but some steps helping to achieve your goal.

    First of all you will need to enable generating the documentation file. Add the following to the .csproj file:

    <PropertyGroup>
        <GenerateDocumentationFile>true</GenerateDocumentationFile>
    </PropertyGroup>
    

    Which will generate the file named {AssemblyName}.xml in the output folder (docs):

    The GenerateDocumentationFile property controls whether the compiler generates an XML documentation file for your library. If you set this property to true and you don’t specify a file name via the DocumentationFile property, the generated XML file is placed in the same output directory as your assembly and has the same file name (but with an .xml extension).

    which you then will be able to access for example via the following (sample):

    var readAllText = 
        File.ReadAllText($"{Assembly.GetExecutingAssembly().GetName().Name}.xml");
    

    Then comes the hard part – parsing the file, which will look something like the following (for the sample code):

    <?xml version="1.0"?>
    <doc>
        <assembly>
            <name>NET8Console</name>
        </assembly>
        <members>
            <member name="T:Hello">
                <summary>
                Text I want to get 1
                </summary>
            </member>
            <member name="P:Hello.name">
                <summary>
                Text I want to get 2
                </summary>
            </member>
        </members>
    </doc>
    

    This task is actually already done by Swashbuckle which can use xml comments to to generate Swagger descriptions, so I strongly suggest to look at how they are doing it – see Swashbuckle.AspNetCore.SwaggerGen/XmlComments for example.

    Another option is to look into source generators which allow to analyze your codebase (including XML comments) and generate some code files which will be compiled into the assebmly.

    Login or Signup to reply.
  2. One way would be to use CodeDOM and parse your sourcecode. This gives you a DOM-like structure of your code:

    var provider = CodeDomProvider.CreateProvider("CSharp");
    var dom = provider.Parse(mySourceCode);
    

    Now you can easily search your DOM for all kinds of statements, comments and whatnot. E.g. to get the comments for all your members use this:

    var comments = dom.Types.Cast<CodeTypeDeclaration>()
        .SelectMany(x => x.Member.Cast<CodeTypeMember>())
        .SelectMany(x => x.Comments.Cast<CodeCommentStatement>());
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search