Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Documentation generation for nullable types

Tags:

c#

xml

There was a question about the generation of documentation by C# compiler.

Source code:

public class SomeClass {
    /// <summary>
    /// Do some work
    /// </summary>
    /// <returns cref="Nullable{Boolean}">
    /// Some stuff
    /// </returns>
    public bool? SomeMethod() {
       return false;
    }
}

Compiler generated XML documentation fragment for method SomeMethod:

<member name="...." >
 ....
  <returns cref="T:System.Nullable`1">SomeStuff</returns>
</member>

Is there any way to force it to produce

<member name="...." >
 ....
  <returns cref="T:System.Nullable{System.Boolean}">SomeStuff</returns>
</member>

instead.?

like image 432
l0nley Avatar asked Jun 13 '13 10:06

l0nley


People also ask

How do you declare as nullable?

You can declare nullable types using Nullable<t> where T is a type. Nullable<int> i = null; A nullable type can represent the correct range of values for its underlying value type, plus an additional null value. For example, Nullable<int> can be assigned any value from -2147483648 to 2147483647, or a null value.

What is a nullable reference type?

Nullable reference types aren't new class types, but rather annotations on existing reference types. The compiler uses those annotations to help you find potential null reference errors in your code. There's no runtime difference between a non-nullable reference type and a nullable reference type.

What does #nullable enable mean?

#nullable enable : Sets the nullable annotation context and nullable warning context to enable. #nullable disable : Sets the nullable annotation context and nullable warning context to disable. #nullable restore : Restores the nullable annotation context and nullable warning context to the project settings.

Should you use nullable reference types?

Although using nullable reference types can introduce its own set of problems, I still think it's beneficial because it helps you find potential bugs and allows you to better express your intent in the code. For new projects, I would recommend you enable the feature and do your best to write code without warnings.


1 Answers

Unfortunately no.

There is no way to change how the .xml files are produced.

Instead you will have to change whatever it is that is processing those files to handle that syntax, or change the XML documentation.

The problem here is that Nullable{Boolean} is no different from Nullable{T} because the part between the brackets is not understood as a type, but as a generic type parameter. T, Boolean, XYZ, it's all the same thing.

You will either have to change the tool that reads this (and I don't know how you would do that), or write out your references differently:

/// <returns>
/// <see cref="Nullable{T}/> with <c>T</c> being <see cref="Boolean"/>.
/// </returns>
like image 129
Lasse V. Karlsen Avatar answered Sep 20 '22 19:09

Lasse V. Karlsen