3
Is there any more "elegant" way to convert a dynamic
for a query string?
I’m currently doing with several Replace()
but it’s not quite what I wanted.
string data = new
{
email = email,
password = password
}
.ToString()
.Replace(',', '&')
.Replace(' ', char.MinValue)
.Replace('{', char.MinValue)
.Replace('}', char.MinValue);
I tried to create an extension that would solve this case for me, but it doesn’t work with the guy dynamic
public static StringContent ToQueryString(this object model)
{
Dictionary<string, string> query = new Dictionary<string, string>();
dynamic data = model as dynamic;
foreach (PropertyInfo property in data.GetType().GetProperties())
query.Add(property.Name, property.GetValue(data, null));
return new StringContent(string.Join("&", query));
}
I didn’t quite understand your code, but apparently if you want to take an object of any kind and turn into query String would be this ?
– novic