How can I pass an anonymous object to a method, and then traverse it?
What I'm trying to do is the following:
public static string QueryStringToUrl(string url, Dictionary<string, string> query)
{
var uriBuilder = new UriBuilder(url);
var queryString = HttpUtility.ParseQueryString(uriBuilder.Query);
// Aqui o objeto anônimo seria percorrido e então adicionaria
// um novo parâmetro para a QueryString da URL AO INVÉS do dicionário.
foreach (var q in query)
{
queryString[q.Key] = q.Value;
}
uriBuilder.Query = query.ToString();
return uriBuilder.ToString();
}
And I'm calling it like this:
QueryStringToUrl("http://example.com/?param1=abc", new Dictionary<string, string>
{
{ "param2", "12345" },
{ "otherParam", "huehue" }
});
// URL passada -> http://example.com/?param1=abc
// URL retorno -> http://example.com/?param1=abc¶m2=12345&otherParam=huehue
But I wish it were like this:
QueryStringToUrl("http://example.com/?param1=abc", new
{
param2 = 12345,
otherParam = "huehuehue"
});
// URL passada -> http://example.com/?param1=abc
// URL retorno -> http://example.com/?param1=abc¶m2=12345&otherParam=huehue
Example of similar use with Jil to serialize in JSON:
JSON.Serialize(new
{
param2 = 12345,
otherParam = "huehuehue"
})
Output:
{"param2":"12345","otherParam":"huehuehue"}
I met Jil here . At the bottom of the " Programming Stack " page