mercredi 16 février 2022

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();
    }
}




Aucun commentaire:

Enregistrer un commentaire