1

I wanted to use nameof() to get variable name, to later use it in something like an Undo/Redo method. In my specific case I have 2 different classes, which I want to use the Undo/Redo function.

My first thought was to push changes to a stack in a format. However, will I be able to use it in the code itself? Is something like the code below possible?

public class Person
{
    public string Name;
    public Person(string Name)
    {
        this.Name = Name;
    }
}

class Program
{
    static void Main(string[] args)
    {
        var person = new Person("Pete");
        var nameProperty = nameof(person.Name);
        person.nameProperty = "Luke";
    }
}
Samuel Novelinka
  • 328
  • 3
  • 10
  • 5
    That code will not work; there is no property named `nameProperty` on `Person`. You'd have to use reflection to get the actual property and set it. See [Can I set a property value with Reflection?](//stackoverflow.com/q/7718792) – Heretic Monkey Aug 30 '18 at 15:53
  • No, it is not possible like that. – FCin Aug 30 '18 at 15:53

0 Answers0