vendredi 26 août 2016

Getting a property just by providing a string name [duplicate]

This question already has an answer here:

looking for hours now on this particular question, I hope you can help. Following class hierachy is given:

public class Toon 
{
 public Skillset skillset;

 public Toon()
 {

    skillset = new Skillset();
 }
}

public class Skillset 
{
    public Skill Fight;
    public Skill Trade;
    public Skill Talk;


   public Skillset()
   {
        Fight = new Skill() { name = "Fight", value = 10 };
        Trade = new Skill() { name = "Trade", value = 12 };
        Talk = new Skill() { name = "Talk", value = 15 };
   }

}

public class Skill
{ 
    public string Name;
    public int Value;
    public int Rank

} 

This is to provide the Syntax of Toon.Skillset.Fight etc.

Now the question: I want to be able to randomly increase Skills, if they are in the favorites list, which is a List.

So basicly:

public void setSkill(string skillname, Toon toon, int value)
{
  => get property and set it to value
}

so that

setSkill("Fight", NewToon, 30); would set Toon.Skillset.Fight to 30.

I hope I could explain clear enough, and I'm thankful for any input. I heard of Reflection to solve this problem nicely, but I found no answer yet.

PS: just getting a List in Toon wouldn't help, it destroys the syntax.





Aucun commentaire:

Enregistrer un commentaire