0

At the moment I am working on a simulator. I am commenting all code using xml tags in visual studio. I would like to use the summary or remark tag of certain methods / properties to use in as content for other purposes (like tooltip text).

Is it possible to reference to xml documentation text?

Thanks in advance!

JR_
  • 183
  • 1
  • 3
  • 15

1 Answers1

2

It was discussed on SO several times, but for some reason it is extremely hard to find.

There are basically two options:

1) Use Roslyn. This is what you want if you have access to the sources. There is a great answer on this topic: How to read XML documentation comments using Roslyn

2) Parse xml documentation by yourself. Along with your binaries, you can find .xml files with all the type descriptions. You need to turn the generation on, before having them - go to solution properties->build and specify XML documentation file If you parse it you can retrieve any information you need. For each element, there is a separate member tag. It has attribute name which defines what element are you looking at. The name specified is has a prefix, which defines the type of the element - a type (T:), a property (P:), etc. The inners of the tag contain all the required info.

Here is an example:

<member name="T:MyNameSpace.MyTuple`2">
  <summary>The summary</summary> 
  <typeparam name="TA">The first tuple type.</typeparam> 
  <typeparam name="TB">The second tuple type.</typeparam> 
</member>

Please notice `2 in the end of the type name - this is because the type is generic.

Here is an example in powershell I used before:

function Get-Type-Xml-Description($typeName) {
    $type = $assembly.GetTypes() | Where-Object { $_.Name -eq $typeName } | Select -First 1
    if ($type -eq $null){
        return $null
    }

    $tName = $type.FullName
    $path = "T:"+$tName
    $node = $xmlDocument.SelectSingleNode("//member[@name = '" + $path + "']")
    return [Regex]::Replace($node["summary"].InnerText.Trim(), "\s+", " ")
}

It uses the $assembly$ to get the full name of a type, but if you know that already and you don't need to iterate over all types, you don't have to have the assembly. Also you need to account generic type names, which have `[number of typeparams] in the end. This powershell example doesn't do this.

Community
  • 1
  • 1
Archeg
  • 8,364
  • 7
  • 43
  • 90