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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T11:45:36+00:00 2026-06-02T11:45:36+00:00

Suppose I have some class MyClass. In one part of my code I want

  • 0

Suppose I have some class MyClass. In one part of my code I want something like this:

Observable.Subscribe<MyClass>(myClass => DoSomething(myClass));

Then in another place (file/project/time) I have something like this:

Observable.Publish(instanceOfMyClass);

This second line triggers all the methods that were subscribed with that exact class type. Is this something that the Reactive Extensions (v1 or v2) supports?

It would be useful to specify a SynchronizationContext as part of the Subscribe call. It would also be nice to specify there whether or not the method should be held with a WeakReference. And the Publish method should have the ability to do it all synchronously or give me something I can wait on.

  • 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-02T11:45:38+00:00Added an answer on June 2, 2026 at 11:45 am

    This wouldn’t be hard to create.

    You just need an internal Dictionary<Type, Object> and use it to store each Subject<T> (as the object) by Type.

    You can then just write the two Subscribe and Publish methods to work off of the internal dictionary.

    Should be quite simple in fact.


    Rather than just say it was simple I thought I’d give it a go.

    Here’s my Rx Pub/Sub class:

    public static class RxPS
    {
        private static Dictionary<Type, object> _subjects
            = new Dictionary<Type, object>();
    
        public static IDisposable Subscribe<T>(Action<T> observer)
        {
            lock(_subjects)
            {
                if (!_subjects.ContainsKey(typeof(T)))
                {
                    _subjects.Add(typeof(T), new Subject<T>());
                }
                return (_subjects[typeof(T)] as Subject<T>)
                    .Subscribe(observer);
            }
        }
    
        public static void Publish<T>(T item)
        {
            lock(_subjects)
            {
                if (_subjects.ContainsKey(typeof(T)))
                {
                    (_subjects[typeof(T)] as Subject<T>)
                        .OnNext(item);
                }
            }
        }
    }
    

    And this is how it is used:

    RxPS.Publish(1);
    var d = RxPS.Subscribe<int>(x => Console.WriteLine(x)); 
    RxPS.Publish(2);
    d.Dispose();
    RxPS.Publish(3);
    

    The result is that this code will write 2 to the console only.

    Enjoy!

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

Sidebar

Related Questions

Let's suppose I have xml like this one: <Server Active=No> <Url>http://some.url</Url> </Server> C# class
Suppose I have the following html: This a test of <code>some code</code>. <div class='highlight'>
Suppose I have a class like this: class MyClass { private: vector<MyOtherClass> myMember; public:
I have some HTML that looks like this: <h2>Heading</h2> <div class='myClass'> <h2>Another Heading</h2> </div>
Suppose I have this HTML: <div id=Wrapper> <div class=MyClass>some text</div> <div class=MyClass>some text</div> <div
Suppose I have a class MyClass to which I want to add certain 'observer'
Suppose I have simple class like: class MyClass { private $_prop; public function getProp()
Suppose you have a class where you start some background thread: this class could
Suppose I have a class, and I want to reference some elements in the
Suppose I have a class like: public class MyClass{ public MyClass(){ ... } ...

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.