简体   繁体   中英

Adding each variables in the class together using reflection

Is there a way to add each variables using reflection? For example, the manual way of doing it is:

public class PlayerStats {
    public float Health;
    public float HealthRegen;
    public float Mana;
    public float ManaRegen;
    public float Stamina;
    public float StaminaRegen;
    public float Armor; 
    public float AttackDamage;
    public float AttackDamageCritical;
    public float AttaackSpeed;
    public float AttackRange;
    public float MovementSpeed;

    public static PlayerStats operator +(PlayerStats ps1, PlayerStats ps2){
        PlayerStats returnPlayerStats = new PlayerStats();
        returnPlayerStats.Health = ps1.Health + ps2.Health;
        returnPlayerStats.HealthRegen = ps1.HealthRegen + ps2.HealthRegen;
        returnPlayerStats.Mana = ps1.Mana + ps2.Mana;
        returnPlayerStats.ManaRegen = ps1.ManaRegen + ps2.ManaRegen;
        returnPlayerStats.Stamina = ps1.Stamina + ps2.Stamina;
        returnPlayerStats.StaminaRegen = ps1.StaminaRegen + ps2.StaminaRegen;
        returnPlayerStats.Armor = ps1.Armor + ps2.Armor;
            returnPlayerStats.AttackDamage = ps1.AttackDamage + ps2.AttackDamage;
        returnPlayerStats.AttackDamageCritical = ps1.AttackDamageCritical + ps2.AttackDamageCritical;
        returnPlayerStats.AttaackSpeed = ps1.AttaackSpeed + ps2.AttaackSpeed;
        returnPlayerStats.Health = ps1.AttackRange + ps2.AttackRange;
        returnPlayerStats.Health = ps1.MovementSpeed + ps2.MovementSpeed;
        return returnPlayerStats;
    }
}

So I was thinking can I automate this by using reflection? Maybe something like:

foreach(var field in typeof(PlayerStats).getFields()){
    field.setValue((object)((int)field.getValue(ps1) + (int)field.getValue(ps2)))
}

First of all you are better off leaving it the way it is, as reflection is much slower than normal access, and its a bad idea to include it in operators as they may be used in loops etc, but if you want to achieve a similar effect do the following:

Use a Dictionary seeing as your fields are all floats, then you can add a method to

private Dictionary<string,float> fields = new Dictionary<string,float>()

public PlayerStats()
{
    fields.Add("Health", value);
    // other fields here 
}

public void SetField(string name, float value)
{
     if(!fields.ContainsKey(name))
         throw new InvalidOperationException("Field doesnt exists");
     fields[name] = value;
}

Your class could have a default set of fields which you can add to.

The + operator method would loop through the dictionary keys and sum the values on both objects.

public static PlayerStats operator +(PlayerStats ps1, PlayerStats ps2){
    var playerstats = new PlayerStats();
    foreach(var key in fields.keys)
    {
      playerstats[key] = ps1[key] + ps2[key];
    }
    return playerstats;
}

You can add strongly typed accessor's as properties

public float Health
{
     get { return fields["Health"]; }
     set { field["Health"] = value; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM