[Serializable] |
Most languages implement a delegate keyword, and compilers for those languages are able to derive from the MulticastDelegate class; therefore, users should use the delegate keyword provided by the language.
The declaration of a delegate type establishes a contract that specifies the signature of one or more methods. A delegate is an instance of a delegate type, and references one or more of the following:
A delegate can reference a method only if the signature of the method exactly matches the signature specified by the delegate type. When a delegate references an instance method, the delegate stores a reference to the method's entry point and a reference to an object, called the target, which is the class instance that the method is invoked on. The target of an instance method cannot be null. When a delegate references a static method, the delegate stores a reference to the method's entry point. The target of a static method is null.
The invocation list of a delegate is an ordered set of delegates in which each element of the list invokes exactly one of the methods invoked by the delegate. An invocation list can contain duplicate methods. During an invocation, a delegate invokes methods in the order in which they appear in the invocation list. A delegate attempts to invoke every method in its invocation list; duplicates are invoked once for each time they appear in the invocation list. Delegates are immutable; once created, the invocation list of a delegate does not change.
A delegate is either multicast (combinable) or singlecast (noncombinable). Multicast or combinable delegates invoke one or more methods, and can be used in combining operations. Singlecast or noncombinable delegates invoke exactly one method and cannot be used in combining operations. The invocation list of a singlecast delegate A contains a single element: a reference to A.
Combining operations, such as Delegate.Combine and Delegate.Remove, do not alter existing delegates. Instead, such an operation returns a new delegate that contains the results of the operation, an unchanged delegate, or null. A combining operation returns null when the result of the operation is a delegate that does not reference at least one method. A combining operation returns an unchanged delegate when the requested operation has no effect.
If an invoked method throws an exception, the method stops executing, the exception is passed back to the caller of the delegate, and remaining methods in the invocation list are not invoked. Catching the exception in the caller does not alter this behavior.
When the signature of the methods invoked by a delegate includes a return value, the delegate returns the return value of the last element in the invocation list. When the signature includes a parameter that is passed by reference, the final value of the parameter is the result of every method in the invocation list executing sequentially and updating the parameter's value.
Compilers provide two additional methods to the delegate: BeginInvoke and EndInvoke. For more information on these methods, see .
The closest equivalent of a delegate in C or C++ is a function pointer. However, a function pointer can only reference static functions, whereas a delegate can reference both static and instance methods. When the delegate references an instance method, the delegate stores not only a reference to the method's entry point, but also a reference to the class instance for which to invoke the method. Unlike function pointers, delegates are object-oriented, type-safe, and secure.
using System; public class SamplesDelegate { // Declares a delegate for a method that takes in an int and returns a String. public delegate String myMethodDelegate( int myInt ); // Defines some methods to which the delegate can point. public class mySubClass { public static String myStringMethod ( int myInt ) { if ( myInt > 0 ) return( "positive" ); if ( myInt < 0 ) return( "negative" ); return ( "zero" ); } public static String mySignMethod ( int myInt ) { if ( myInt > 0 ) return( "+" ); if ( myInt < 0 ) return( "-" ); return ( "" ); } } public static void Main() { // Creates one delegate for each method. myMethodDelegate myD1 = new myMethodDelegate( mySubClass.myStringMethod ); myMethodDelegate myD2 = new myMethodDelegate( mySubClass.mySignMethod ); // Invokes the delegates. Console.WriteLine( "{0} is {1}; use the sign \"{2}\".\n", 5, myD1( 5 ), myD2( 5 ) ); Console.WriteLine( "{0} is {1}; use the sign \"{2}\".\n", -3, myD1( -3 ), myD2( -3 ) ); Console.WriteLine( "{0} is {1}; use the sign \"{2}\".\n", 0, myD1( 0 ), myD2( 0 ) ); } } /* Output: 5 is positive; use the sign "+". -3 is negative; use the sign "-". 0 is zero; use the sign "". */
Method | Read-only Gets the static method represented by the delegate. |
Target | Read-only Gets the class instance on which the current delegate invokes the instance method. |
Clone | Creates a shallow copy of the delegate. |
Combine | Overloaded:Combine(Delegate[] delegates) Concatenates the invocation lists of an array of multicast (combinable) delegates. |
Combine | Overloaded:Combine(Delegate a, Delegate b) Concatenates the invocation lists of two multicast (combinable) delegates. |
CreateDelegate | Overloaded:CreateDelegate(Type type, MethodInfo method) Creates a delegate of the specified type to represent the specified static method. |
CreateDelegate | Overloaded:CreateDelegate(Type type, object target, string method) Creates a delegate of the specified type that represents the specified instance method to invoke on the specified class instance. |
CreateDelegate | Overloaded:CreateDelegate(Type type, Type target, string method) Creates a delegate of the specified type that represents the specified static method of the specified class. |
CreateDelegate | Overloaded:CreateDelegate(Type type, object target, string method, bool ignoreCase) Creates a delegate of the specified type that represents the specified instance method to invoke on the specified class instance with the specified case-sensitivity. |
DynamicInvoke | Dynamically invokes (late-bound) the method represented by the current delegate. |
Equals | Overridden: Determines whether the specified object and the current singlecast (noncombinable) delegate share the same target, method, and invocation list. |
GetHashCode | Overridden: Returns a hash code for the delegate. |
GetInvocationList | Returns the invocation list of the delegate. |
GetObjectData | Implements the ISerializable interface and returns the data needed to serialize the delegate. |
GetType (inherited from System.Object) |
See base class member description: System.Object.GetType Derived from System.Object, the primary base class for all objects. |
Remove | Removes the invocation list of a delegate from the invocation list of another delegate. |
ToString (inherited from System.Object) |
See base class member description: System.Object.ToString Derived from System.Object, the primary base class for all objects. |
op_Equality | Determines whether the specified delegates are equal. |
op_Inequality | Determines whether the specified delegates are not equal. |
ctor #1 | Overloaded:.ctor(object target, string method) Initializes a delegate that invokes the specified instance method on the specified class instance. |
ctor #2 | Overloaded:.ctor(Type target, string method) Initializes a delegate that invokes the specified static method from the specified class. |
CombineImpl | Concatenates the invocation lists of the specified multicast (combinable) delegate and the current multicast (combinable) delegate. |
DynamicInvokeImpl | Dynamically invokes (late-bound) the method represented by the current delegate. |
Finalize (inherited from System.Object) |
See base class member description: System.Object.Finalize Derived from System.Object, the primary base class for all objects. |
GetMethodImpl | Gets the static method represented by the current delegate. |
MemberwiseClone (inherited from System.Object) |
See base class member description: System.Object.MemberwiseClone Derived from System.Object, the primary base class for all objects. |
RemoveImpl | Removes the invocation list of a delegate from the invocation list of another delegate. |
Hierarchy:
target
method
Exception Type | Condition |
---|---|
ArgumentNullException | target is null. -or- method is null. |
This constructor creates delegates for instance methods only. An instance method is a method that is associated with an instance of a class; a static method is a method that is associated with the class itself.
target
method
Exception Type | Condition |
---|---|
ArgumentNullException | target is null. -or- method is null. |
This constructor creates delegates for static methods only. An instance method is a method that is associated with an instance of a class; a static method is a method that is associated with the class itself.
To get the Type that represents a class, use the Type.GetType method with the fully qualified (including namespace) name of the class. For example, passing "System.Threading.ThreadStart" to Type.GetType returns a Type for the ThreadStart class.
public MethodInfo Method {get;}
|
Exception Type | Condition |
---|---|
MemberAccessException | The caller does not have access to the method represented by the delegate (for example, if the method is private). |
public object Target {get;}
|
If the delegate invokes one or more instance methods, this property returns the target of the last instance method in the invocation list.
public virtual object Clone(); |
A shallow copy creates a new instance of the same type as the original object, and then copies the non-static fields of the original object. If the field is a value type, a bit-by-bit copy of the field is performed. If the field is a reference type, the reference is copied but the referred object is not; therefore, the reference in the original object and the reference in the clone point to the same object. In contrast, a deep copy of an object duplicates everything directly or indirectly referenced by the fields in the object.
public static Delegate Combine( |
delegates
-or-
null, if delegates is null, if delegates contains zero elements, or if every entry in delegates is null.
Exception Type | Condition |
---|---|
ArgumentException | Not all the non-null entries in delegates are instances of the same delegate type. |
MulticastNotSupportedException | One or more non-null entries in delegates are singlecast (noncombinable) delegates. |
The invocation list can contain duplicate entries; that is, entries that refer to the same method on the same object.
Delegate.Combine is useful for creating event handlers that call multiple methods each time an event occurs.
a
b
-or-
a, if b is null.
-or-
b, if a is null.
-or-
null, if both a and b are null.
Exception Type | Condition |
---|---|
ArgumentException | Both a and b are not null, and a and b are not instances of the same delegate type. |
MulticastNotSupportedException | Both a and b are not null and are singlecast (noncombinable) delegates. |
Delegate.Combine is useful for creating event handlers that call multiple methods each time an event occurs.
d
-or-
The current multicast (combinable) delegate, if d is null.
Exception Type | Condition |
---|---|
MulticastNotSupportedException | Always thrown. |
The current implementation simply throws a MulticastNotSupportedException.
The invocation list can contain duplicate entries; that is, entries that refer to the same method on the same object.
public static Delegate CreateDelegate( |
type
method
Exception Type | Condition |
---|---|
ArgumentNullException | type is null. -or- method is null. |
ArgumentException | type does not inherit from either Delegate or MulticastDelegate. -or- method is not a static method. |
InvalidProgramException | The Invoke method of type is not found. |
MemberAccessException | The caller does not have access to the method represented by the delegate (for example, if the method is private). |
SecurityException | method is outside the current assembly and the caller does not have ReflectionPermission for the assembly containing method. |
To get the Type that represents a class, use the Type.GetType method with the fully qualified (including namespace) name of the class. For example, passing "System.Threading.ThreadStart" to Type.GetType returns a Type for the ThreadStart class.
type
target
method
Exception Type | Condition |
---|---|
ArgumentNullException | type is null. -or- target is null. -or- method is null. |
ArgumentException | type does not inherit from either Delegate or MulticastDelegate. -or- method is not an instance method. |
MemberAccessException | The caller does not have access to the method represented by the delegate (for example, if the method is private). |
SecurityException | method is outside the current assembly and the caller does not have ReflectionPermission for the assembly containing method. |
To get the Type that represents a class, use the Type.GetType method with the fully qualified (including namespace) name of the class. For example, passing "System.Threading.ThreadStart" to Type.GetType returns a Type for the ThreadStart class.
type
target
method
Exception Type | Condition |
---|---|
ArgumentNullException | type is null. -or- target is null. -or- method is null. |
ArgumentException | type does not inherit from either Delegate or MulticastDelegate. -or- method is not a static method. |
MemberAccessException | The caller does not have access to the method represented by the delegate (for example, if the method is private). |
SecurityException | method is outside the current assembly and the caller does not have ReflectionPermission for the assembly containing method. |
To get the Type that represents a class, use the Type.GetType method with the fully qualified (including namespace) name of the class. For example, passing "System.Threading.ThreadStart" to Type.GetType returns a Type for the ThreadStart class.
public static Delegate CreateDelegate( |
type
target
method
ignoreCase
Exception Type | Condition |
---|---|
ArgumentNullException | type is null. -or- target is null. -or- method is null. |
ArgumentException | type does not inherit from either Delegate or MulticastDelegate. -or- method is not an instance method. |
MemberAccessException | The caller does not have access to the method represented by the delegate (for example, if the method is private). |
SecurityException | method is outside the current assembly and the caller does not have ReflectionPermission for the assembly containing method. |
To get the Type that represents a class, use the Type.GetType method with the fully qualified (including namespace) name of the class. For example, passing "System.Threading.ThreadStart" to Type.GetType returns a Type for the ThreadStart class.
args
-or-
null, if the method represented by the current delegate does not require arguments.
An array of objects that are the arguments to pass to the method represented by the current delegate.-or-
null, if the method represented by the current delegate does not require arguments.
Exception Type | Condition |
---|---|
MemberAccessException | The caller does not have access to the method represented by the delegate (for example, if the method is private). -or- The number, order, or type of parameters listed in args is invalid. |
TargetException | The method represented by the delegate is an instance method and the target object is null. -or- The method represented by the delegate is invoked on an object or a class that does not support it. |
TargetInvocationException | One of the encapsulated methods throws an exception. |
args
-or-
null, if the method represented by the current delegate does not require arguments.
An array of objects that are the arguments to pass to the method represented by the current delegate.-or-
null, if the method represented by the current delegate does not require arguments.
Exception Type | Condition |
---|---|
MemberAccessException | The caller does not have access to the method represented by the delegate (for example, if the method is private). -or- The number, order, or type of parameters listed in args is invalid. |
TargetException | The method represented by the delegate is an instance method and the target object is null. -or- The method represented by the delegate is invoked on an object or a class that does not support it. |
TargetInvocationException | One of the encapsulated methods throws an exception. |
obj
Exception Type | Condition |
---|---|
MemberAccessException | The caller does not have access to the method represented by the delegate (for example, if the method is private). |
Two delegates with the same methods, the same targets, and the same invocation lists are considered equal, even if they are not both multicast (combinable) or both singlecast (noncombinable).
The methods and targets are compared for equality as follows:
Two invocation lists are considered identical only if they have the same order and the corresponding elements from the two lists represent the same method and target.
~Delegate(); |
public override int GetHashCode(); |
public virtual Delegate[] GetInvocationList(); |
The order of the delegates in the array is the same order in which the current delegate invokes the methods that those delegates represent.
protected virtual MethodInfo GetMethodImpl(); |
Exception Type | Condition |
---|---|
MemberAccessException | The caller does not have access to the method represented by the delegate (for example, if the method is private). |
public virtual void GetObjectData( |
info
context
Exception Type | Condition |
---|---|
ArgumentNullException | info is null. |
public Type GetType(); |
protected object MemberwiseClone(); |
d1
d2
The methods and targets are compared for equality as follows:
Two invocation lists are considered identical if they have the same order and the corresponding elements from the two lists represent the same method and target.
d1
d2
The methods and targets are compared for equality as follows:
Two invocation lists are not equal if they have different sizes, if they are ordered differently, or if at least one element from one list represents a method or target that is different from that represented by its corresponding element in the other list.
source
value
-or-
source, if value is null, or if the invocation list of value is not found within the invocation list of source.
-or-
null, if the invocation list of value is equal to the invocation list of source, or if source is null.
Exception Type | Condition |
---|---|
MemberAccessException | The caller does not have access to the method represented by the delegate (for example, if the method is private). |
d
-or-
The current delegate, if value is null, or if the invocation list of value is not found within the current delegate's invocation list.
-or-
null, if the invocation list of value is equal to the current delegate's invocation list.
Exception Type | Condition |
---|---|
MemberAccessException | The caller does not have access to the method represented by the delegate (for example, if the method is private). |
public virtual string ToString(); |