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

213
Views
¿Cómo restringir un parámetro de tipo genérico a clases secundarias?

Estoy tratando de hacer algo como eso:

 internal class ConcreteLinkedItem : GenericLinkedItem<ConcreteLinkedItem> { //Specific methods which use GenericLinkedItem } internal class GenericLinkedItem<TItem> where TItem : GenericLinkedItem<TItem> { public TItem? Next { get; private set; } public TItem? Previous { get; private set; } public void AttachNext(TItem item) { Next = item; //Error CS0266 Cannot implicitly convert type 'GenericLinkedItem<TItem>' to 'TItem'... item.Previous = this; } }

Las únicas soluciones que tengo en mente son:

  • Reparto explícito (sin verificación de tipo de tiempo de compilación)
 item.Previous = (TItem)this;
  • Método abstracto para recuperar esto (repetitivo)
 public void AttachPrevious(TItem item) { Previous = item; item.Next = GetThis(); } protected abstract TItem GetThis();
  • Use la composición en lugar de la herencia, también conocida como System.Collections.Generic.LinkedList (aún más repetitivo).

¿Alguna idea hermosa?

PD: Por ahora voy con "TItem abstracto protegido Este { get; }". Lo cual todavía no es lo que quiero, ya que puede mezclar diferentes tipos de elementos (nodos), pero al menos es seguro escribir:

 internal class ConcreteLinkedItem : GenericLinkedItem<ConcreteLinkedItem> { //Specific methods which use GenericLinkedItem protected override ConcreteLinkedItem This => this; } internal abstract class GenericLinkedItem<TItem> where TItem : GenericLinkedItem<TItem> { public TItem? Next { get; private set; } public TItem? Previous { get; private set; } public void AttachPrevious(TItem item) { Previous = item; item.Next = This; } protected abstract TItem This { get; } }
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

usar una interfaz:

 internal class ConcreteLinkedItem : IGenericLinkedItem<ConcreteLinkedItem> { public ConcreteLinkedItem? Next { get; private set; } public ConcreteLinkedItem? Previous { get; private set; } public void AttachNext(ConcreteLinkedItem item) { Next = item; item.Previous = this; } } internal interface IGenericLinkedItem<TItem> where TItem : IGenericLinkedItem<TItem> { TItem? Next { get; } TItem? Previous { get; } void AttachNext(TItem item); }

o si debe tener una clase genérica, necesitará un molde:

 internal class ConcreteLinkedItem : BaseLinkedItem<ConcreteLinkedItem> { //Specific methods which use GenericLinkedItem } internal class BaseLinkedItem<TItem> : IGenericLinkedItem<TItem> where TItem : BaseLinkedItem<TItem> { public TItem? Next { get; private set; } public TItem? Previous { get; private set; } public void AttachNext(TItem item) { Next = item; item.Previous = (TItem)this; } } internal interface IGenericLinkedItem<TItem> where TItem : IGenericLinkedItem<TItem> { TItem? Next { get; } TItem? Previous { get; } void AttachNext(TItem item); }
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!