Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NotNullAttribute missing when checking by reflection

What we are trying to do is to list all properties of the class with NotNull attribute. The one from .NET, not from JetBrains. Unfortunately, it looks like NotNullAttribute is removed during the compilation process (or on some other stage) and it can't be observed in the runtime.

Does anyone know why does it happen? Can't find an explanation on the internet/MSDN.

Here is a test that can easily reproduce it. It fails on the second assertion.

public class Tests
{
    public class Foo
    {
        [NotNull, Required] public string? Bar { get; set; }
    }

    [Test]
    public void GetAttributesTest()
    {
        var type = typeof(Foo);
        var property = type.GetProperties()[0];

        Attribute.IsDefined(property, typeof(RequiredAttribute)).Should().BeTrue();
        Attribute.IsDefined(property, typeof(NotNullAttribute)).Should().BeTrue();
    }
}
like image 929
GrzegorzM Avatar asked Feb 04 '26 10:02

GrzegorzM


1 Answers

If you use SharpLab you can see in the lowered code that the attribute is indeed removed from the property, and instead is applied to the return parameter:

public string Bar
{
    [CompilerGenerated]
    [return: NotNull] // Note the "return" target
    get
    {
        return <Bar>k__BackingField;
    }
    //snip
}

So if you want to get the NotNull attribute, you need to dive deeper into the structure. For example:

var type = typeof(Foo);
var property = type.GetProperties()[0];
var getMethod = property.GetGetMethod()!;
var returnParameter = getMethod.ReturnParameter;

Attribute.IsDefined(returnParameter, typeof(NotNullAttribute)).Should().BeTrue();
like image 74
DavidG Avatar answered Feb 06 '26 23:02

DavidG