is it possible in C# to get an object by name?
i.e. get this.obj0 using
string objectName = "obj0";
executeSomeFunctionOnObject(this.someLoadObjectByName(objectName));
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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.