(added updates below) I'm wanting to build a method which accepts a string param, and an object which I would like to return a particular member of based on the param. So, the easiest method is to build a switch statement:
public GetMemberByName(MyObject myobj, string name)
{
switch(name){
case "PropOne": return myobj.prop1;
case "PropTwo": return myobj.prop2;
}
}
Works fine, but I may wind up with a rather large list.. so I was curious if theres a way, without writing a bunch of nested if-else structures, to accomplish this in an indexed way, so that the matching field is found by index instead of falling through a switch until a match is found.
I considered using a Dictionary to give fast access to the matching strings (as the key member) but since I'm wanting to access a member of a passed-in object, I'm not sure how this could be accomplished.
(updates)
Ok looks like I need a few more specifics for what I'm trying to do
-Im specifically trying to avoid reflection etc in order to have a very fast implementation. I'll likley use code generation, so the solution doesnt need to be small/tight etc.
-I originally was building a dictionary of but each object was initializing it. So I began to move this to a single method that can look up the values based on the keys- a switch statement. But since I'm no longer indexed, Im afraid the contniuous lookups calling this method would be slow.
-SO: I am looking for a way to combine the performance of an indexed/hashed lookup (like the Dictionary uses) with returning particular properties of a passed-in object. I'll likely put this in a static method within each class it is used for.
Thanks for all the answers so far.