I have a method that accepts a bunch of strings in seperate parameters
public string GetQueryString(string string1, string string2, string string3...)
It should return a string in the form of
'string1:value1 string2:value2 string3:value3...'
Because I don’t want to put reflection logic in the callers of GetQueryString, I think working with string[,] parameters is not an option.
What I need now is a way to walk through all my parameters and add ‘parName:parValue‘ to my StringBuilder.
Currently I do this by checking each of them with an if clause
if (!string.IsNullOrEmpty(string1)) sb.AppendFormat('{0}:{1}', 'string1', string1);
How can this be improved?
–CLARIFICATION–
What I believe the OP wants to do is something like this:
string str1 = 'Hello'; string s2 = 'World'; string MyStr = 'Greetings'; string YourStr = 'Earthlings'; string query = GetQueryString(str1, s2, MyStr, YourStr);
and have it return 'str1:Hello s2:World MyStr:Greetings YourStr:Earthlings'
–EDIT–
clarification is correct.
Furthermore, because my method is called GetQueryString doesn’t mean I’m working on an HTML query string.. In this particular case it is actually a Lucene query string. But it could be any search function that can parse strings to a query..
The problem is, as soon as you compile this, the variable names will go away. Reflection won’t even help you.
The only thing I can think of that may work, is to use a expression tree (.NET v3.5+)