Skip to content

Instantly share code, notes, and snippets.

@kkbruce
Last active January 16, 2017 09:45
Show Gist options
  • Select an option

  • Save kkbruce/950c5554abb32e73ca39489ce6be16c2 to your computer and use it in GitHub Desktop.

Select an option

Save kkbruce/950c5554abb32e73ca39489ce6be16c2 to your computer and use it in GitHub Desktop.
Reflection Method Invoke 3 Ways
// v1. Reflection
// 速度最慢,約 Delegate 的 4 倍
return mi.Invoke(instance, new object[] { });
// v2. Delegate
// 就撰寫難度與執行速度與可讀性,Delegate應列為首選
return mi.CreateDelegate<Func<object>>(instance)();
// v3. Expression
// 速度與 Delegate 相當
var parameter = Expression.Parameter(parameterType, "p");
var call = Expression.Call(Expression.Constant(instance), mi, parameter);
var lambda = Expression.Lambda(call, parameter);
var func = lambda.Compile();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment