c#: get class member by name (reflection) -


I have two lists (list) l1 & amp; L2 I'm going out of the list name which I want to use (say l1).

How can I get the requested list?

Do I need to use getMember method?

Thanks

You can use reflection, but it is quite expensive . Here is a question of how to do this:

Given that list name is known at the time of compilation, you can consider implementing a method which lists Accept the string name of and gives a reference to the appropriate list using the switch statement or if statement.

  Fixed IList FindList (name of string) {if (name == "L1") {return L1; } And if (name == "l2") {return l2; } And leave the exception ("list" + name + "not found"); }   

Compared to reflection, this will be faster on runtime, but maintenance is required (if you have enough lists to assure this effort, then you can generate that method Code).

Comments