Skip to content

Instantly share code, notes, and snippets.

@JayDouglass
Created July 7, 2013 11:28
Show Gist options
  • Save JayDouglass/5943190 to your computer and use it in GitHub Desktop.
Save JayDouglass/5943190 to your computer and use it in GitHub Desktop.
Invoke any static method in a solution from a console program
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