Created
July 7, 2013 11:28
-
-
Save JayDouglass/5943190 to your computer and use it in GitHub Desktop.
Invoke any static method in a solution from a console program
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 CallMethod | |
{ | |
class Program | |
{ | |
static void Main(string[] args) { | |
Console.WriteLine("Type: {0}, Method: {1}", args[0], args[1]); | |
var type = Type.GetType(args[0]); | |
if(type == null) throw new InvalidOperationException("type not found"); | |
var method = type.GetMethod(args[1]); | |
if (method == null) throw new InvalidOperationException("method not found"); | |
var parameters = args.Skip(2).Select<string, object>(a => | |
{ | |
int @int; | |
if (int.TryParse(a, out @int)) return @int; | |
bool @bool; | |
if (bool.TryParse(a, out @bool)) return @bool; | |
return a; | |
}).ToArray(); | |
method.Invoke(null, parameters); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment