Created
September 29, 2019 22:15
-
-
Save candidodmv/00e6435d730fa73f8b91db2bb210b0a5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Newtonsoft.Json; | |
using System; | |
using System.Collections.Generic; | |
using Flurl; | |
namespace ConsoleApp1 | |
{ | |
public class Launcher | |
{ | |
public Launcher() | |
{ | |
AdditionalParameters = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase); | |
} | |
public string ActionName { get; set; } | |
[JsonConverter(typeof(DictionaryConverter))] | |
public Dictionary<string, object> AdditionalParameters { get; set; } | |
public bool TryGetValue<T>(string key, out T value) | |
{ | |
value = default; | |
try | |
{ | |
var status = AdditionalParameters.TryGetValue(key, out var extracted); | |
if (status && CanChangeType(extracted, typeof(T))) | |
value = (T)Convert.ChangeType(extracted, typeof(T)); | |
else | |
return false; | |
return status; | |
} | |
catch (Exception) | |
{ | |
return false; | |
} | |
} | |
public string AbsolutePath => $"{ActionName}".SetQueryParams(AdditionalParameters); | |
public static bool CanChangeType(object value, Type conversionType) => (conversionType == null || value == null) ? false : value is IConvertible; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment