Skip to content

Instantly share code, notes, and snippets.

@juanplopes
Created January 11, 2011 15:50
Show Gist options
  • Save juanplopes/774594 to your computer and use it in GitHub Desktop.
Save juanplopes/774594 to your computer and use it in GitHub Desktop.
var foo = new Foo();
var bar = typeof(Foo).GetMethod("Bar");
var param = Expression.Parameter(typeof(int));
var bar2 = Expression.Lambda<Func<int, string>>(
Expression.Call(Expression.Constant(foo), bar, param), param).Compile();
var s = Stopwatch.StartNew();
for (int i = 0; i < 5000000; i++)
foo.Bar(i);
Console.WriteLine("Sem reflection: {0}", s.Elapsed);
s.Restart();
for (int i = 0; i < 5000000; i++)
bar2.Invoke(i);
Console.WriteLine("Com reflection: {0}", s.Elapsed);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment