Created
March 23, 2017 15:35
-
-
Save Rottweiler/3593730515988897d08f9532e19dc2ae to your computer and use it in GitHub Desktop.
Call method by name
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 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