Last active
March 11, 2017 21:15
-
-
Save sontx/e89ec85d1eb565397e94a9e00f0016ea to your computer and use it in GitHub Desktop.
Execute an executable file with complex arguments
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 Newtonsoft.Json.Linq; | |
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
namespace Executor | |
{ | |
public sealed class ArgumentDeserializer | |
{ | |
private IList<JObject> arguments { get; set; } | |
private readonly string[] commandLineArguments = null; | |
public ArgumentDeserializer(string[] arguments) | |
{ | |
this.commandLineArguments = arguments; | |
} | |
public ArgumentDeserializer() | |
{ | |
this.commandLineArguments = Environment.GetCommandLineArgs(); | |
} | |
public bool Deserialize() | |
{ | |
if (commandLineArguments.Length != 2) | |
return false; | |
string tempFile = commandLineArguments[1]; | |
if (!File.Exists(tempFile)) | |
return false; | |
try | |
{ | |
DeserializeArguments(tempFile); | |
} | |
catch | |
{ | |
return false; | |
} | |
File.Delete(tempFile); | |
return true; | |
} | |
private void DeserializeArguments(string tempFile) | |
{ | |
string json = File.ReadAllText(tempFile); | |
arguments = JsonConvert.DeserializeObject<IList<JObject>>(json); | |
} | |
public T GetArgument<T>(int index) | |
{ | |
return arguments[index].ToObject<ObjectWrapper<T>>().Value; | |
} | |
public T GetArgument<T>(string key) | |
{ | |
JObject argument = arguments.Single((arg) => { return arg.ToObject<ObjectWrapper<object>>().Key == key; }); | |
return argument.ToObject<ObjectWrapper<T>>().Value; | |
} | |
} | |
} |
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
var deserializer = new ArgumentDeserializer(); | |
if (deserializer.Deserialize()) | |
{ | |
var person = deserializer.GetArgument<Person>(0); | |
var future = deserializer.GetArgument<Future>(1); | |
var world = deserializer.GetArgument<World>("key3"); | |
var and = deserializer.GetArgument<And>("key4"); | |
var love = deserializer.GetArgument<Love>(4); | |
var myString = deserializer.GetArgument<string>(5); | |
var myCode = deserializer.GetArgument<int>(6); | |
} |
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
var executor = new ProcessExecutor("awesome-program.exe"); | |
executor.Add("key1", new Person()); | |
executor.Add("key2", new Future()); | |
executor.Add("key3", new World()); | |
executor.Add("key4", new And()); | |
executor.Add("key5", new Love()); | |
executor.Add("don't care about the above lines"); | |
executor.Add(0x3393); | |
executor.Execute(); |
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
namespace Executor | |
{ | |
internal class ObjectWrapper<T> | |
{ | |
public T Value { get; set; } | |
public string Key { get; set; } | |
} | |
} |
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.Collections.Generic; | |
using System.IO; | |
using System; | |
using System.Linq; | |
using System.Diagnostics; | |
namespace Executor | |
{ | |
public sealed class ProcessExecutor | |
{ | |
public string FileName { get; set; } | |
private IList<ObjectWrapper<object>> objectWrapperArguments = new List<ObjectWrapper<object>>(); | |
public ProcessExecutor(string fileName) | |
{ | |
this.FileName = fileName; | |
} | |
public ProcessExecutor() | |
{ | |
} | |
public void Add(object argument) | |
{ | |
objectWrapperArguments.Add(new ObjectWrapper<object>() { Value = argument, Key = GenerateKey() }); | |
} | |
public void Add(string key, object argument) | |
{ | |
if (objectWrapperArguments.Any((arg) => { return arg.Key == key; })) | |
throw new ArgumentException("Key is Duplicated."); | |
objectWrapperArguments.Add(new ObjectWrapper<object>() { Value = argument, Key = key }); | |
} | |
public void Execute() | |
{ | |
var tempFile = SerializeArgumentsToFile(); | |
Process.Start(FileName, "\"" + tempFile + "\""); | |
} | |
private string GenerateKey() | |
{ | |
return Guid.NewGuid().ToString(); | |
} | |
private string SerializeArgumentsToFile() | |
{ | |
string json = JsonConvert.SerializeObject(objectWrapperArguments); | |
string tempFile = Path.GetTempFileName(); | |
File.WriteAllText(tempFile, json); | |
return tempFile; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment