RicoSuter / Namotion.Reflection

.NET library with advanced reflection APIs.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Nested types can incorrectly show Unknown instead of NotNullable for properties

ProductiveRage opened this issue · comments

The following code works as expected, it outputs "PersonDetails.Name.Nullability: NotNullable" -

#nullable enable

using System;
using Namotion.Reflection;

namespace Tester
{
    static class Program
    {
        static void Main()
        {
            Console.WriteLine("PersonDetails.Name.Nullability: " + typeof(PersonDetails).GetContextualProperties()[0].Nullability);
        }

        public sealed class PersonDetails
        {
            public PersonDetails(string name) => Name = name;
            public string Name { get; }
        }
    }
}

(See dotnetfiddle.net/Jqdk7S)

However, if I add a second nested classed with Program then the output becomes "PersonDetails.Name.Nullability: Unknown" -

#nullable enable

using System;
using Namotion.Reflection;

namespace Tester
{
    static class Program
    {
        static void Main()
        {
            Console.WriteLine("PersonDetails.Name.Nullability: " + typeof(PersonDetails).GetContextualProperties()[0].Nullability);
        }

        public sealed class PersonDetails
        {
            public PersonDetails(string name) => Name = name;
            public string Name { get; }
        }

        public sealed class RoleDetails
        {
            public RoleDetails(string name) => Name = name;
            public string Name { get; }
        }
    }
}

(See dotnetfiddle.net/9rYIow)

Looking at the attributes that the compiler generates for these, it seems to remove the [NullableContext] attribute from the PersonDetails class in the second version and append it to the Program class, though it leaves the [Nullable] on PersonDetails - I've started reading through your explanations of these attributes but I don't understand enough yet to be able to tell whether this could be causing the problem!

image

image

v1.0.10