microsoft/teams.net

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v2.0.3

Branches

Tags

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

Clone

HTTPS

Download ZIP

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

59lines · modecode

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