I wrote an extension method that takes a custom object and converts its properties to a query string for use in a URL. You can specify what properties you want included by using a [QueryString]
attribute. For example:
class Thing
{
[QueryString]
public int Id {get; set;}
[QueryString]
public string Name {get; set;}
public object MetaData {get; set;}
}
var thing = new Thing { Id = 1, Name = "Test", MetaData = someObj };
thing.ToQueryString(); // outputs `?Id=1&Name=Test
Here's the code for the extension method:
public static string ToQueryString(this object obj)
{
StringBuilder queryStringBuilder = new StringBuilder("?");
Type objType = obj.GetType();
// Get properties marked with `[QueryString]`
List<PropertyInfo> props = objType.GetProperties()
.Where(p => Attribute.IsDefined(p, typeof(QueryStringAttribute)))
.ToList();
foreach (var prop in props)
{
string name = prop.Name;
var value = prop.GetValue(obj, null);
if (value != null)
{
// Check's if this is the last property, if so, don't add an '&'
if (props.IndexOf(prop) != (props.Count - 1))
{
queryStringBuilder.Append($"{Uri.EscapeDataString(name)}={Uri.EscapeDataString(value.ToString())}&");
}
else
{
queryStringBuilder.Append($"{Uri.EscapeDataString(name)}={Uri.EscapeDataString(value.ToString())}");
}
}
}
return queryStringBuilder.ToString();
}
The code works just fine, but it seems a bit messy to me. Specifically, checking if we are on the last property seems a bit messy, because we end up doing the same thing, just nixing one character. Is there a way to make that a little cleaner? Are there any other way's this method could be simplified?