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 8557955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:41:40+00:00 2026-06-11T15:41:40+00:00

I have the following code working in Silverlight 5: public void Send(Notification notification) {

  • 0

I have the following code working in Silverlight 5:

public void Send(Notification notification)
{
    // Because the variable is passed as Notification, we have to trick the
    // type-inference feature of the runtime so the message will be sent using
    // the actual type of the object.
    // Using the dynamic keyword, we can convert the type of the object passed
    // to the Send method.
    // This allows subscribers to register for messages using the generic interface.
    dynamic stronglyTypedNotification = Convert.ChangeType(notification,
                                                           notification.GetType(),
                                                           CultureInfo.InvariantCulture);

    SendCore(stronglyTypedNotification);
}

private void SendCore<T>(T notification)
    where T : Notification
{
    foreach (var handlerFactory in Handlers)
    {
        var handler = handlerFactory.Value as INotificationHandler<T>;

        if (handler != null)
        {
            handler.Handle(notification);
        }
    }
}

I have to port this code over to run in a WPF application.

When I run it in my WPF application and set a breakpoint inside the SendCore method, T is not the correct type. I can only assume this is because generics are supposed to be staticly defined so the compiler has created the version of SendCore it thinks it will need at runtime. I guess Silverlight handles this differently because this code works perfectly in SL.

The goal of this code is to locate any of the objects contained in the Handlers collection that implement INotificationHandler where T is the type of the object passed to the Send method (subclassing the base Notification class) then call the Handle(T n) method on those objects.

How can I do this in my WPF app?

UPDATE

After some additional testing, I am finding more peculiar results. When I set a breakpoint on the first line of the SendCore method and inspect T and notification, I find:

  • Intellisense indicates that T is “GenericNotification” which is
    legitimate class in the assembly.
  • Intellisense indicates that “notification” is the correct,
    strongly-typed class (DataChangedNotification).
  • DataChangedNotification does NOT subclass GenericNotification in any
    way. Both subclass Notification.
  • Because T is resolving to GenericNotification, attempting to cast
    each handler to INotificationHandler<T> will fail because none
    implement INotificationHandler<GenericNotification>

Given that this exact code works in Silverlight, a Console application and another WPF application (as well as David’s test in LINQpad), what in the world could be going on?

The only thing I can think of mentioning is that the code actually exists in a Class Library that is referenced by the WPF application. Not sure that matters because I tested that scenario in another (new) WPF application and had the correct results.

  • 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-06-11T15:41:41+00:00Added an answer on June 11, 2026 at 3:41 pm

    It works for me and that’s expected. I would have been surprised if it was different.

    void Main()
    {
        Send(new NA());
        Send(new NB());
    }
    
    public class Notification {}
    public class NA : Notification {}
    public class NB : Notification {}
    
    public void Send(Notification notification)
    {
        dynamic stronglyTypedNotification
            = Convert.ChangeType(notification,
                                 notification.GetType(),
                                 CultureInfo.InvariantCulture);
    
        SendCore(stronglyTypedNotification);
    }
    
    public void SendCore<T>(T notification) where T : Notification
    {
        Console.WriteLine(typeof(T));
    }
    

    This outputs

    typeof (NA) 
    typeof (NB)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Right now I have the following code working: @UiHandler(usernameTextBox) void onUsernameTextBoxKeyPress(KeyPressEvent event) { keyPress(event);
I have following code and it is working fine. public partial class MainWindow :
In YUI I have following code working for mouse wheel. How do I make
We have the following code working for a complex rails form with checkboxes. I'm
On a Solaris 5.8 machine, I have the following code: [non-working code] char *buf;
I have following code And it is working fine in AIR device simulator on
I have the following code which is working, I was wondering if this can
I have the following code that is working fine in jsfiddle - http://jsfiddle.net/darkajax/FHZBy/ I've
I am working with NSCache in iOS i have following code in .h file
I have the following code and it's working (as usual) in everything but IE.

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.