Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6684023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:53:26+00:00 2026-05-26T04:53:26+00:00

In the latest video by Rx team Bart De Smet: Rx Update – .NET

  • 0

In the latest video by Rx team Bart De Smet: Rx Update – .NET 4.5, Async, WinRT I saw that WinRT events exposed to .NET by some really strange metadata, more preciesly – add_/remove_ pair methods signature:

EventRegistrationToken add_MyEvent(EventHandler<MyEventArgs> handler) { … }
void remove_MyEvent(EventRegistrationToken registrationToken) { … }

It looks really great, allowing unsubscribing from event by “disposing” the registration token (Rx does the same kind of thing, returning IDisposable instance from Subscribe() method). So it’s became possible to easily unsubscribe lamba-expressions from events, but…

So how does C# allows for working with this kind of events? In .NET it’s possible to subscribe an method (static and instance) with one instance on delegate and unsubscribe with completely another delegate instance pointed to the same method. So if I using an WinRT event and just do unsubscribing of some delegate type instance in C#… where did compiler get the correct EventRegistrationToken? How all this magic works?

— update —

Actually EventRegistrationToken doesn’t allows to unsubscribe simply by calling some kind of Dispose() method, that is really sadly:

public struct EventRegistrationToken
{
    internal ulong Value { get; }
    internal EventRegistrationToken(ulong value)
    public static bool operator ==(EventRegistrationToken left, EventRegistrationToken right)
    public static bool operator !=(EventRegistrationToken left, EventRegistrationToken right)
    public override bool Equals(object obj)
    public override int GetHashCode()
}

— update2 —

WinRT interoperability actually uses global table of registration tokens when subscribing WinRT events with managed objets. For example, interop code for removing handlers looks like this:

internal static void RemoveEventHandler<T>(Action<EventRegistrationToken> removeMethod, T handler)
{
  object target = removeMethod.Target;
  var eventRegistrationTokenTable = WindowsRuntimeMarshal.ManagedEventRegistrationImpl.GetEventRegistrationTokenTable(target, removeMethod);
  EventRegistrationToken obj2;
  lock (eventRegistrationTokenTable)
  {
    List<EventRegistrationToken> list;
    if (!eventRegistrationTokenTable.TryGetValue(handler, out list)) return;
    if (list == null || list.Count == 0) return;
    int index = list.Count - 1;
    obj2 = list[index];
    list.RemoveAt(index);
  }
  removeMethod(obj2);
}

That is really sadly.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T04:53:27+00:00Added an answer on May 26, 2026 at 4:53 am

    When you add or remove a delegate to an WinRT event, like this:

    this.Loaded += MainPage_Loaded;
    
    this.Loaded -= MainPage_Loaded;
    

    It looks just like you were working with normal .Net events. But this code actually compiles to something like this (Reflector seems to have some trouble decompiling WinRT code, but I think this is what the code actually does):

    WindowsRuntimeMarshal.AddEventHandler<RoutedEventHandler>(
        new Func<RoutedEventHandler, EventRegistrationToken>(this.add_Loaded),
        new Action<EventRegistrationToken>(remove_Loaded),
        new RoutedEventHandler(this.MainPage_Loaded));
    
    WindowsRuntimeMarshal.RemoveEventHandler<RoutedEventHandler>(
        new Action<EventRegistrationToken>(this.remove_Loaded),
        new RoutedEventHandler(this.MainPage_Loaded));
    

    This code won’t actually compile, because you can’t access the add_ and remove_ methods from C#. But you can that in IL, and that’s exactly what the compiler does.

    It looks like WindosRuntimeMarshal keeps all those EventRegistrationTokens and uses them to unsubscribe when necessary.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the latest version of the Boost library that is compatible with Microsoft
I downloaded the latest video capture samples from WWDC 2010 and am trying to
i have a flash video called abc.swf that i would like to play when
After watching the latest hanselminutes on 9 video, I went to our dev environment
I tried to implement html5 <video> tag in asp.net 2010 web form (ex: http://localhost/SampleWebsite/test.aspx
I have project that relies heavily on video and leverages html5 video when available.
Does the latest JavaFx version support streaming webcam video? Is it possible to get
I have a stored procedure that will give the latest records i.e., order by
I am looking to returning the video-thumbnail of the latest uploaded video from my
I'm following the Migration video posted at http://subsonicproject.com/docs/Using_SimpleRepository I've downloaded the latest SubSonic (3.0.0.4),

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.