我有 3 个类形成这样的对象:
public class Pax
{
public PaxType PaxType { get; set; }
public int Age { get; set; }
public string Title { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class Room
{
public Room()
{
Paxes = new List<Pax>();
}
public List<Pax> Paxes { get; set; }
}
public class AvaliableHotelRequest
{
public AvaliableHotelRequest()
{
Rooms = new List<Room>();
}
public string Method { get; set; }
public string ApiKey { get; set; }
public string DestinationId { get; set; }
public DateTime CheckIn { get; set; }
public DateTime CheckOut { get; set; }
public string Currency { get; set; }
public string ClientNationality { get; set; }
public bool OnRequest { get; set; }
public List<Room> Rooms { get; set; }
}
我需要使用反射将以下对象放入这样的字符串中:
var pax1 = new Pax()
{
PaxType = PaxType.Adult
};
var pax2 = new Pax()
{
PaxType = PaxType.Adult
};
var pax3 = new Pax()
{
PaxType = PaxType.Children,
Age = 5
};
var paxList1 = new List<Pax> {pax1, pax2, pax3};
var paxList2 = new List<Pax> { pax2, pax3 };
var rooms = new List<Room>(){new Room(){Paxes = paxList1}, new Room(){Paxes = paxList2}};
var request = new AvaliableHotelRequest()
{
ApiKey = "jhjfjdshsfjkhjhfsdfks",
CheckIn = DateTime.Now,
CheckOut = DateTime.Now.AddDays(5),
ClientNationality = "AU",
Currency = "EUR",
DestinationId = "LD6J",
Method = "getAvailableHotel",
OnRequest = false,
Rooms = rooms
};
输出:?method=getAvailableHotel&apiKey=kggdjjgdhgkjghkgjghkjdg&destinationId=LD6J&checkIn=2011-04-20&checkOut=2011-04-24¤cy=EUR&clientNationality=UK&onRequest=false&rooms[0][0][paxType]=Adult&rooms[0][1][paxType]=Adult&rooms [0][2][paxType]=儿童&房间[0][2][年龄]=6&房间[1][0][paxType]=成人&房间[1][1][paxType]=成人&房间[1][2] [paxType]=孩子&房间[1][2][年龄]=8
一直在尝试不同的方法,但无法通过这个问题。如果你能帮助我,我将不胜感激。
我发现这个Recursively Get Properties & Child Properties Of An Object 但它只是列出了属性而不是值。谢谢。