开发者

Event in C# - Is it OOP?

Since I start using event in c# I always want to know if it's OOP oriented.

Let me explain. In Java, it had EventListner, Observer/Observable which object have to inherit to be able to fire an event or to listen to it . My point is, in Java, it have to be an object which have the responsibility to notify the subscriber or to do an action after being notify. In 开发者_Python百科c#, all I see is :

public delegate void SomeHandler();
public event SomeHandler OnAction;

...
//somewhere in the firing class
OnAction();

...
//somewhere else in a subscriber class
_generateReport.ReportSubmited += someMethod;

private void someMethod()
{
//do some job
}

No class, only method and attribut...

So, is it OOP and if it is, how does it work?

Thanks !


The type responsible for holding a list of handlers is the SomeHandler type, which is a delegate type. You can build an instance of a delegate from an appropriate method, representing a call to that method... and you can combine delegates together to represent a sequence of calls.

Think of events as language/platform support for the observer pattern, basically.

You should be aware of what events and delegates are like under the hood - see my article on them for some more details.

The non-OO part of this is that although delegates can be passed around like any other object, events can't :( You can use their reflection equivalent (EventInfo) but it's not quite the same thing...


It is OOP- C# just hides the implementation details from you. someMethod() is called on the instance of the class, if it is an instance method, from which it was taken.

In Java, you have to manually inherit and fiddle around.


In many scenarios,like when its required to have a loosely coupling relation between modules which are in different assemblies,its a very good idea to use delegates and events.So a class, by means of these kind of useful objects(I mean delegates and events),can invoke a method without having to know anything about its code,and in such a way we decouple the responsibilities of each class clearly. lets assume we have two classes in completely different assemblies as follows:

        private Rectangle GetActiveWindowBounds(Func<IRunningAppActionResult> 
               methodOne , Func<IRunningAppActionResult>  methodTwo)
                 {
                  Task.Run(() => { return methodOne(); }).Wait();




 _clientAppRectangleBitmapHelper.GetBrowserActiveAreaByColor(Color.Red);
                      Task.Run(() =>  {return methodTwo();
                       }).Wait();
                        return _clientAppRectangle;
              }

and from other class that uses this class and want to make this class to obtain a rectangle,but does not want it to know how obtain an IRunningAppActionResult, we can use Func as follow:

     var browserRectangle = _browserWindowManager.GetActiveWindowBounds(
            () => this.SendActionWithResult(new 
                                 MakeBackgroundRedAction()),
                                         () => this.SendActionWithResult(new 
                  RevertBackgroundAction()));


someMethod() is declared private. It is a private member from a class, which is OOP


I see nothing that contradicts with OOP. Event is just set of two methods that manipulate with delegate (in your case delegate itself if behind the scene). Delegate in turn is linked list of pairs of method reference and target of invocation.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜