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

后端 未结 4 1784
南方客
南方客 2020-12-06 21:44

is it possible in C# to get an object by name?

i.e. get this.obj0 using

string objectName = \"obj0\";
executeSomeFunctionOnObject(this.someLoadObject         


        
4条回答
  •  孤街浪徒
    2020-12-06 22:08

    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.

提交回复
热议问题