Access variable by string name

I am trying to create a system in which the player can rebind keys.
In a player script, there is a KeyCode variable that is used for inputs.
In my UI to edit the button, I have it run a function in this script that sets an “editingKey” string to the key the player is trying to edit. If they go into the UI and press the “Move Right” keybind button, the button would pass in “Move Right”.
Then, in the OnGUI function that is called on a new event, I set Event e to Event.current, and check if the editingkey string is null. If not null, this means we are editing a key, and so I would want to find the KeyCode variable inside the player’s script where the OnGUI is called, and set the KeyCode “Move Right” to e.keyCode. I know how to do all of this besides find the key the player is currently trying to edit through this button.
I read some other threads that refer to this as Reflections, but some people say that there could be better ways to do this and I can’t seem to find how to do that in this context.

To do it without reflection you would need to encapsulate the information yourself. Such as having an object that ties together both the name and keycode:

[System.Serializable]
public sealed class InputAction
{
	[SerializeField]
	private string _name;
	
	[SerializeField]
	private KeyCode _keyCode;
	
	public string Name => _name;
	
	public KeyCode KeyCode
	{
		get => _keyCode;
		set => _keyCode = value;
	}
}

Then you have a List/Array of these, and look them up to edit what keycode is used. Though you would have to do the same for reading inputs, and would have to cache the lookups.

But at that point you’re just making a ghetto version of all the stuff the Input System package already provides.

I have attempted to make an array of KeyCodes to pass in an int when we press the button to codes[number] = e.keyCode in the OnGUI function
however I get the error:

A field initializer cannot reference the non-static field, method, or property 'PlayerController.moveRight'

on the line:

public KeyCode[] codes = new KeyCode[] {moveRight, moveLeft};

I mean just read the error. You’re trying to field-initialise an array with non-static members. You can’t do that. Assign the keycodes via the inspector.