If you already have the class and do not know anything about it, and it is fully dynamic you really should use reflection so you can use variables instead of the name. You have a complex example in another response .
If you are going to create the object and want the same JavaScript semantics you should use a dictionary instead of a class, after all in JS the object is actually a dictionary. I already answered with an example . Actually in several examples .
If you do not know what to access then you should use dynamic
, but you should avoid it whenever possible because C # is not a dynamic typing language . Example .
One last solution . Of course, it has even more complicated solutions.
By the comments I would go from switch
, extensive code is not justification for using reflection, the code gets absurdly slower. If you do not want to write the code write a code generator, C # has more and more tools to help with this. But if you still insist on not writing the pure code, at least consider using a dictionary, by the description of the question and comment is what you need. Almost always reflection is gambiarra.
One last tip: what you want is a field and not the attribute , I know they taught you so, but taught wrong, use the correct terminology to avoid confusion.