Created
September 8, 2015 14:31
-
-
Save migueldeicaza/cd99938c2a4372e7e5d5 to your computer and use it in GitHub Desktop.
Probing for events having a single callback on .NET and Mono
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
const BindingFlags SEARCH_FLAGS = BindingFlags.NonPublic | BindingFlags.Instance; | |
var hasSingleTargetMethod = typeof(MulticastDelegate).GetMethod("InvocationListLogicallyNull", SEARCH_FLAGS) ?? | |
typeof(MulticastDelegate).GetMethod("get_HasSingleTarget", SEARCH_FLAGS); | |
if (hasSingleTargetMethod != null) | |
HasSingleTarget = (HasSingleTargetFn)Delegate.CreateDelegate(typeof(HasSingleTargetFn), hasSingleTargetMethod, true); | |
else // gracefull degradation | |
HasSingleTarget = md => md.GetInvocationList().Length == 1; | |
// ... | |
static void QueueContinuation(WaitCallback continuation) | |
{ | |
if (!HasSingleTarget(continuation)) | |
{ | |
foreach (WaitCallback act in continuation.GetInvocationList()) | |
QueueContinuation(act); | |
return; | |
} | |
ThreadPool.QueueUserWorkItem(continuation); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment