Private delegate void dldislay string s có nghĩa là gì năm 2024
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support. Show System.Delegate.CreateDelegate methods
In this articleThis article provides supplementary remarks to the reference documentation for this API. The CreateDelegate methods create a delegate of a specified type. methodThis method overload is equivalent to calling the method overload and specifying
0. ExamplesThis section contains two code examples. The first example demonstrates the two kinds of delegates that can be created with this method overload: open over an instance method and open over a static method. The second code example demonstrates compatible parameter types and return types. Example 1The following code example demonstrates the two ways a delegate can be created using this overload of the CreateDelegate method. Note There are two overloads of the CreateDelegate method that specify a MethodInfo but not a first argument; their functionality is the same except that one allows you to specify whether to throw on failure to bind, and the other always throws. This code example uses both overloads. The example declares a class
1 with a static method
2 and an instance method
3, and two delegate types:
4 takes an instance of
1 and a string, and
6 takes a string. A second class named
7 contains the code that creates the delegates.
Example 2The following code example demonstrates compatibility of parameter types and return types. The code example defines a base class named
2 and a class named
3 that derives from
2. The derived class has a
5 (
6 in Visual Basic) method named
7 with one parameter of type
2 and a return type of
3. The code example also defines a delegate named
7 that has one parameter of type
3 and a return type of
2. The code example demonstrates that the delegate named
7 can be used to represent the method
7. The method can be bound to the delegate because:
The code example produces no output.
and methodsThe functionality of these two overloads is the same except that one allows you to specify whether to throw on failure to bind, and the other always throws. The delegate type and the method must have compatible return types. That is, the return type of
2 must be assignable to the return type of
3.
4, the second parameter to these overloads, is the first argument of the method the delegate represents. If
4 is supplied, it is passed to
2 every time the delegate is invoked;
4 is said to be bound to the delegate, and the delegate is said to be closed over its first argument. If
2 is
5 (
6 in Visual Basic), the argument list supplied when invoking the delegate includes all parameters except the first; if
2 is an instance method, then
4 is passed to the hidden instance parameter (represented by
3 in C#, or by
4 in Visual Basic). If
4 is supplied, the first parameter of
2 must be a reference type, and
4 must be compatible with that type. Important If
2 is
5 (
6 in Visual Basic) and its first parameter is of type Object or ValueType, then
4 can be a value type. In this case
4 is automatically boxed. Automatic boxing does not occur for any other arguments, as it would in a C# or Visual Basic function call. If
4 is a null reference and
2 is an instance method, the result depends on the signatures of the delegate type
3 and of
2:
If
4 is a null reference and
2 is static, the result depends on the signatures of the delegate type
3 and of
2:
ExampleThe following code example shows all the methods a single delegate type can represent: closed over an instance method, open over an instance method, open over a static method, and closed over a static method. The code example defines two classes,
1 and
2, and a delegate type
3 with one argument of type
1. The classes have matching static and instance methods
3,
6, and
7, and class
1 also has an instance method
2 that has no arguments. A third class named
7 contains the code that creates the delegates.
Compatible parameter types and return typeThe parameter types and return type of a delegate created using this method overload must be compatible with the parameter types and return type of the method the delegate represents; the types do not have to match exactly. A parameter of a delegate is compatible with the corresponding parameter of a method if the type of the delegate parameter is more restrictive than the type of the method parameter, because this guarantees that an argument passed to the delegate can be passed safely to the method. Similarly, the return type of a delegate is compatible with the return type of a method if the return type of the method is more restrictive than the return type of the delegate, because this guarantees that the return value of the method can be cast safely to the return type of the delegate. For example, a delegate with a parameter of type Hashtable and a return type of Object can represent a method with a parameter of type Object and a return value of type Hashtable. Determine the methods a delegate can representAnother useful way to think of the flexibility provided by the overload is that any given delegate can represent four different combinations of method signature and method kind (static versus instance). Consider a delegate type
3 with one argument of type
1. The following describes the methods
3 can represent, ignoring the return type since it must match in all cases:
Collaborate with us on GitHub The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, see our contributor guide. |