Strategy to: Hook-up a Delegate Using Representation. Get an example of the delegate, by using the CreateDelegate process.
By using expression to burden and owned assemblies, you cannot make use of words services similar to the C# += driver and the artistic practical AddHandler account to attach activities. This operations demonstrate getting hook up a preexisting technique to an event by obtaining all needed type through representation, and how to generate a dynamic method making use of picture give off and catch it to a meeting.
For the next technique to get together an event-handling delegate, your rule instance towards AddEventHandler method of the EventInfo type.
To hook-up a delegate using picture
Burden a system which contains a kind that lifts occasions. Assemblies are often loaded with the Assembly.Load system. Maintain this case straightforward, a derived kind in the current set up is utilized, so that the GetExecutingAssembly strategy is regularly weight the existing assembly.
Put a Type object stage the sort, and produce an example on the kinds. The CreateInstance(kinds) method is included in in this article rule because the version has a parameterless constructor. You will find additional overloads for the CreateInstance process useful if the form you might be starting doesn’t have a parameterless constructor. The example is actually kept as means Object to sustain the literary composition that there’s nothing regarded on the assembly. (picture enables you to attain the kinds in an assembly lacking the knowledge of his or her brands ahead.)
See an EventInfo object presenting the expensive vacation event, and make use of the EventHandlerType residence to find the sort of delegate used to use the expensive vacation event. During the preceding signal, an EventInfo your push function are acquired.
Put a MethodInfo target representing the tactic that deals with case. The overall course rule when you look at the case segment later on found in this topic is made up of one way that matches the trademark of this EventHandler delegate, which deals with 1 click occasion, but you can furthermore generate active techniques at work opportunity. For things, notice accompanying processes, for creating an event handler at go occasion by using a dynamic method.
This method are fixed ( distributed in graphic Essential), so that the delegate form must furnished. With the overloads of CreateDelegate that take a MethodInfo is preferred.
Receive the mix accessor way and invoke they to hook up the big event. All activities have actually a put accessor and a remove accessor, which have been undetectable from the syntax of high languages. For example, C# uses the += operator to hook up functions, and Visual practical utilizes the AddHandler report. All of the following laws receives the put accessor associated with the press occasion and evoke they late-bound, driving into the delegate incidences. The discussions ought to be passed away as a wide selection.
Experience case. Here signal reveals the design characterized in signal sample. Clicking on the shape creates the big event handler.
To come up with a meeting handler at operate experience by using a dynamic see page strategy
Event-handler systems is often made at extend time period, making use of light-weight vibrant strategies and reflection give off. To create a meeting handler, need the repay sort and parameter different the delegate. These could generally be obtained by examining the delegate’s request technique. The following signal makes use of the GetDelegateReturnType and GetDelegateParameterTypes solutions to receive these details. The rule of these approaches come in the illustration section after with this problem.
It is not necessarily essential to label a DynamicMethod, so the clear string works extremely well. In subsequent rule, the previous discussion colleagues the dynamic approach using present kinds, offering the delegate accessibility every public and exclusive members of the illustration school.
Make a method human anatomy. This technique loads a series, dubs the excess of the MessageBox.Show method that takes a string, pops the return benefits off the pile (because handler doesn’t have return form), and return. To understand more about giving off dynamic methods, discover how to: outline and Execute energetic Methods.
Completed the vibrant means by dialing their CreateDelegate technique. Operate the apply accessor to add the delegate to the invocation listing for its function.
Test the big event. Here signal loads the proper execution described in the laws situation. Clicking on the shape invokes both predefined show handler along with emitted party handler.
Illustration
The following code model displays a way to attach an active technique to an occasion using reflection, and how to use the DynamicMethod lessons to give off a mode at managed some time and catch it to an event.

Leave a Reply