Last active
January 16, 2017 09:45
-
-
Save kkbruce/950c5554abb32e73ca39489ce6be16c2 to your computer and use it in GitHub Desktop.
Reflection Method Invoke 3 Ways
This file contains hidden or 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
| // 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