Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#4.0

Generic Thread Start

0.00/5 (No votes)
22 Mar 2011CPOL 5.2K  
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)