public bool Update(Tickets ticket) { //Tickets ticDB = TicketsDao.GetSingleTicket(ticket.TicketType, ticket.Beginning, ticket.Destination); Type t = SimpleTicketFactory.CreateTicketType(ticket.TicketType); Tickets ticDB = (Tickets)typeof(TicketsDao).GetMethod("GetSingleTicketGeneric").MakeGenericMethod(t) .Invoke(null, new object[] { ticket.TicketType, ticket.Beginning, ticket.Destination }); ticket.Remainder = ticDB.Remainder + ticket.Remainder; return(TicketsDao.Update(ticket) > 0); }
public List <string> BuyTicket(Tickets ticket, int count) { Type t = SimpleTicketFactory.CreateTicketType(ticket.TicketType); //反射调用泛型方法,由于具体泛型只能在运行时确定 object res = typeof(TicketsDao).GetMethod("GetSingleTicketByIDGeneric").MakeGenericMethod(t).Invoke(null, new object[] { ticket.ID }); Tickets ticDb = (Tickets)res; if (ticDb.Remainder < count) { return(null); } ticket.Remainder = ticDb.Remainder - count; TicketsDao.Update(ticket); NotifyObserver(ticket, count); return(ticDb.GetSeats(count)); }