How to pass and traverse an anonymous object in a method

Asked

Viewed 279 times

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&param2=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&param2=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"}

Jil at Github:

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.

  • Explain the goal to see a better solution.

  • 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 =/

1 answer

1


It is possible using reflection, but not recommend, saves typing, but lose speed and can create maintenance difficulties. Jil does it, but more performatively.

Note that the example where it was used has a generic method.

I’d rather use (param2 : 12345, otherParam : "huehuehue"). But creating a type is better. It should also work with a type dynamic, albeit rarely is it necessary, even though people think it is, but what should have better performance is the Dictionary even.

The following code works for everyone.

using static System.Console;

public class Program {
    public static void Main() => Teste(new { param2 = 12345, otherParam = "huehuehue" });
    public static void Teste<T>(T param){
        foreach (var item in typeof(T).GetProperties()) WriteLine($"{item.Name} : {item.GetValue(param, null)}");
    }
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

  • Thank you very much :D, I was very curious kkkkkkk. I will use even dictionary, I like performance, although I believe I do a lot of things no little performatic, thinking I’m doing kkkk .--.

  • Recommends that I also use a dictionary in Jil?

  • I don’t know enough technology to give an opinion

Browser other questions tagged

You are not signed in. Login or sign up in order to post.