Im looking to use some reflection to access all the members in a class and get their values. This is the class's. I always initialize the "Championsids" class. (I know there is a lot of code but its quite straight forward).
public class ChampionIds
{
public string type;
public string format;
public string version;
public Data data;
}
public class Data
{
public Champions Aatrox;
public Champions Ahri;
public Champions Akali;
public Champions Alistar;
public Champions Amumu;
public Champions Anivia;
public Champions Annie;
public Champions Ashe;
public Champions AurelionSol;
public Champions Azir;
public Champions Bard;
public Champions Blitzcrank;
public Champions Brand;
public Champions Braum;
public Champions Caitlyn;
public Champions Camille;
public Champions Cassiopeia;
public Champions Chogath;
public Champions Corki;
public Champions Darius;
public Champions Diana;
public Champions Draven;
public Champions DrMundo;
public Champions Ekko;
public Champions Elise;
public Champions Evelynn;
public Champions Ezreal;
public Champions FiddleSticks;
public Champions Fiora;
public Champions Fizz;
public Champions Galio;
public Champions Gangplank;
public Champions Garen;
public Champions Gnar;
public Champions Gragas;
public Champions Graves;
public Champions Hecarim;
public Champions Heimerdinger;
public Champions Illaoi;
public Champions Irelia;
public Champions Ivern;
public Champions Janna;
public Champions JarvanIV;
public Champions Jax;
public Champions Jayce;
public Champions Jhin;
public Champions Jinx;
public Champions Kalista;
public Champions Karma;
public Champions Karthus;
public Champions Kassadin;
public Champions Katarina;
public Champions Kayle;
public Champions Kennen;
public Champions Khazix;
public Champions Kindred;
public Champions Kled;
public Champions KogMaw;
public Champions Leblanc;
public Champions LeeSin;
public Champions Leona;
public Champions Lissandra;
public Champions Lucian;
public Champions Lulu;
public Champions Lux;
public Champions Malphite;
public Champions Malzahar;
public Champions Maokai;
public Champions MasterYi;
public Champions MissFortune;
public Champions MonkeyKing;
public Champions Mordekaiser;
public Champions Morgana;
public Champions Nami;
public Champions Nasus;
public Champions Nautilus;
public Champions Nidalee;
public Champions Nocturne;
public Champions Nunu;
public Champions Olaf;
public Champions Orianna;
public Champions Pantheon;
public Champions Poppy;
public Champions Quinn;
public Champions Rammus;
public Champions RekSai;
public Champions Renekton;
public Champions Rengar;
public Champions Riven;
public Champions Rumble;
public Champions Ryze;
public Champions Sejuani;
public Champions Shaco;
public Champions Shen;
public Champions Shyvana;
public Champions Singed;
public Champions Sion;
public Champions Sivir;
public Champions Skarner;
public Champions Sona;
public Champions Soraka;
public Champions Swain;
public Champions Syndra;
public Champions TahmKench;
public Champions Taliyah;
public Champions Talon;
public Champions Taric;
public Champions Teemo;
public Champions Thresh;
public Champions Tristana;
public Champions Trundle;
public Champions Tryndamere;
public Champions TwistedFate;
public Champions Twitch;
public Champions Udyr;
public Champions Urgot;
public Champions Varus;
public Champions Vayne;
public Champions Veigar;
public Champions Velkoz;
public Champions Vi;
public Champions Viktor;
public Champions Vladimir;
public Champions Volibear;
public Champions Warwick;
public Champions Xerath;
public Champions XinZhao;
public Champions Yasuo;
public Champions Yorick;
public Champions Zac;
public Champions Zed;
public Champions Ziggs;
public Champions Zilean;
public Champions Zyra;
}
public class Champions
{
public string version;
public string id;
public string key;
public string name;
public string title;
public string blurb;
public Dictionary<string, int> info;
public Dictionary<string, string> image;
public List<string> tags;
public string partype;
public Dictionary<string, double> stats;
}
When i initialize it values are automatically assigned to all the members. That all works fine
ChampionIds championIds = JsonConvert.DeserializeObject<ChampionIds>(json2); //Assignment works perfect!
And now i can access all the values as so. Just for an example of usage
Console.WriteLine(championIds.data.Aatrox.id);
Console.WriteLine(championIds.data.Aatrox.key);
Console.WriteLine(championIds.data.AurelionSol.version);
Console.WriteLine(championIds.data.AurelionSol.title);
But the problem is i want to get all the different champions keys and names into a Dictionary. So something like this.
Dictionary<string, string> ChampIdDict = new Dictionary<string, string>();
ChampIdDict.Add(championIds.data.Aatrox.key, championIds.data.Aatrox.name);
And i want to do this for each champion. For an example like this
Dictionary<string, string> ChampIdDict = new Dictionary<string, string>();
ChampIdDict.Add(championIds.data.Aatrox.key, championIds.data.Aatrox.name);
ChampIdDict.Add(championIds.data.Ahri.key, championIds.data.Ahri.name);
ChampIdDict.Add(championIds.data.Akali.key, championIds.data.Akali.name);
//and so on
But i dont want to make 100 lines in my code of this way. So i iterate through all members and get their names easily with this code
FieldInfo[] fields = typeof(Data).GetFields();
foreach (var field in fields)
{
Console.WriteLine(field.Name);
}
And the result is this
Aatrox
Ahri
Akali
Alistar
Amumu
Anivia
//and so on
How do i make it so i can do this
FieldInfo[] fields = typeof(Data).GetFields();
foreach (var field in fields)
{
Console.WriteLine("Inserting champion = " + field.Name + " into the dictionary");
string key = championIds.data.(field.Name).key;
string name = championIds.data.(field.Name).name;
ChampIdDict.Add(key, name)
}
The result is that i simple get the champion id from a website and i say string returnedChampionId = //from website ex. 266 string championName = ChampIdDict["266"];
Thanks for reading. Hope you can recommend something
Aucun commentaire:
Enregistrer un commentaire