I have C# class for which I am creating multiple objects for that class.
Now, I have one event for which all the objects of that class needs perform operation(In other words, I need to call specific function of all the object for that event).
The event is external to class.
Event is caused when I receive data from some external input in other class B or class C and I have to send that data to all the object of class A using method or event.
I need to raise event from multiple class B/C/D in class A.
A modern, clean way to handle this would be to use Reactive Extensions.
Suppose your event argument is of type
EArg.Place a static
Subject<EArg>in your class A.Each instance of A can observe this
IObservable<T>and act on it.When any external class calls (perhaps via a static method on class A) the
OnNextmethod, all of the instances of class A can respond to it.Essentially you are looking for a pub-sub mechanism and Reactive Extensions is the cleanest way to achieve that.
NB, don't expose the
Subjectexternally, see for example Why are Subjects not recommended in .NET Reactive Extensions?