If you use reflection to fill and go assemblies, you cannot utilize terms qualities much like the C# += owner or the optical Essential AddHandler statement to hook-up activities. The below processes showcase how exactly to hook up an existing technique to an event through getting all essential kinds through expression, and the way to make a dynamic system making use of representation release and catch it up to a meeting.
For another approach to connect an event-handling delegate, begin code situation for your AddEventHandler method for the EventInfo school.
To connect a delegate using picture
Weight a meeting comprising a type that increases activities. Assemblies are usually full of the system.Load process. Maintain this model basic, a derived version in the current system can be used, as a result GetExecutingAssembly method is familiar with load the existing installation.
Come a kind item symbolizing the character, and develop an example belonging to the means. The CreateInstance(kind) technique is made use of in here rule because kind have a parameterless constructor. There are many more overloads on the CreateInstance strategy that can be used if the type you’re getting do not have a parameterless constructor. The newest incidences is saved as form target to maintain the fiction that there’s nothing understood regarding the forum. (expression helps you attain the type in an assembly without knowing her name beforehand.)
Put an EventInfo subject presenting in case, and employ the EventHandlerType home to achieve the sort of delegate used to deal with case. In following code, an EventInfo towards Click show are gotten.
Bring a MethodInfo object standing for the method that manages the big event. The overall system laws in instance point afterwards contained in this topic have a technique that fits the unique for the EventHandler delegate, which deals with 1 click party, but you can additionally create vibrant methods at work moment. For info, see the accompanying method, for creating a conference handler at go your time using a dynamic strategy.
This technique was fixed ( revealed in graphic standard), therefore, the delegate kinds must be provided. With the overloads of CreateDelegate that grab a MethodInfo is usually recommended.
Obtain the mix accessor system and invoke it to get together the event. All functions has an incorporate accessor and a clear away accessor, which might be invisible by syntax of high-level languages. Eg, C# makes use of the += agent to hook-up competition, and aesthetic standard uses the AddHandler argument. This laws contains the incorporate accessor with the Click show and invokes they late-bound, driving for the delegate example. The reasons must certanly be passed as a selection.
Experience the expensive vacation event. In this article laws demonstrates the contour explained from inside the rule instance. Clicking on the contour evoke the expensive vacation event handler.
In order to create a celebration handler at go time period simply by using a vibrant system
Event-handler practices tends to be created at work hours, using light-weight powerful practices and reflection produce. To construct a celebration handler, you need the return form and factor varieties the delegate. These may end up being received by examining the delegate’s request strategy. The next rule uses the GetDelegateReturnType and GetDelegateParameterTypes methods to obtain this information. The code for these approaches can be obtained from the Example part later in this particular concept.
It is not necessarily necessary to identify a DynamicMethod, so the vacant string works extremely well. Within the subsequent rule, the last argument acquaintances the active system on your present form, providing the delegate having access to all of the open and individual people in the case lessons.
Create a mode human body. This approach loads a series, telephone calls the overload associated with MessageBox.Show system that takes a series, pops the homecoming benefits off the heap (considering that the handler does not have homecoming means), and profit. For more information on giving off compelling options, find out how to: establish and do energetic Options.
Execute the vibrant means by phoning their CreateDelegate process. Take advantage of put accessor to provide the delegate to your invocation show when it comes to event.
Taste the expensive vacation event. The below code forces the shape characterized through the signal example. Clicking on the proper execution evoke the predetermined party handler as well as the emitted show handler.
Instance
The below rule model indicates getting hook up a preexisting solution to an occasion making use of reflection, and how to use the DynamicMethod school to emit a technique at operate some time and catch it up to a meeting.