Delegates with creation time arguments?

C

chelrw

The pattern supported by C# for delegates seems to be:

new Delegate(CallbackFunction)
Delegate.Invoke(arg1, arg2)
Callbackfunction(arg1,arg2) is invoked

What is the simplest way to get the pattern:

new Delegate(CallbackFunction, arg1)
Delegate.Invoke(arg2, arg3)
Callbackfunction(arg1,arg2,arg3) is invoked

I would have tried inheriting from Delegate an adding a new
constructor etc. but the class is sealed??

thanks,

r
 
P

Pavel Minaev

The pattern supported by C# for delegates seems to be:

new Delegate(CallbackFunction)
Delegate.Invoke(arg1, arg2)
Callbackfunction(arg1,arg2) is invoked

What is the simplest way to get the pattern:

new Delegate(CallbackFunction, arg1)
Delegate.Invoke(arg2, arg3)
Callbackfunction(arg1,arg2,arg3) is invoked

You have to use a wrapper function. A delegate by itself can only bind
a single argument in the called method (normally "this", though for a
static method you can also bind the first argument of that). For what
you want, use lambdas or anonymous delegates:

(arg2, arg3) => CallbackFunction(arg1value, arg2, arg3);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top