Skip to content

Instantly share code, notes, and snippets.

@Porges
Last active December 27, 2015 08:39
Show Gist options
  • Save Porges/7298264 to your computer and use it in GitHub Desktop.
Save Porges/7298264 to your computer and use it in GitHub Desktop.
part 9 in a series of stupid gists
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