mirror of
https://github.com/m-lamonaca/dev-notes.git
synced 2025-04-07 11:26:41 +00:00
Improve notes on Events & Delegates
This commit is contained in:
parent
9a56180316
commit
090fb95a0e
1 changed files with 21 additions and 32 deletions
|
@ -2091,13 +2091,13 @@ Any method from any accessible class or struct that matches the delegate type ca
|
||||||
This makes it possible to programmatically change method calls, and also plug new code into existing classes.
|
This makes it possible to programmatically change method calls, and also plug new code into existing classes.
|
||||||
|
|
||||||
```cs
|
```cs
|
||||||
// dedlegate definition
|
// delegate definition
|
||||||
public delegate Type DelegateName(Type param, ...); // can take any method with specidied type params and return type
|
public delegate Type Delegate(Type param, ...); // can take any method with specidied type params and return type
|
||||||
public delegate bool Predicate<in T>(T obj);
|
public delegate Type Delegate<T>(T param); // generic delegate
|
||||||
|
|
||||||
// delegate creation
|
// delegate creation
|
||||||
var delegate = new Delegate<Type>(method); // explicit creation, useful when compiler cannot infer delegate type
|
var delegate = new Delegate<Type>(Method); // explicit creation, useful when compiler cannot infer delegate type
|
||||||
Delegate<Type> delegate = method; // implicit creation
|
Delegate<Type> delegate = Method; // implicit creation
|
||||||
```
|
```
|
||||||
|
|
||||||
### [Multicast Delegates](https://docs.microsoft.com/en-us/dotnet/api/system.multicastdelegate)
|
### [Multicast Delegates](https://docs.microsoft.com/en-us/dotnet/api/system.multicastdelegate)
|
||||||
|
@ -2105,9 +2105,9 @@ Delegate<Type> delegate = method; // implicit creation
|
||||||
**Multicast Delegares** are delegates that can have more than one element in their invocation list.
|
**Multicast Delegares** are delegates that can have more than one element in their invocation list.
|
||||||
|
|
||||||
```cs
|
```cs
|
||||||
Delegate<Type> multicastDelegate = method1 + method2 + ...; // multicast delegate creation
|
Delegate<Type> multicastDelegate = Method1 + Method2 + ...; // multicast delegate creation
|
||||||
multicastDelegate += method; // add method to delegate
|
multicastDelegate += Method; // add method to delegate
|
||||||
multicastDelegate -= method; // remove method from delegate
|
multicastDelegate -= Method; // remove method from delegate
|
||||||
```
|
```
|
||||||
|
|
||||||
**NOTE**: Delegate removal behaves in a potentially surprising way if the delegate removed refers to multiple methods.
|
**NOTE**: Delegate removal behaves in a potentially surprising way if the delegate removed refers to multiple methods.
|
||||||
|
@ -2119,7 +2119,7 @@ Invoking a delegate with a single target method works as though the code had cal
|
||||||
Invoking a multicast delegate is just like calling each of its target methods in turn.
|
Invoking a multicast delegate is just like calling each of its target methods in turn.
|
||||||
|
|
||||||
```cs
|
```cs
|
||||||
Delegate<Type> delegate = method;
|
Delegate<Type> delegate = Method;
|
||||||
|
|
||||||
delegate(args); // use delegat as standard method
|
delegate(args); // use delegat as standard method
|
||||||
delegate.DynamicInvoke(argsArray); // Dynamically invokes the method represented by the current delegate.
|
delegate.DynamicInvoke(argsArray); // Dynamically invokes the method represented by the current delegate.
|
||||||
|
@ -2175,10 +2175,11 @@ The class who raises events is called _Publisher_, and the class who receives th
|
||||||
Typically, a publisher raises an event when some action occurred. The subscribers, who are interested in getting a notification when an action occurred, should register with an event and handle it.
|
Typically, a publisher raises an event when some action occurred. The subscribers, who are interested in getting a notification when an action occurred, should register with an event and handle it.
|
||||||
|
|
||||||
```cs
|
```cs
|
||||||
|
public delegate void EventDelegate(object sender, CustomEventArgs args); // called on event trigger
|
||||||
|
|
||||||
public class Publisher
|
public class Publisher
|
||||||
{
|
{
|
||||||
|
public event EventDelegate Event;
|
||||||
public event Delegate<Type> Event;
|
|
||||||
|
|
||||||
// A derived class should always call the On<EventName> method of the base class to ensure that registered delegates receive the event.
|
// A derived class should always call the On<EventName> method of the base class to ensure that registered delegates receive the event.
|
||||||
public virtual void OnEvent(Type param)
|
public virtual void OnEvent(Type param)
|
||||||
|
@ -2193,19 +2194,15 @@ public class Publisher
|
||||||
```cs
|
```cs
|
||||||
public class Subscriber
|
public class Subscriber
|
||||||
{
|
{
|
||||||
|
|
||||||
Publisher publisher = new Publisher();
|
Publisher publisher = new Publisher();
|
||||||
|
|
||||||
public Subscriber()
|
public Subscriber()
|
||||||
{
|
{
|
||||||
publisher.Event += eh_Event; // register handler (+= syntax)
|
publisher.OnEvent += Handler; // register handler (+= syntax)
|
||||||
}
|
}
|
||||||
|
|
||||||
// event handler, handles the event because it matches the signature of the Event delegate.
|
// event handler, matches the signature of the Event delegate.
|
||||||
public Delegate<Type> eh_Event()
|
public Type Handler() { /* act */ }
|
||||||
{
|
|
||||||
// act
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -2232,22 +2229,17 @@ public class Subsciber
|
||||||
|
|
||||||
public Subscriber()
|
public Subscriber()
|
||||||
{
|
{
|
||||||
publisher.Event += eh_Event; // register handler (+= syntax)
|
publisher.OnEvent += Handler; // register handler (+= syntax)
|
||||||
}
|
}
|
||||||
|
|
||||||
public void eh_Event(object sender, EventArgs e)
|
public Type Handler(object sender, EventArgs e) { /* act */ }
|
||||||
{
|
|
||||||
// act
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### Custom Event Args
|
### Custom Event Args
|
||||||
|
|
||||||
```cs
|
```cs
|
||||||
public class CustomEventArgs : EventArgs {
|
public class CustomEventArgs : EventArgs { }
|
||||||
// custom attributes
|
|
||||||
}
|
|
||||||
|
|
||||||
public class Publisher
|
public class Publisher
|
||||||
{
|
{
|
||||||
|
@ -2265,13 +2257,10 @@ public class Subsciber
|
||||||
|
|
||||||
public Subscriber()
|
public Subscriber()
|
||||||
{
|
{
|
||||||
publisher.Event += eh_Event; // register handler (+= syntax)
|
publisher.OnEvent += Handler; // register handler (+= syntax)
|
||||||
}
|
}
|
||||||
|
|
||||||
public void eh_Event(object sender, CustomEventArgs e)
|
public Type Handler(object sender, CustomEventArgs e) { /* act */ }
|
||||||
{
|
|
||||||
// act
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue