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

171
Views
Falta NotNullAttribute al verificar por reflexión

Lo que intentamos hacer es enumerar todas las propiedades de la clase con el atributo NotNull. El de .NET, no el de JetBrains. Desafortunadamente, parece que NotNullAttribute se eliminó durante el proceso de compilación (o en alguna otra etapa) y no se puede observar en el tiempo de ejecución.

¿Alguien sabe por qué sucede? No puedo encontrar una explicación en Internet/MSDN.

Aquí hay una prueba que puede reproducirla fácilmente. Falla en la segunda afirmación.

 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

Si usa SharpLab , puede ver en el código reducido que el atributo se elimina de la propiedad y, en su lugar, se aplica al parámetro de retorno:

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

Entonces, si desea obtener el atributo NotNull , debe profundizar en la estructura. Por ejemplo:

 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!