-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueryHandler.cs
42 lines (35 loc) · 1.28 KB
/
QueryHandler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
using System;
using System.Collections.Generic;
using System.Reflection;
namespace Modsi.QueryAttribute
{
public static class QueryHandler
{
public static string BuildQuery<T>(T request)
{
Type type = request.GetType();
PropertyInfo[] properties = type.GetProperties();
Dictionary<string, string> queryParameters = new Dictionary<string, string>();
string queryString = "?";
foreach (PropertyInfo property in properties)
{
QueryAttribute queryAtrributes = property.GetCustomAttribute<QueryAttribute>();
if (queryAtrributes != null)
{
string name = property.Name;
string value = property.GetValue(request)?.ToString();
if (value != null && value != queryAtrributes.NullValue)
{
queryParameters.Add(queryAtrributes.Name, value);
}
}
}
foreach (var param in queryParameters)
{
queryString += param.Key + "=" + param.Value + "&";
}
queryString = queryString.Remove(queryString.Length - 1, 1);
return queryString;
}
}
}