2

This question is very similar to a previous post: convert-object-to-url-in-c-sharp

I am trying to convert an object to a URL string.

For Example:

public class example {
    public string property1;
    public int property2;
    public double property3;

}

Then the string will come out like

property1=value&property2=value&property3=value

Ideally I would like to not do this with a fancy loop and string manipulation. (Not Lazy, just efficient).

It is for a set class library's, end goal is to work with a Win Forms application, connecting to a third party website which doesn't receive JSON objects. I would like to stay away from using MVC framework stuff.

Community
  • 1
  • 1
Malcor
  • 2,667
  • 21
  • 29
  • 1
    Show or tell us what have you tried and what concrete problem do you have as SO is not "write me some code" portal. – Konrad Kokosa Dec 23 '13 at 14:17
  • 1
    you can use reflection for it, but it can be slow. and in case if you need to ignore some properties, then it will complicate things.. – Sergey Litvinov Dec 23 '13 at 14:18
  • I have tried the other SO post I mention in the question, But tbhis requires having MVC references which I am trying to avoid. I have tried serializing to a JSON object then replacing brackets and quotes. I am trying to find a more elegant way. I am not asking anyone to write code for me, just pointer to maybe a particular method. – Malcor Dec 23 '13 at 14:22
  • @mart I particularly like the fact that the accepted answer to the other question uses linq, thus avoiding "a fancy loop" (or at least hiding it from the OP ;) – David Arno Dec 23 '13 at 14:23
  • 1
    @Malcor No, the accepted answer to the question you have duplicated references `HttpUtility.UrlEncode`, but you don't need that and so you don't need refs to MVC. Rather than expecting to be spoon-fed an exact answer for your specific needs, read that answer, understand it and adjust to your needs. – David Arno Dec 23 '13 at 14:25
  • @Malcor The proposed duplicate relies only on `System.Web` namespace which isn't much. – BartoszKP Dec 23 '13 at 14:26
  • @DavidArno Im not expecting to be spoonfed I did not see the duplicated post, the one I referenced in the my original post requires UrlHelper, which clicking on the link in the post said its namespace is System.Web.Mvc. – Malcor Dec 23 '13 at 14:30

3 Answers3

6

Try the following:

var obj = new example { ... };
var result = new List<string>();
foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(obj))
{
    result.Add(property.Name + "=" + property.GetValue(obj));
}

return string.Join("&", result);
Leonid Vasilev
  • 11,910
  • 4
  • 36
  • 50
Dmytro Rudenko
  • 2,524
  • 2
  • 13
  • 22
2

You can use reflection and some linq:

void Main()
{
    var e = new example
    {
        property1 = "a",
        property2 = 42,
        property3 = 100
    };

    string s = string.Join("&", e.GetType().GetProperties().Select(p => p.Name + "=" + p.GetValue(e, null)));
    // s= property1=a&property2=42&property3=100
}

public class example {
    public string property1 {get;set;}
    public int property2 {get;set;}
    public double property3 {get;set;}
}
Stef Heyenrath
  • 9,335
  • 12
  • 66
  • 121
1

Why not just do it in a straightforward manner?

Add an extension method to your Example class (btw the standard .NET naming convention for classes and properties is to use Pascal casing):

public string ToUrlString(this Example example)
{
    return "property1=" +  HttpServerUtility.UrlEncode(example.Property1)
       + "&property2=" + HttpServerUtility.UrlEncode(example.Property2.ToString())
       + "&property3=" + HttpServerUtility.UrlEncode(example.Property3.ToString());
}
Roy Dictus
  • 32,551
  • 8
  • 60
  • 76