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

93
Views
Uso de propiedad de interfaz explícita al convertir a tipo genérico

Me gustaría llamar a una propiedad de interfaz explícita convirtiéndola en un tipo genérico. Más específicamente, aquí está el ejemplo que traté de hacer funcionar:

 public interface I1 { double Value { get; set; } } public interface I2 : I1 { // Hide value from I1 to use it as an explicit interface property. new double Value { get; set; } } public class C1 : I2 { // Exception because I only care about I2 explicit Value property. double I1.Value { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } double I2.Value { get; set; } } public class ExampleClass<T> where T : I1 { public double? SomeMethod(I1 i1) { if (i1 is T t) return t.Value; return null; } } public static class Main { public static void Test() { var exampleClass = new ExampleClass<I2>(); var c1 = new C1(); ((I2)c1).Value = 1; exampleClass.SomeMethod(c1); } }

Si llamo a Main.Test(), arrojará una excepción porque intentará obtener la propiedad I1.Value en c1. ¿Por qué es ese caso? Me parece que debería lanzar c1 a I2, por lo tanto, devolver la propiedad I2.Value.

over 4 years ago · Santiago Trujillo
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!