samedi 18 juillet 2015

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?





Aucun commentaire:

Enregistrer un commentaire