Skip to content

Instantly share code, notes, and snippets.

@Rottweiler
Created March 23, 2017 15:35
Show Gist options
  • Save Rottweiler/3593730515988897d08f9532e19dc2ae to your computer and use it in GitHub Desktop.
Save Rottweiler/3593730515988897d08f9532e19dc2ae to your computer and use it in GitHub Desktop.
Call method by name
using System;
using System.Text;
using System.Reflection;
using System.Linq;
class Program
{
public static void Main(string[] argv)
{
WriteLine("Hi");
}
public static void WriteLine(string msg)
{
CallReflect(typeof(Console), "WriteLine", BindingFlags.Public | BindingFlags.Static, msg);
}
public static object CallReflect(Type @class, string name, BindingFlags scope, params object[] argv)
{
Type[] argt = Array.ConvertAll(argv, item => item.GetType());
var method = @class.GetMethod(name, scope, null /* defaultbinder */, CallingConventions.Any, argt, null /* parametermodifier for COM interop */);
return method.Invoke(null, argv) as object;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment