Alright, i dont know how to explain it well.. but i have a switch statement,
string mystring = "hello";
switch(mystring)
{
case "hello":
break;
case "goodbye":
break;
case "example":
break;
}
of course this is an example, and in the real situation, there will be different things happening for each case.
ok, hope you get the point, now, doing this manually is impossible, because of the sheer number of different case’s. i need to respectively create a list, of all the cases, so for instance.. for the above switch statement, i would need
string[] list = { "hello", "goodbye", "example" };
maybe could be done with a foreach some how i dont know, any help would be greatly appreciated.
also, any working codes provided would be awesome!
edit:
people are asking for more detail, so here is how it works.
the user of the program, inputs a series of strings.
based on the string(s) they entered, it will do a few if’s and else if’s and throw back the new strings basically. i need to be able to be able to create a list, through the program, of all the options available to use. and i cant just make a list and hard code it in, because im always adding more case’s to the statement, and i cant be going back and keeping a list up to date.
It depends on how clever you want to get… You could create a custom attribute that attaches to a method with the string that method should handle. Then, instead of a
switchstatement, you would just find the attribute with your desired value and execute it.Once you have the framework, you wouldn’t need to alter the
HandleString()code, just add the methods you want to take care of and set theProvidesattribute on them. If you wanted to extend the idea a little further, you could create multiple classes to handle a wide variety of strings, then loop through each type in your assembly looking for theProvidesattribute.EDIT this has the added benefit that you can define multiple methods that act on the same string (by removing the
breakin the loop logic).