Skip to content

Instantly share code, notes, and snippets.

@jpd21122012
Created May 27, 2025 18:43
Show Gist options
  • Save jpd21122012/a97ca3ef5fa53728318bf3f0a25276ed to your computer and use it in GitHub Desktop.
Save jpd21122012/a97ca3ef5fa53728318bf3f0a25276ed to your computer and use it in GitHub Desktop.
// 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