Jack Glow
Jack Glow

Reputation: 51

How to loop all class variables with key and value

I'm making an Save Version checker and if version doesn't match transport old variable values into new.

I have this class

[System.Serializable]
public class SavePattern
{
    public int SaveVersion = 1;
    public string Name;
    public int Electricity;
    public string[] Inventory;
}

I want to loop all public variables of class and be able to use it like key and value variable.

Upvotes: 2

Views: 1574

Answers (1)

ArunPratap
ArunPratap

Reputation: 5020

you can loop your class variable by using System.Reflection;

SavePattern savePatt = new SavePattern();
PropertyInfo[] properties = typeof(SavePattern).GetProperties();

foreach (PropertyInfo property in properties)
{
    var val = property.GetValue(savePatt);
}   

Upvotes: 2

Related Questions