microsoft/teams.net

Public

mirrored fromhttps://github.com/microsoft/teams.netAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
aacebo/mcp-client

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

Libraries/Microsoft.Teams.Common/Http/QueryString.cs

56lines · modecode

1using System.Reflection;
2using System.Text;
3using System.Text.Json.Serialization;
4using System.Web;
5
6namespace Microsoft.Teams.Common.Http;
7
8public static class QueryString
9{
10 public static string Serialize(object value)
11 {
12 var properties = value.GetType().GetProperties();
13 var parts = new List<string>();
14
15 foreach (var property in properties)
16 {
17 if (property.PropertyType == typeof(IList<string>))
18 {
19 SerializeIListString(value, property, parts);
20 continue;
21 }
22 var builder = new StringBuilder();
23 var jsonAttribute = property.GetCustomAttribute<JsonPropertyNameAttribute>();
24 var name = jsonAttribute?.Name ?? property.Name;
25
26 builder.Append(HttpUtility.UrlEncode(name));
27 builder.Append('=');
28 builder.Append(HttpUtility.UrlEncode(property.GetValue(value, null)?.ToString()));
29 parts.Add(builder.ToString());
30 }
31
32 return string.Join("&", parts);
33 }
34
35 private static void SerializeIListString(object value, PropertyInfo property, List<string> parts)
36 {
37 var jsonAttributeList = property.GetCustomAttribute<JsonPropertyNameAttribute>();
38 var nameList = jsonAttributeList?.Name ?? property.Name;
39 var listObject = property.GetValue(value, null) as IList<string>;
40 if (listObject != null)
41 {
42 for (int i = 0; i < listObject.Count; i++)
43 {
44 if (listObject[i] != null)
45 {
46 var builder = new StringBuilder();
47 builder.Append(HttpUtility.UrlEncode(nameList));
48 builder.Append(HttpUtility.UrlEncode($"[{i}]"));
49 builder.Append('=');
50 builder.Append(HttpUtility.UrlEncode(listObject[i]));
51 parts.Add(builder.ToString());
52 }
53 }
54 }
55 }
56}