Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

163
Views
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();
    }
}
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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();
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!