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
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:
Which will generate the file named
{AssemblyName}.xml
in the output folder (docs):which you then will be able to access for example via the following (sample):
Then comes the hard part – parsing the file, which will look something like the following (for the sample code):
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.
One way would be to use
CodeDOM
and parse your sourcecode. This gives you a DOM-like structure of your code: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: