Created
May 27, 2025 18:43
-
-
Save jpd21122012/a97ca3ef5fa53728318bf3f0a25276ed to your computer and use it in GitHub Desktop.
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
// Cache the delegate | |
private static Func<MyClass, int, int, int> _cachedDelegate; | |
static void BuildCache() | |
{ | |
MethodInfo method = typeof(MyClass).GetMethod("Calculate"); | |
_cachedDelegate = (Func<MyClass, int, int, int>)Delegate | |
.CreateDelegate(typeof(Func<MyClass, int, int, int>), method); | |
} | |
// Invoke (much faster) | |
int result = _cachedDelegate(instance, 5, 10); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment