Optimize ToString() method override

This commit is contained in:
弘韬 张 2021-02-14 12:02:59 +08:00
parent f93dcaef73
commit aabb820f6f
2 changed files with 13 additions and 24 deletions

View file

@ -58,13 +58,12 @@ namespace Flow.Launcher.Core.Plugin
string rpc = string.Empty; string rpc = string.Empty;
if (Parameters != null && Parameters.Length > 0) if (Parameters != null && Parameters.Length > 0)
{ {
string parameters = Parameters.Aggregate("[", (current, o) => current + (GetParameterByType(o) + ",")); string parameters = $"[{string.Join(',', Parameters.Select(GetParameterByType))}]";
parameters = parameters.Substring(0, parameters.Length - 1) + "]"; rpc = $@"{{\""method\"":\""{Method}\"",\""parameters\"":{parameters}";
rpc = string.Format(@"{{\""method\"":\""{0}\"",\""parameters\"":{1}", Method, parameters);
} }
else else
{ {
rpc = string.Format(@"{{\""method\"":\""{0}\"",\""parameters\"":[]", Method); rpc = $@"{{\""method\"":\""{Method}\"",\""parameters\"":[]";
} }
return rpc; return rpc;
@ -72,26 +71,16 @@ namespace Flow.Launcher.Core.Plugin
} }
private string GetParameterByType(object parameter) private string GetParameterByType(object parameter)
=> parameter switch
{ {
if (parameter == null) { null => "null",
return "null"; string _ => $@"\""{ReplaceEscapes(parameter.ToString())}\""",
} bool _ => $@"{parameter.ToString().ToLower()}",
if (parameter is string) _ => parameter.ToString()
{ };
return string.Format(@"\""{0}\""", ReplaceEscapes(parameter.ToString()));
}
if (parameter is int || parameter is float || parameter is double)
{
return string.Format(@"{0}", parameter);
}
if (parameter is bool)
{
return string.Format(@"{0}", parameter.ToString().ToLower());
}
return parameter.ToString();
}
private string ReplaceEscapes(string str)
private string ReplaceEscapes(string str)
{ {
return str.Replace(@"\", @"\\") //Escapes in ProcessStartInfo return str.Replace(@"\", @"\\") //Escapes in ProcessStartInfo
.Replace(@"\", @"\\") //Escapes itself when passed to client .Replace(@"\", @"\\") //Escapes itself when passed to client

View file

@ -119,7 +119,7 @@ namespace Flow.Launcher.Core.Plugin
/// <param name="arguments"></param> /// <param name="arguments"></param>
/// <param name="token">Cancellation Token</param> /// <param name="token">Cancellation Token</param>
/// <returns></returns> /// <returns></returns>
protected async Task<string> ExecuteAsync(string fileName, string arguments, CancellationToken token = default) protected Task<string> ExecuteAsync(string fileName, string arguments, CancellationToken token = default)
{ {
ProcessStartInfo start = new ProcessStartInfo(); ProcessStartInfo start = new ProcessStartInfo();
start.FileName = fileName; start.FileName = fileName;
@ -128,7 +128,7 @@ namespace Flow.Launcher.Core.Plugin
start.CreateNoWindow = true; start.CreateNoWindow = true;
start.RedirectStandardOutput = true; start.RedirectStandardOutput = true;
start.RedirectStandardError = true; start.RedirectStandardError = true;
return await ExecuteAsync(start, token); return ExecuteAsync(start, token);
} }
protected string Execute(ProcessStartInfo startInfo) protected string Execute(ProcessStartInfo startInfo)