-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathExtensionMethods.cs
More file actions
85 lines (71 loc) · 2.72 KB
/
ExtensionMethods.cs
File metadata and controls
85 lines (71 loc) · 2.72 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
using System;
using System.Collections.Generic;
using System.Text;
namespace LiveSDKHelper
{
public static class ExtensionMethods
{
/// <summary>
/// Creates a string from the sequence by concatenating the result
/// of the specified string selector function for each element.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="source">The source.</param>
/// <param name="stringSelector">The string selector.</param>
/// <param name="separator">
/// The string which separates each concatenated item.
/// Optional; default is string.Empty
/// </param>
/// <returns></returns>
public static string ToConcatenatedString<T>(this IEnumerable<T> source,
Func<T, string> stringSelector,
string separator = "")
{
var b = new StringBuilder();
foreach (var item in source)
{
b.Append(stringSelector(item) + separator);
}
return b.ToString().Trim(separator.ToCharArray());
}
public static string ToStringScope(this Scope scope)
{
var scopeName = scope.GetAttribute<ScopeNameAttribute>();
return scopeName == null ? string.Empty : scopeName.ScopeName;
}
internal static UriBuilder SetQueryParam(this UriBuilder uri, string key, string value)
{
var collection = uri.ParseQuery();
// add (or replace existing) key-value pair
collection[key] = value;
var query = collection
.ToConcatenatedString(pair =>
pair.Key == null
? pair.Value
: pair.Key + "=" + pair.Value, "&");
uri.Query = query;
return uri;
}
internal static IEnumerable<KeyValuePair<string, string>> GetQueryParams(
this UriBuilder uri)
{
return uri.ParseQuery();
}
internal static Dictionary<string, string> ParseQuery(this UriBuilder uri)
{
var nameValueCollection = new Dictionary<string, string>();
string[] items = uri.Query.Split('&');
foreach (string item in items)
{
if (item.Contains("="))
{
string[] nameValue = item.Split('=');
if (nameValue[0].Contains("?"))
nameValue[0] = nameValue[0].Replace("?", "");
nameValueCollection.Add(nameValue[0], (nameValue[1]));
}
}
return nameValueCollection;
}
}
}