jeudi 7 septembre 2017

Accessing and setting child class field from within parent class?

Im really having trouble to figure out a nice way to access the child objects fields by string from the parent and setting them. The idea ive had was to store some setter functions into a parents dictionary:

public Base {
    internal Dictionary<string, Action<string>> dict;
}

public A : Base {
  public string name;
  public string age;

  public A() {
    dict.Add("name", SetName);
  }

  public void SetName(string newName) {
    name = newName;
  }
}

public B : Base {
  public string CarType;
  public string Engine;

  // Same here
}

Obviously I will end up with my setters. Is there a more elegant solution?

Aucun commentaire:

Enregistrer un commentaire