无法将类型'System.Collections.ArrayList'隐式转换为'BookReservationRQReservation []

时间:2017-09-20 16:22:27

标签: c# arraylist types

我有一个ArrayList我想在其中添加项目,然后用bookingrquest.Reservations分配它我尝试了几次但我没有得到它的解决方案,如何将ArrayList转换为BookReservationRQReservation []

 BookReservationRQ bookingrquest = new BookReservationRQ();
  ArrayList reservation = new ArrayList();

  int count = 0;

  foreach (var item in reservation_details.selectedrooms)
  {
      reservation.Add(item.IDHotel);
      count++;
  }


  bookingrquest.Reservations = reservation;// i have this error in this line


   public BookReservationRQReservation[] Reservations {
     get {
        return this.reservationsField;
     }
     set {
         this.reservationsField = value;
    }
    }

1 个答案:

答案 0 :(得分:-1)

由于您的请求对象需要一个数组,如何通过替换:

来直接uzing数组
ArrayList reservation = new ArrayList();

使用

BookReservationRQReservation reservation = new BookReservationRQReservation[reservation_details.selectedrooms.Length];

假设reservation_details.selected是List

然后替换

 reservation.Add(item.IDHotel);

使用

reservation[iter++] = item.IDHotel;

在for循环之前,请初始化iter = 0