jeudi 23 mai 2019

Counting properties of a particular class?

I'm trying to retrieve the number of properties of a certain type. For example in class Player:

public Player(string name, Stat endurance, Stat sprint, Stat dribble, Stat passing, Stat shooting)
{
   //Some code
}

public Stat Endurance { get; private set; }
public Stat Sprint { get; private set; }
public Stat Dribble { get; private set; }
public Stat Passing { get; set; }
public Stat Shooting { get; private set; }

public double OverallSkillLevel { get; private set; }    {

public string Name { get; private set; }    

public void CalculateAverage()
{        
    double sumOfStat = this.Endurance.Level + this.Sprint.Level + 
                       this.Dribble.Level + this.Shooting.Level + 
                       this.Passing.Level;
    //MethodOne:
    Type type =    this.GetType();
    int countOne = type .GetProperties().Count(x => x.MemberType is Stat);

    //MethodTwo
    double countTwo = this.GetType().GetFields().Count(x => x is Stat);

    //this.OverallSkillLevel = Math.Round((sumOfStat /  ), 0);
}

I expect the variables "countOne " or "countTwo " to return me as a count only the properties that are Stat. But I always get 0





Aucun commentaire:

Enregistrer un commentaire