C#: how to get an object by the name stored in String?

No, it’s not.

Objects don’t have names – variables do. An object may be referenced by any number of variables: zero, one or many.

What you can do, however, is get fields (static or instance variables) by name (using Type.GetField) and get the values of those fields (for a specific instance, if you’re using instance variables).

Depending on what you’re trying to do, you might also want to consider a dictionary from names to objects.

Leave a Comment