From bc585fb640a3981ac871f063cebac251dd7cc1fc Mon Sep 17 00:00:00 2001 From: luqunl Date: Wed, 27 Jun 2018 10:52:55 -0700 Subject: [PATCH 1/3] Add Public implementation for WinRT EventHandler --- .../System.Private.CoreLib.csproj | 1 + .../WindowsRuntimeMarshalSupport.cs | 21 +++++++++++++++++++ .../EventRegistrationTokenTable.cs | 1 - 3 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 src/System.Private.CoreLib/src/Internal/Runtime/InteropServices/WindowsRuntime/WindowsRuntimeMarshalSupport.cs diff --git a/src/System.Private.CoreLib/System.Private.CoreLib.csproj b/src/System.Private.CoreLib/System.Private.CoreLib.csproj index 9e8d9f7fe732..66a760fe9062 100644 --- a/src/System.Private.CoreLib/System.Private.CoreLib.csproj +++ b/src/System.Private.CoreLib/System.Private.CoreLib.csproj @@ -303,6 +303,7 @@ + diff --git a/src/System.Private.CoreLib/src/Internal/Runtime/InteropServices/WindowsRuntime/WindowsRuntimeMarshalSupport.cs b/src/System.Private.CoreLib/src/Internal/Runtime/InteropServices/WindowsRuntime/WindowsRuntimeMarshalSupport.cs new file mode 100644 index 000000000000..a240969c6713 --- /dev/null +++ b/src/System.Private.CoreLib/src/Internal/Runtime/InteropServices/WindowsRuntime/WindowsRuntimeMarshalSupport.cs @@ -0,0 +1,21 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. +using System; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices.WindowsRuntime; + +namespace Internal.Runtime.InteropServices.WindowsRuntime +{ + public static class WindowsRuntimeMarshalSupport + { + /// + /// Get EventHandler for specified EventRegistrationToken if it exists. + /// An internal contract between System.Private.Corelib and System.Runtime.WindowsRuntime + /// + public static T GetEventHandlerFromEventRegistrationToken(EventRegistrationTokenTable table, EventRegistrationToken token) where T : class + { + return table.ExtractHandler(token); + } + } +} \ No newline at end of file diff --git a/src/System.Private.CoreLib/src/System/Runtime/InteropServices/WindowsRuntime/EventRegistrationTokenTable.cs b/src/System.Private.CoreLib/src/System/Runtime/InteropServices/WindowsRuntime/EventRegistrationTokenTable.cs index 1a210a05cb03..72e165f71392 100644 --- a/src/System.Private.CoreLib/src/System/Runtime/InteropServices/WindowsRuntime/EventRegistrationTokenTable.cs +++ b/src/System.Private.CoreLib/src/System/Runtime/InteropServices/WindowsRuntime/EventRegistrationTokenTable.cs @@ -95,7 +95,6 @@ private EventRegistrationToken AddEventHandlerNoLock(T handler) // Get the delegate associated with an event registration token if it exists. Additionally, // remove the registration from the table at the same time. If the token is not registered, // Extract returns null and does not modify the table. - // [System.Runtime.CompilerServices.FriendAccessAllowed] internal T ExtractHandler(EventRegistrationToken token) { T handler = null; From bb266bada5b2ce50de7739235540726bc34d9f0a Mon Sep 17 00:00:00 2001 From: luqunl Date: Wed, 27 Jun 2018 14:09:09 -0700 Subject: [PATCH 2/3] update according to comments --- .../System.Private.CoreLib.csproj | 1 - .../EventRegistrationTokenTable.cs | 34 +++++++++---------- 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/src/System.Private.CoreLib/System.Private.CoreLib.csproj b/src/System.Private.CoreLib/System.Private.CoreLib.csproj index 66a760fe9062..9e8d9f7fe732 100644 --- a/src/System.Private.CoreLib/System.Private.CoreLib.csproj +++ b/src/System.Private.CoreLib/System.Private.CoreLib.csproj @@ -303,7 +303,6 @@ - diff --git a/src/System.Private.CoreLib/src/System/Runtime/InteropServices/WindowsRuntime/EventRegistrationTokenTable.cs b/src/System.Private.CoreLib/src/System/Runtime/InteropServices/WindowsRuntime/EventRegistrationTokenTable.cs index 72e165f71392..0fb88950391e 100644 --- a/src/System.Private.CoreLib/src/System/Runtime/InteropServices/WindowsRuntime/EventRegistrationTokenTable.cs +++ b/src/System.Private.CoreLib/src/System/Runtime/InteropServices/WindowsRuntime/EventRegistrationTokenTable.cs @@ -92,23 +92,6 @@ private EventRegistrationToken AddEventHandlerNoLock(T handler) return token; } - // Get the delegate associated with an event registration token if it exists. Additionally, - // remove the registration from the table at the same time. If the token is not registered, - // Extract returns null and does not modify the table. - internal T ExtractHandler(EventRegistrationToken token) - { - T handler = null; - lock (m_tokens) - { - if (m_tokens.TryGetValue(token, out handler)) - { - RemoveEventHandlerNoLock(token); - } - } - - return handler; - } - // Generate a token that may be used for a particular event handler. We will frequently be called // upon to look up a token value given only a delegate to start from. Therefore, we want to make // an initial token value that is easily determined using only the delegate instance itself. Although @@ -168,6 +151,23 @@ private static EventRegistrationToken GetPreferredToken(T handler) return new EventRegistrationToken(tokenValue); } + // Remove the event handler from the table and + // Get the delegate associated with an event registration token if it exists + // If the event registration token is not registered, returns false + public bool RemoveEventHandler(EventRegistrationToken token, out T handler) + { + lock (m_tokens) + { + if (m_tokens.TryGetValue(token, out handler)) + { + RemoveEventHandlerNoLock(token); + return true; + } + } + + return false; + } + public void RemoveEventHandler(EventRegistrationToken token) { // The 0 token is assigned to null handlers, so there's nothing to do From dcc6d19f4bed642479862915b6631cb14e334427 Mon Sep 17 00:00:00 2001 From: luqunl Date: Wed, 27 Jun 2018 14:11:05 -0700 Subject: [PATCH 3/3] Remove WindowsRuntimeMarshalSupport --- .../WindowsRuntimeMarshalSupport.cs | 21 ------------------- 1 file changed, 21 deletions(-) delete mode 100644 src/System.Private.CoreLib/src/Internal/Runtime/InteropServices/WindowsRuntime/WindowsRuntimeMarshalSupport.cs diff --git a/src/System.Private.CoreLib/src/Internal/Runtime/InteropServices/WindowsRuntime/WindowsRuntimeMarshalSupport.cs b/src/System.Private.CoreLib/src/Internal/Runtime/InteropServices/WindowsRuntime/WindowsRuntimeMarshalSupport.cs deleted file mode 100644 index a240969c6713..000000000000 --- a/src/System.Private.CoreLib/src/Internal/Runtime/InteropServices/WindowsRuntime/WindowsRuntimeMarshalSupport.cs +++ /dev/null @@ -1,21 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. -using System; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices.WindowsRuntime; - -namespace Internal.Runtime.InteropServices.WindowsRuntime -{ - public static class WindowsRuntimeMarshalSupport - { - /// - /// Get EventHandler for specified EventRegistrationToken if it exists. - /// An internal contract between System.Private.Corelib and System.Runtime.WindowsRuntime - /// - public static T GetEventHandlerFromEventRegistrationToken(EventRegistrationTokenTable table, EventRegistrationToken token) where T : class - { - return table.ExtractHandler(token); - } - } -} \ No newline at end of file