I have a class with a list of variables (shortened to one "a" here) - I'm trying to create a way to populate a variable based on two string inputs, so I can just use, for instance:
SET_VAL("a", "value_for_a")
Which I'd like to get the MessageBox showing just "value_for_a"
but I'm tripping up trying to use reflection, I'm sure this is possible, I know it's possible to get a value from a variable using a string to find it's name, cos every time I search for this that's all I seem to get, but why doesn't the following work? The method SetValue
is asking for objects as parameters when I just want to pass two strings?
public class MySystem_SubsNote
{
public string a;
public void SET_VAL(string mytype, string myvalue)
{
this.GetType().GetField(mytype).SetValue(myvalue);
MessageBox.Show(a);
}
}