1
How can I pass an anonymous object to a method, and then traverse it?
What I’m trying to do is this:
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 that:
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 so:
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"
})
Exit:
{"param2":"12345","otherParam":"huehuehue"}
I met Jil here. At the bottom of the page "Programming Stack"
It’s impossible, C# is a rigid typing language. If you really need this kind of thing you should work with another language.
– Jéf Bueno
Explain the goal to see a better solution.
– Maniero
kkkkkk, sorry I used to serialize in JSON with Jil (I added the question), I thought the code looked beautiful and easier to understand, then I thought I could do it too and that there was no problem =/
– user45722