填充列表我有這些類:從另一個類
class Game
{
public List<Participant> Participants = new List<Participant>();
}
class Participant
{
public int ChampionId { get; set; }
public string SummonerName { get; set; }
public int SummonerId { get; set; }
}
public class APICalls
{
//private Game game;
private string GetResponse(string url)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
WebResponse response = request.GetResponse();
using (Stream responseStream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
return reader.ReadToEnd();
}
}
public void GetParticipants()
{
// create a string from the JSON output
var jsonString = GetResponse("https://euw1.api.riotgames.com/lol/league/v3/positions/by-summoner/27528610?api_key=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
// fill the Participants list from the Game class with participants based on the contents of the jsonString variable
Game.Participants = JsonConvert.DeserializeObject<Game>(jsonString);
}
}
我要填寫與在APICALLS類的GetParticipants方法Game類我與會者列表。我的問題是我無法從這個類訪問列表。
我想將我的所有API代碼保存在一個類文件中,這就是爲什麼我創建了這個類。
我在做什麼錯?
你不能這樣做,因爲你沒有你的'Game'類的實例。一次只能有一個「遊戲」嗎?那麼也許你可以讓'Game'成爲所謂的Singleton。 –
我建議你的函數有一個返回類型,例如''Public List GetParticipants()',這樣你可以在任何你想要的地方聲明一個'APICalls'的實例,並使用'GetParticipants()''方法手動分配值。 –