I'm working on Unity, using C#, and I made up a script that would make my life simpler if I could access constants using string variables.
public class Foo
{
public const string FooConst = "Foo!";
public const string BarConst = "Bar!";
public const string BazConst = "Baz!";
}
// ...inside some method, somewhere else
public string Bar(string constName)
{
// is it possible to do something like this?
// perhaps with reflections?
return Foo.GetConstant(constName);
}
My only solution was to create a method that gets the constant inside a switch. But every time I add a new constant, I have to modify that switch.
Fun fact: I'm a PHP kid that moved into C#. I like it is pretty strict, strong-typed and stuff... but that also makes things unnecessarily complicated.