0

I want to convert all C# enums to markdown documentation. I am still considering two options: C# native way and custom grammar implementation. The second may lead to perfect implementation but I have not touched the bison for tens of years.

One C# way is described here: https://dzone.com/articles/converting-c-enums-to-javascript

public static MvcHtmlString EnumToString<T>(this HtmlHelper helper)
{
    var values = Enum.GetValues(typeof(T)).Cast<int>();
    var enumDictionary = values.ToDictionary(value => Enum.GetName(typeof(T), value));           return new MvcHtmlString(JsonConvert.SerializeObject(enumDictionary));
}

Good point is that I can use this method to generate a MD file. Unfortunatelly C# API does not provide a method to get the enum documentation:

/// <summary>pecial forces cars</summary>
C = 1,

Is there a way to overcome this limimation? Can reflection provide the field documentation? I cannot find such method.

I have read Parser for C# and it seems that Roslyn may be helpful too. Would it be a good approach? It is a C# compiler so I worry it is too heavy weight for my use case.

Leos Literak
  • 8,805
  • 19
  • 81
  • 156
  • 2
    You won't get XML docs using reflection -- you'll have to load up the xml docs file yourself. It would be a lot easier to use Roslyn to analyse the C# file – canton7 Apr 13 '21 at 10:22
  • It sounds to me like you want to have *meta* information attached to enum. This is achieved by utilizing *attributes*. See [here](https://stackoverflow.com/q/1799370/1997232). – Sinatr Apr 13 '21 at 10:25
  • @Sinatr: yes, parsing output XML seems legit and much easier than learning Roslyn – Leos Literak Apr 13 '21 at 11:34
  • @canton7 thanks, so my search was correct – Leos Literak Apr 13 '21 at 11:34

0 Answers0