Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

164
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda