0

Is it possible to make a dictionary that points to getters & setters?

For example:

public class test
{
  public Dictionary<string, int> pointer = new Dictionary<string,int>()
  {
    { "a", a }
  }; // the goal is to do pointer["a"] = someInt or print(pointer[a])
  
  public int a { get { return b;} set { b = 2; } }
  public int b;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 2
    what are you actually trying to achieve here. (Don't say ' i want to put a setter or getter in a dictionary') – pm100 Apr 04 '22 at 20:46
  • In C++, what you are asking for is doable, setting the dictionary's value type to an intermediate proxy class that overloads `operator=` and `operator int()`. But in C#, I'm not sure this is doable, at least not for the syntax you want. If you are willing to change the syntax to something more like `pointer["a"].set(someInt)` and `print(pointer[a].get())` or `print((int)pointer[a])`, then it is certainly doable to accomplish this in C#. – Remy Lebeau Apr 04 '22 at 20:48
  • What does `pointer["a"] = someInt` or `print(pointer[a])` do for you other than make code that's hard to understand? – D Stanley Apr 04 '22 at 20:49
  • 1
    my guess - you want to be able to access fields in 'test' class by their string names. ie 'test1["a"]' rather than 'test.a' – pm100 Apr 04 '22 at 20:49
  • @pm100 I have a function with some enum as a parameter. My goal is to access the getter/setter directly based on the enum. I can't do test.a = someInt because I don't know whether that enum points to 'a' or 'b' – Palver Preem Apr 04 '22 at 20:53
  • @PalverPreem If that is all that is stopping you, why not simply convert the enum to a string and use Reflection to access the corresponding field? – Remy Lebeau Apr 04 '22 at 20:56

1 Answers1

4

Odds are you are not actually trying to put getters and setters in a dictionary, and that you are trying to access properties by their names. This is easily possible through Reflection. If that is what your question is about you should mark this as duplicate and close the question.

But, the answer to your actual question is yes. It is possible to make a dictionary that points to getters and setters. You will need to use some lambda programming. Here is one way to do it:

public class test
{
    public Dictionary<string, GetSet<int>> pointer;

    public test()
    {
        pointer = new Dictionary<string, GetSet<int>>()
        {
            { "a", new(()=>a,i=>a = i) }
        };
    }

    public int a { get { return b; } set { b = 2; } }
    public int b;
}

public class GetSet<T>
{
    private readonly Func<T> _get;
    private readonly Action<T> _set;

    public GetSet(Func<T> get, Action<T> set)
    {
        _get = get;
        _set = set;
    }

    public T Get() => _get();
    public void Set(T value) => _set(value);
}

Usage:

    var test = new test();
    var property = test.pointer["a"];
    var value = property.Get();//returns the value of test.a
    property.Set(3);//sets test.a to 3
Nigel
  • 2,961
  • 1
  • 14
  • 32