Last active
December 27, 2015 08:39
-
-
Save Porges/7298264 to your computer and use it in GitHub Desktop.
part 9 in a series of stupid gists
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.Linq; | |
class Program | |
{ | |
static void Main() | |
{ | |
var s = "hello"; | |
var result = s.Call<string>("Substring", 0, 4); | |
Console.WriteLine(result); | |
/* stupid because */ | |
dynamic s2 = s; | |
string result2 = s2.Substring(0, 4); | |
Console.WriteLine(result2); | |
} | |
} | |
public static class ReflectionExtensions | |
{ | |
public static T Call<T>(this object o, string methodName, params object[] args) | |
{ | |
var methodInfo = o.GetType().GetMethod(methodName, args.Select(x => x.GetType()).ToArray()); | |
return (T)methodInfo.Invoke(o, args); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment