about delagate signature and parameters

C

# Cyrille37 #

Hello,

I come here to expose my use of delegate.
I hope there is a better fashion, because in that form, parameters could not be
verified at compilation time.

<code>
delegate void TheMethodDelegate( param1, param2 );
void TheMethod( param1, param2 )
{
}

void AnotherMethod()
{
this.BeginInvoke(
new TheMethodDelegate(TheMethod),
new object[] { "foo" }
);
}
</code>

You can see that one parameter is missing ...
And the compiler do not say anything.

Is there another method that be safe for method signature verification at
compilation time ?

Thanks for you lights,
cyrille.
 
J

Jon Skeet [C# MVP]

# Cyrille37 # said:
I come here to expose my use of delegate.
I hope there is a better fashion, because in that form, parameters could not be
verified at compilation time.

You can see that one parameter is missing ...
And the compiler do not say anything.

Is there another method that be safe for method signature verification at
compilation time ?

Not if you need to provide the parameters separately. Now, you could
keep the potential for problems down by having a static method which
took a TheMethodDelegate and the two parameters (and maybe what to call
BeginInvoke on). That way you'd only need one call to BeginInvoke, so
only one place where things could go wrong - and that's easy to verify
in terms of correctness, because you've been given all the parameters
you need and *only* the parameters you need.

Of course, you'd need a method like that for each delegate type you
wanted to use...

Jon
 
C

# Cyrille37 #

Jon Skeet [C# MVP] a écrit :
Not if you need to provide the parameters separately. Now, you could
keep the potential for problems down by having a static method which
took a TheMethodDelegate and the two parameters (and maybe what to call
BeginInvoke on). That way you'd only need one call to BeginInvoke, so
only one place where things could go wrong - and that's easy to verify
in terms of correctness, because you've been given all the parameters
you need and *only* the parameters you need.

Of course, you'd need a method like that for each delegate type you
wanted to use...

Jon

Thanks Jon.

Is that also true for .Net 2.0 ?
Is there, or not, some new practices on that subject ?

cyrille.
 
J

Jon Skeet [C# MVP]

# Cyrille37 # said:
Is that also true for .Net 2.0 ?
Is there, or not, some new practices on that subject ?

Yes, it's still true for .NET 2.0, and has to be - if you're calling
BeginInvoke, you've got to provide the parameters as an array of
objects. There's no way round that, and there's no way the compiler can
detect whether or not that array is appropriate for the delegate you're
passing.

Jon
 

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