You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
78 lines
1.9 KiB
78 lines
1.9 KiB
<Query Kind="Program"> |
|
<Namespace>System.Net</Namespace> |
|
</Query> |
|
|
|
void Main() |
|
{ |
|
string pattern = @"\{([A-Za-z]+)\}"; |
|
|
|
Regex rgx = new Regex(pattern); |
|
|
|
string source = @"/_matrix/client/r0/user/{userId}/rooms/{roomId}/account_data/{type}"; |
|
|
|
MatrixApiEntities entities = new MatrixApiEntities() |
|
{ |
|
UserId = "claire", |
|
RoomId = "boobs", |
|
Type = "private", |
|
Tags = "dummy" |
|
}; |
|
|
|
foreach (Match match in rgx.Matches(source)) |
|
{ |
|
string replace = match.Value; |
|
string prop = UpperFirst(match.Groups[1].Value); |
|
|
|
// check if property exists in entity list |
|
try |
|
{ |
|
// get value of property through GetType().GetProperty().GetValue() |
|
string entityValue = entities.GetType().GetProperty(prop).GetValue(entities).ToString(); |
|
// return URL with entities replaced and URL-encoded |
|
source = WebUtility.UrlEncode(source.Replace(replace,entityValue)); |
|
} |
|
catch |
|
{ |
|
// shit broke |
|
} |
|
|
|
} |
|
|
|
Console.WriteLine(source); |
|
} |
|
|
|
public string UpperFirst(string s) |
|
{ |
|
// Check for empty string. |
|
if (string.IsNullOrEmpty(s)) |
|
{ |
|
return string.Empty; |
|
} |
|
// Return char and concat substring. |
|
return char.ToUpper(s[0]) + s.Substring(1); |
|
} |
|
|
|
// Define other methods and classes here |
|
public class MatrixApiEntities |
|
{ |
|
public string DeviceId { get; set; } |
|
public string EventId { get; set; } |
|
public string EventType { get; set; } |
|
public string FileName { get; set; } |
|
public string FilterId { get; set; } |
|
public string Kind { get; set; } |
|
public string MediaId { get; set; } |
|
public string NetworkId { get; set; } |
|
public string ReceiptType { get; set; } |
|
public string RoomAlias { get; set; } |
|
public string RoomId { get; set; } |
|
public string RoomIdOrAlias { get; set; } |
|
public string RuleId { get; set; } |
|
public string Scope { get; set; } |
|
public string ServerName { get; set; } |
|
public string StateKey { get; set; } |
|
public string Tags { get; set; } |
|
public string TxnId { get; set; } |
|
public string Type { get; set; } |
|
public string UserId { get; set; } |
|
} |