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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T21:26:15+00:00 2026-05-10T21:26:15+00:00

Suppose I want to create a set of observers based on type. That is

  • 0

Suppose I want to create a set of observers based on type. That is to say, when they are notified of an event, they are told the type of one of the arguments and then decides whether or not to act based on if it can operate on that type.

Are there any simple ways to do this? I figured this would be fairly simple to do with generics, but that seems to be turning out to be harder than I imagined. And I would prefer not to have to deal with casting a bunch of references to object if I can avoid it.

Where I’m getting stuck is in doing this:

public delegate void NotifyDelegate<T>(IEnumerator<T> loadable, NotifyArgs na);  interface IObserver {     void Notify<T>(IEnumerator<T> loadable, NotifyArgs na); }  class Subject {     NotifyDelegate notifier;  //won't compile:  needs type args      void Register(IObserver o)     {         notifier += o.Notify;     } } 

Of course, I could make the Subject generic as well, but then I have to have a separate Subject for each type. Does anyone have any advice here? Is there some piece of functionality that I’m missing somewhere or am I overcomplicating this?

UPDATE: I did oversimplify the arguments that Notify and NotifyDelegate take. Instead of this:

public delegate void NotifyDelegate<T>(NotifyArgs na); 

I’m actually wanting to do something like this:

public delegate void NotifyDelegate<T>(IEnumerator<T> loadable, NotifyArgs na); 

What I’m basically trying to pass back and forth is data from a database. Sorry if the previous code sample confused anyone.

  • 1 1 Answer
  • 1 View
  • 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. 2026-05-10T21:26:16+00:00Added an answer on May 10, 2026 at 9:26 pm

    First off, change the code you’ve got to the following:

    interface IObserver { }  class Subject {   public Subject ()   {     m_observers = new List<IObserver> ();   }    public void Register (IObserver o)   {     m_observers.Add (o);   }    List<IObserver>     m_observers; } 

    Then, use reflection to find an appropriate function based on parameter type:

      public void NotifyObservers (object param)   {     foreach (IObserver observer in m_observers)     {       foreach (MethodInfo method in observer.GetType ().GetMethods (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy | BindingFlags.Instance))       {         if (method.Name == 'Notify')         {           ParameterInfo []             parameters = method.GetParameters ();            if (parameters.Length == 1 && parameters [0].ParameterType == param.GetType ())           {             method.Invoke (observer, new object [] { param });             break;           }         }       }     }   } 

    and use it like this:

    class Observer : IObserver {   public Observer (Subject s)   {     s.Register (this);   }    void Notify (float value)   {     System.Diagnostics.Trace.WriteLine ('float value = ' + value);   }    void Notify (int value)   {     System.Diagnostics.Trace.WriteLine ('int value = ' + value);   } }  static void Main (string [] args) {   Subject     s = new Subject ();    Observer     o = new Observer (s);    float     v1 = 3.14f;    int     v2 = 42;    System.Diagnostics.Trace.WriteLine ('sending float');   s.NotifyObservers (v1);   System.Diagnostics.Trace.WriteLine ('sending int');   s.NotifyObservers (v2); } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I want to create trees of a certain set depth, that is, the
Suppose I want to put objects that identify a server into a stl set
Suppose you want to create a parent-child relationship in a table of users that
Suppose I have a set {a, b, c, d} . I want to create
I want to create a small delay so that my 1st set of codes
I want to create sub-domains using PHP on the fly. Suppose a user registers
Suppose I want to create a (stateless) WCF service with three methods exposed on
I want to create a set , list , etc. from an iterator. Unfortunately,
Suppose I have a simple MySQL table that looks like this: CREATE TABLE `my_table`
Suppose I have a table like that: create table reserved ( id int(4) primary

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.