Is there a more "elegant" way of converting a dynamic
to 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 does not work with type 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));
}