Click here to Skip to main content
15,879,535 members
Articles / Programming Languages / C# 4.0
Alternative
Tip/Trick

Generic Thread Start

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
22 Mar 2011CPOL 5K  
Another approach is to use a generic delegate-factory class. Essentially, the class has a field of generic type, a delegate field which accepts an argument of that type, an parameterless instance method which accepts an argument of that type and invokes the delegate with it, and a static method...
Another approach is to use a generic delegate-factory class. Essentially, the class has a field of generic type, a delegate field which accepts an argument of that type, an parameterless instance method which accepts an argument of that type and invokes the delegate with it, and a static method which accepts a delegate of the same type as the field, along with the parameter that should be passed to that delegate. It creates a new class instance using the parameter values as fields, and returns a new delegate to invoke the instance method.

If I need a MethodInvoker which will execute foo(bar), e.g. to pass to a SetCallback function, I simply write something like (VB syntax):
SetCallback(InvokeMaker.Create(AddressOf foo, bar))

I have a family of such classes/factories for up to four parameters. Very handy.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Unknown
Embedded systems programmer since 1994.

Comments and Discussions

 
-- There are no messages in this forum --