title: How do I check with Roslyn if a Type is a Nullable Reference
body: <p>I am trying to analyze with roslyn if a type declaration is a "Nullable Reference" type (C#8)</p>
  
  <p>I was planing on looking if the <code>TypeSyntex</code> was a <code>NullableTypeSyntax</code> and if the <code>ITypeSymbol.IsReferenceType</code> is <code>true</code>.</p>
  
  <p>The following code works for the most part, but is fails when the declared type is generic i.e. <code>List&lt;T&gt;?</code></p>
  
  <pre class="lang-cs prettyprint-override"><code>void Main()
  {
      string text = @"
          #nullable enable
          public class MyClass
          {
              public string? Get() =&gt; null;
              public List&lt;string&gt;? GetGeneric() =&gt; null;
          }";
      SyntaxTree tree = CSharpSyntaxTree.ParseText(text);
      PortableExecutableReference mscorlib = MetadataReference.CreateFromFile(typeof(object).Assembly.Location);
      CSharpCompilation compilation = CSharpCompilation.Create("RefitCompilation", syntaxTrees: new[] { tree }, references: new[] { mscorlib });
      SemanticModel semanticModel = compilation.GetSemanticModel(tree);
  
      MethodDeclarationSyntax nonGenericMethodSyntax = tree.GetRoot().DescendantNodes().OfType&lt;MethodDeclarationSyntax&gt;().First();
      ITypeSymbol nonGenericReturnType = semanticModel.GetTypeInfo(nonGenericMethodSyntax.ReturnType).Type;
      bool isNullableTypeReference = nonGenericMethodSyntax.ReturnType is NullableTypeSyntax &amp;&amp; nonGenericReturnType.IsReferenceType;
      Console.WriteLine($@"NonGeneric Nullalbe Reference: `{nonGenericMethodSyntax}`
          Is Nullable Type Reference: {isNullableTypeReference}
          Original Definition: {nonGenericReturnType.OriginalDefinition}, 
          IsNullableTypeSyntax: {nonGenericMethodSyntax.ReturnType is NullableTypeSyntax}
          Is Reference Type: {nonGenericReturnType.IsReferenceType}");
  
      Console.WriteLine();
  
      MethodDeclarationSyntax genericMethodSyntax = tree.GetRoot().DescendantNodes().OfType&lt;MethodDeclarationSyntax&gt;().Last();
      ITypeSymbol genericReturnType = semanticModel.GetTypeInfo(genericMethodSyntax.ReturnType).Type;
      isNullableTypeReference = genericMethodSyntax.ReturnType is NullableTypeSyntax &amp;&amp; genericReturnType.IsReferenceType;
      Console.WriteLine($@"Generic Nullalbe Reference: `{genericMethodSyntax}`
          Is Nullable Type Reference: {isNullableTypeReference}
          Original Definition: {genericReturnType.OriginalDefinition}, 
          IsNullableTypeSyntax: {genericMethodSyntax.ReturnType is NullableTypeSyntax}
          Is Reference Type: {genericReturnType.IsReferenceType}");
  }
  </code></pre>
  
  <p>which outputs
  </p>
  
  <pre><code>NonGeneric Nullalbe Reference: `public string? Get() =&gt; null;`
          Is Nullable Type Reference: True
          Original Definition: string, 
          IsNullableTypeSyntax: True
          Is Reference Type: True
  
  Generic Nullalbe Reference: `public List&lt;string&gt;? GetGeneric() =&gt; null;`
          Is Nullable Type Reference: False
          Original Definition: System.Nullable&lt;T&gt;, 
          IsNullableTypeSyntax: True
          Is Reference Type: False
  </code></pre>
  
  <p>Why is <code>List&lt;T&gt;?</code> Original Definition <code>System.Nullable&lt;T&gt;</code>? and how can I determine if a type is a Nullable Reference type?</p>