If you use representation to fill and powered assemblies, you cannot utilize tongue qualities like C# += agent and/or artistic vital AddHandler statement to connect happenings. The below techniques showcase a way to connect a pre-existing technique to a conference by obtaining most of the essential varieties through representation, and the way to build a dynamic way utilizing click tids link here now representation produce and lift it up to an event.
For an additional method to hook up an event-handling delegate, see the laws example for all the AddEventHandler technique of the EventInfo lessons.
To attach a delegate utilizing representation
Load a meeting which has a sort that lifts parties. Assemblies usually are laden with the forum.Load strategy. To keep this example painless, a derived type with the current economic installation is utilized, therefore, the GetExecutingAssembly method is used to burden the latest system.
Come a sort target stage the character, and create an instance belonging to the type. The CreateInstance(kind) method is utilized in all of the following rule considering that the kind has a parameterless constructor. There are plenty of various other overloads belonging to the CreateInstance strategy which you can use in the event the kind that you are starting does not have a parameterless constructor. The brand new circumstances was put as kinds item to keep up the literary composition that nothing is recognized about the set-up. (picture helps you take advantage of the type in an assembly lacking the knowledge of her figure upfront.)
Get an EventInfo thing standing for the big event, and employ the EventHandlerType assets to achieve the kind of delegate accustomed control the event. Inside subsequent laws, an EventInfo for that touch event was gotten.
See a MethodInfo item representing the tactic that handles the event. The entire regimen laws from inside the model area after in this theme have a way that meets the trademark with the EventHandler delegate, which handles 1 click event, but you can additionally render vibrant systems at managed your time. For specifics, your associated treatment, for producing a celebration handler at run time period by utilizing a dynamic approach.
Using this method happens to be stationary ( distributed in Visual Essential), therefore the delegate kind must be supplied. Making use of the overloads of CreateDelegate that grab a MethodInfo is advised.
How to get the combine accessor way and invoke they to attach the function. All occasions have got an add accessor and a remove accessor, that are concealed with the syntax of high-level dialects. For example, C# employs the += owner to hook-up happenings, and optical Simple uses the AddHandler report. These rule receives the put accessor associated with press occasion and invokes they late-bound, passing when you look at the delegate incidences. The discussions needs to be died as a variety.
Sample the expensive vacation event. This laws shows the design defined in the laws example. Clicking the shape creates the function handler.
To build an event handler at extend energy by utilizing a vibrant technique
Event-handler means may made at work time period, making use of light powerful practices and reflection produce. To create a meeting handler, you will want the repay kinds and parameter kinds the delegate. These may generally be received by test the delegate’s request process. Listed here signal makes use of the GetDelegateReturnType and GetDelegateParameterTypes ways to acquire this data. The code for those techniques come in the Example point later in this concept.
It is not necessarily essential to list a DynamicMethod, so the unused string can be employed. For the subsequent laws, the past point associates the compelling way using existing kind, supplying the delegate the means to access those open public and private people in the sample lessons.
Make a way human anatomy. This technique loads a line, dubs the overload from the MessageBox.Show process that takes a string, pops the homecoming price from the stack (since handler doesn’t have homecoming form), and yield. To learn more about giving off active options, discover how to: express and Execute Dynamic techniques.
Perform the powerful method by phoning its CreateDelegate system. Operate the put accessor to add the delegate for the invocation variety towards function.
Check in case. The subsequent laws plenty the proper execution explained when you look at the code instance. Hitting the design creates the predefined show handler in addition to the released celebration handler.
Situation
Below laws case reveals a way to attach a pre-existing solution to a meeting using expression, also making use of the DynamicMethod classroom to give off a way at operated time and connect it to a celebration.