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.

7
  • 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. Commented Dec 23, 2013 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.. Commented Dec 23, 2013 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. Commented Dec 23, 2013 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 ;) Commented Dec 23, 2013 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. Commented Dec 23, 2013 at 14:25

3 Answers 3

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);
Sign up to request clarification or add additional context in comments.

Comments

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;}
}

Comments

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());
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.