EmiL EmiL - 4 months ago 14
ASP.NET (C#) Question

Adding a property to a list c# that doesn't contain it

I have a method that returns a

List
of datatype
<Reserva>
. This class
Reserva
only has 4 properties which I can return with this method.

Within my diagram
Reserva
is a class that only is instanced within
<Espectaculo>


I want to add a property to the new
List
I´m creating, for example I want to add the name of the
Espectaculo
:
e.name


public List<Reserva> reservasNoPagas()
{
List<Reserva> reservasNoPagas = new List<Reserva>();

foreach (Espectaculo e in espectaculos)
{
reservasNoPagas.AddRange(e.reservasNoPagas());

//somewhere here I want to add e.name to the reservasNoPagas
}

return reservasNoPagas;
}

Answer

You can't add properties dynamically to classes. Inherit the class and add another in a new class:

public class ReservaExtended : Reserva
{
    public string ExtraProperty { get; set; }
}