Connecting Tech Pros Worldwide Help | Site Map

function pointer to a delegate

  #1  
Old November 11th, 2008, 09:35 PM
vcquestions
Guest
 
Posts: n/a
Hi.

Is there way to have a function pointer to a delegate in c++/cli that
would allow me to pass delegates with the same signatures as
parameters to a method?

I'm working with managed code. Let's say we have 2 delegates:
public delegate void FirstDelegate( int i );
public delegate void SecondDelegate( int i );

classA::method1( )
{
int i = 1;
doSomething( );
m_obj->FirstDelegate( i );
}

class A::method2( )
{
doSomething( );
int j = 2;
m_obj->SecondDelegate( j );
}

Is there a way to have a function pointer to a delegate so that I can
just have a common function that would take the delegate as a
parameter:

class A::methodInsteadOf1And2( delegateFunctionPointer )
{
int i = 3;
m_obj->delegateFunctionPointer( i );
}

thanks,
vcq
  #2  
Old November 11th, 2008, 10:25 PM
Paavo Helde
Guest
 
Posts: n/a

re: function pointer to a delegate


vcquestions <vcquestions@gmail.comkirjutas:
Quote:
Hi.
>
Is there way to have a function pointer to a delegate in c++/cli that
would allow me to pass delegates with the same signatures as
parameters to a method?
>
I'm working with managed code. Let's say we have 2 delegates:
In C++ there are no delegates (nor managed code). You will probably get
better answers by posting in some MS newsgroups or forums.

In C++, you can form a pointer to a method in a class, with given
signature, which can resolve to different actual methods when actually
called. I am quite sure this is not helpful for you so I don't give an
example.

Regards
Paavo


  #3  
Old November 11th, 2008, 10:35 PM
Paavo Helde
Guest
 
Posts: n/a

re: function pointer to a delegate


Paavo Helde <nobody@ebi.eekirjutas:
Quote:
vcquestions <vcquestions@gmail.comkirjutas:
>
Quote:
>Hi.
>>
>Is there way to have a function pointer to a delegate in c++/cli that
>would allow me to pass delegates with the same signatures as
>parameters to a method?
>>
>I'm working with managed code. Let's say we have 2 delegates:
>
In C++ there are no delegates (nor managed code). You will probably get
better answers by posting in some MS newsgroups or forums.
Oops sorry, mixed up the newsgroup (again!). We are in a mircosoft ng, so
delegates are presumably on-topic. Too bad I don't know anything about them
:-(

Paavo

  #4  
Old November 12th, 2008, 09:45 PM
Ben Voigt [C++ MVP]
Guest
 
Posts: n/a

re: function pointer to a delegate


vcquestions wrote:
Quote:
Hi.
>
Is there way to have a function pointer to a delegate in c++/cli that
would allow me to pass delegates with the same signatures as
parameters to a method?
>
I'm working with managed code. Let's say we have 2 delegates:
public delegate void FirstDelegate( int i );
public delegate void SecondDelegate( int i );
That's two delegate types with the same signature.
Quote:
>
classA::method1( )
{
int i = 1;
doSomething( );
m_obj->FirstDelegate( i );
That won't work, you can't use the -operator with a type name.

Maybe if you are a little more careful to distinguish between delegate
types, methods (aka member functions), and delegate instances, we'll be able
to answer your question.
Quote:
}
>
class A::method2( )
{
doSomething( );
int j = 2;
m_obj->SecondDelegate( j );
}
>
Is there a way to have a function pointer to a delegate so that I can
just have a common function that would take the delegate as a
parameter:
>
class A::methodInsteadOf1And2( delegateFunctionPointer )
{
int i = 3;
m_obj->delegateFunctionPointer( i );
}
>
thanks,
vcq

  #5  
Old November 16th, 2008, 02:55 AM
vcquestions
Guest
 
Posts: n/a

re: function pointer to a delegate


Ben, thanks for replying. I was trying to figure out a way to pass a
delegate as a parameter so that I could pass delegates with the same
signatures to a method that handles common code. This is straight
forward ( and does not require function pointers which I thought I'd
use originally ). When I run code below, I see that DynamicInvoke
walks the chain of delegates while Method->Invoke just invokes one
method.

I suspect that implementation of DynamicInvoke just walks the chain
and calls Method->Invoke for each. Is that a correct assumption? If
DynamicInvoke does not incur any additional cost, it's probably the
way to go.

Thanks!
vcq

public delegate void FirstDelegate( int i );
public delegate void SecondDelegate( int i );


void CommonMethod(System::Delegate^ test)
{
//!CommonCode goes here

//argumest to the invoked method (should be
passed in as well)
array<Object^>^ pArgs = gcnew array<Object^>(1);
pArgs[0] = 3;

//dynamic invoke
test->DynamicInvoke( pArgs );

//method->invoke
test->Method->Invoke( test->Target, pArgs );

//walk the chain of delegates - same as DynamicInvoke ?
array<Delegate^>^ tests = test->GetInvocationList( );
for ( int i = 0; i < tests->Length; ++i )
{
tests[i]->Method->Invoke( test->Target, pArgs );
}
}


  #6  
Old November 17th, 2008, 03:25 PM
Ben Voigt [C++ MVP]
Guest
 
Posts: n/a

re: function pointer to a delegate


vcquestions wrote:
Quote:
Ben, thanks for replying. I was trying to figure out a way to pass a
delegate as a parameter so that I could pass delegates with the same
signatures to a method that handles common code. This is straight
forward ( and does not require function pointers which I thought I'd
use originally ). When I run code below, I see that DynamicInvoke
walks the chain of delegates while Method->Invoke just invokes one
method.
>
I suspect that implementation of DynamicInvoke just walks the chain
and calls Method->Invoke for each. Is that a correct assumption? If
DynamicInvoke does not incur any additional cost, it's probably the
way to go.
Yes, but... dynamic invocation is much more expensive.

The intended way to invoke a delegate is just

dl->Invoke(the parameters are typesafe here);

or even simpler,

dl(the parameters go here);
Quote:
>
Thanks!
vcq
>
public delegate void FirstDelegate( int i );
public delegate void SecondDelegate( int i );
>
>
void CommonMethod(System::Delegate^ test)
{
//!CommonCode goes here
>
//argumest to the invoked method (should be
passed in as well)
array<Object^>^ pArgs = gcnew array<Object^>(1);
pArgs[0] = 3;
>
//dynamic invoke
test->DynamicInvoke( pArgs );
>
//method->invoke
test->Method->Invoke( test->Target, pArgs );
>
//walk the chain of delegates - same as DynamicInvoke ?
array<Delegate^>^ tests = test->GetInvocationList( );
for ( int i = 0; i < tests->Length; ++i )
{
tests[i]->Method->Invoke( test->Target, pArgs );
}
}

  #7  
Old November 17th, 2008, 09:15 PM
vcquestions
Guest
 
Posts: n/a

re: function pointer to a delegate


in my example ( previous post ), where I pass delegates to CommonMethod
( System::Delegate^ test), I can't just
call test->Invoke( params ) or test( params ) since compiler has no
knowledge of how to resolve this call ( any delegate could be passed )
so I have to pay price for late binding. ( Invoke is not even exposed
on the System::Delegate ). Please correct me if I'm wrong here.

That's why I was trying to figure out if there is a better way to
handle this situation ( have common code that invokes delegates with
the same signature - e.g., I can pass the whole object on which I
invoke a delegate and some param that I would switch on and do: obj-
Quote:
>Del1( ); or obj->Del2( ) ), but the switch statement does not really
help in making code clean. I guess there is no magic - the above 2
choices is all I have...

Thanks!


On Nov 17, 8:21*am, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:
Quote:
vcquestions wrote:
Quote:
Ben, thanks for replying. *I was trying to figure out a way to pass a
delegate as a parameter so that I could pass delegates with the same
signatures to a method that handles common code. *This is straight
forward ( and does not require function pointers which I thought I'd
use originally ). *When I run code below, I see that DynamicInvoke
walks the chain of delegates while Method->Invoke just invokes one
method.
>
Quote:
I suspect that implementation of DynamicInvoke just walks the chain
and calls Method->Invoke for each. *Is that a correct assumption? *If
DynamicInvoke does not incur any additional cost, it's probably the
way to go.
>
Yes, but... dynamic invocation is much more expensive.
>
The intended way to invoke a delegate is just
>
dl->Invoke(the parameters are typesafe here);
>
or even simpler,
>
dl(the parameters go here);
>
>
>
>
>
Quote:
Thanks!
vcq
>
Quote:
* * * *public delegate void FirstDelegate( int i );
* * * *public delegate void SecondDelegate( int i );
>
Quote:
void CommonMethod(System::Delegate^ test)
{
//!CommonCode goes here
>
Quote:
* * * * * * * * * * * * * //argumest to the invoked method (should be
passed in as well)
array<Object^>^ pArgs = gcnew array<Object^>(1);
pArgs[0] = 3;
>
Quote:
//dynamic invoke
test->DynamicInvoke( pArgs );
>
Quote:
//method->invoke
test->Method->Invoke( test->Target, pArgs );
>
Quote:
//walk the chain of delegates - same as DynamicInvoke ?
array<Delegate^>^ tests = test->GetInvocationList( );
for ( int i = 0; i < tests->Length; ++i )
{
tests[i]->Method->Invoke( test->Target, pArgs );
}
}- Hide quoted text -
>
- Show quoted text -
  #8  
Old November 18th, 2008, 01:25 AM
Ben Voigt [C++ MVP]
Guest
 
Posts: n/a

re: function pointer to a delegate


vcquestions wrote:
Quote:
in my example ( previous post ), where I pass delegates to
CommonMethod ( System::Delegate^ test), I can't just
call test->Invoke( params ) or test( params ) since compiler has no
knowledge of how to resolve this call ( any delegate could be passed )
so I have to pay price for late binding. ( Invoke is not even exposed
on the System::Delegate ). Please correct me if I'm wrong here.
>
That's why I was trying to figure out if there is a better way to
handle this situation ( have common code that invokes delegates with
the same signature - e.g., I can pass the whole object on which I
invoke a delegate and some param that I would switch on and do: obj-
Quote:
>Del1( ); or obj->Del2( ) ), but the switch statement does not really
help in making code clean. I guess there is no magic - the above 2
choices is all I have...
But the functions all have the same signature, you why are you using
System::Delegate? Use an exact delegate type and then early binding will
work.
Quote:
>
Thanks!
>
>
On Nov 17, 8:21 am, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:
Quote:
>vcquestions wrote:
Quote:
>>Ben, thanks for replying. I was trying to figure out a way to pass a
>>delegate as a parameter so that I could pass delegates with the same
>>signatures to a method that handles common code. This is straight
>>forward ( and does not require function pointers which I thought I'd
>>use originally ). When I run code below, I see that DynamicInvoke
>>walks the chain of delegates while Method->Invoke just invokes one
>>method.
>>
Quote:
>>I suspect that implementation of DynamicInvoke just walks the chain
>>and calls Method->Invoke for each. Is that a correct assumption? If
>>DynamicInvoke does not incur any additional cost, it's probably the
>>way to go.
>>
>Yes, but... dynamic invocation is much more expensive.
>>
>The intended way to invoke a delegate is just
>>
>dl->Invoke(the parameters are typesafe here);
>>
>or even simpler,
>>
>dl(the parameters go here);
>>
>>
>>
>>
>>
Quote:
>>Thanks!
>>vcq
>>
Quote:
>>public delegate void FirstDelegate( int i );
>>public delegate void SecondDelegate( int i );
>>
Quote:
>>void CommonMethod(System::Delegate^ test)
>>{
>>//!CommonCode goes here
>>
Quote:
>>//argumest to the invoked method (should be
>>passed in as well)
>>array<Object^>^ pArgs = gcnew array<Object^>(1);
>>pArgs[0] = 3;
>>
Quote:
>>//dynamic invoke
>>test->DynamicInvoke( pArgs );
>>
Quote:
>>//method->invoke
>>test->Method->Invoke( test->Target, pArgs );
>>
Quote:
>>//walk the chain of delegates - same as DynamicInvoke ?
>>array<Delegate^>^ tests = test->GetInvocationList( );
>>for ( int i = 0; i < tests->Length; ++i )
>>{
>>tests[i]->Method->Invoke( test->Target, pArgs );
>>}
>>}- Hide quoted text -
>>
>- Show quoted text -

  #9  
Old November 18th, 2008, 08:15 PM
vcquestions
Guest
 
Posts: n/a

re: function pointer to a delegate


I posted the answer yesterday - not sure where it is ( maybe I did a
reply to author )...

We do have delegates with the same signature:
public delegate void OnSecondEvent( int i );
public delegate void OnCommonEvent( int i );

void SameSignatureMethod( OnCommonEvent^ test)
{
//!CommonCode goes here
test->Invoke( 55 );
}

I incorrectly used c-style cast to pass an instance of delegate
( which causes an exception ):
SameSignatureMethod( OnCommonEvent^ )( m_pA->SecondHandler );

Using reinterpret_cast works fine.
SameSignatureMethod( reinterpret_cast<OnCommonEvent^>( m_pA-
Quote:
>SecondHandler ) );
Need to review my casting...

Thanks Ben!
vcq

On Nov 17, 6:20*pm, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:
Quote:
vcquestions wrote:
Quote:
in my example ( previous post ), where I pass delegates to
CommonMethod ( System::Delegate^ test), I can't just
call test->Invoke( params ) or test( params ) since compiler has no
knowledge of how to resolve this call ( any delegate could be passed )
so I have to pay price for late binding. *( Invoke is not even exposed
on the System::Delegate ). *Please correct me if I'm wrong here.
>
Quote:
That's why I was trying to figure out if there is a better way to
handle this situation ( have common code that invokes delegates with
the same signature - e.g., I can pass the whole object on which I
invoke a delegate and some param that I would switch on and do: obj-
Quote:
Del1( ); or obj->Del2( ) ), but the switch statement does not really
help in making code clean. *I guess there is no magic - the above 2
choices is all I have...
>
But the functions all have the same signature, you why are you using
System::Delegate? *Use an exact delegate type and then early binding will
work.
>
>
>
>
>
Quote:
Thanks!
>
Quote:
On Nov 17, 8:21 am, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:
Quote:
vcquestions wrote:
>Ben, thanks for replying. I was trying to figure out a way to pass a
>delegate as a parameter so that I could pass delegates with the same
>signatures to a method that handles common code. This is straight
>forward ( and does not require function pointers which I thought I'd
>use originally ). When I run code below, I see that DynamicInvoke
>walks the chain of delegates while Method->Invoke just invokes one
>method.
>
Quote:
Quote:
>I suspect that implementation of DynamicInvoke just walks the chain
>and calls Method->Invoke for each. Is that a correct assumption? If
>DynamicInvoke does not incur any additional cost, it's probably the
>way to go.
>
Quote:
Quote:
Yes, but... dynamic invocation is much more expensive.
>
Quote:
Quote:
The intended way to invoke a delegate is just
>
Quote:
Quote:
dl->Invoke(the parameters are typesafe here);
>
Quote:
Quote:
or even simpler,
>
Quote:
Quote:
dl(the parameters go here);
>
Quote:
Quote:
>Thanks!
>vcq
>
Quote:
Quote:
>public delegate void FirstDelegate( int i );
>public delegate void SecondDelegate( int i );
>
Quote:
Quote:
>void CommonMethod(System::Delegate^ test)
>{
>//!CommonCode goes here
>
Quote:
Quote:
>//argumest to the invoked method (should be
>passed in as well)
>array<Object^>^ pArgs = gcnew array<Object^>(1);
>pArgs[0] = 3;
>
Quote:
Quote:
>//dynamic invoke
>test->DynamicInvoke( pArgs );
>
Quote:
Quote:
>//method->invoke
>test->Method->Invoke( test->Target, pArgs );
>
Quote:
Quote:
>//walk the chain of delegates - same as DynamicInvoke ?
>array<Delegate^>^ tests = test->GetInvocationList( );
>for ( int i = 0; i < tests->Length; ++i )
>{
>tests[i]->Method->Invoke( test->Target, pArgs );
>}
>}- Hide quoted text -
>
Quote:
Quote:
- Show quoted text -- Hide quoted text -
>
- Show quoted text -
  #10  
Old November 18th, 2008, 11:15 PM
Ben Voigt [C++ MVP]
Guest
 
Posts: n/a

re: function pointer to a delegate




"vcquestions" <vcquestions@gmail.comwrote in message
news:a118bc29-cff6-47eb-a2dc-958dd30778c8@q26g2000prq.googlegroups.com...
Quote:
I posted the answer yesterday - not sure where it is ( maybe I did a
reply to author )...
>
We do have delegates with the same signature:
public delegate void OnSecondEvent( int i );
public delegate void OnCommonEvent( int i );
Why? Of course you will have many functions (methods) with the same
signature, but why would you need or want more than one delegate type with
that signature.

In this case, it would be best to simply use the Action<Tdelegate which
"Encapsulates a method that takes a single parameter and does not return a
value." and not create any delegate types at all.

http://msdn.microsoft.com/en-us/library/018hxwa8.aspx

Quote:
>
void SameSignatureMethod( OnCommonEvent^ test)
{
//!CommonCode goes here
test->Invoke( 55 );
}
>
I incorrectly used c-style cast to pass an instance of delegate
( which causes an exception ):
SameSignatureMethod( OnCommonEvent^ )( m_pA->SecondHandler );
>
Using reinterpret_cast works fine.
SameSignatureMethod( reinterpret_cast<OnCommonEvent^>( m_pA-
Quote:
>>SecondHandler ) );
>
Need to review my casting...
>
Thanks Ben!
vcq
>
On Nov 17, 6:20 pm, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:
Quote:
>vcquestions wrote:
Quote:
in my example ( previous post ), where I pass delegates to
CommonMethod ( System::Delegate^ test), I can't just
call test->Invoke( params ) or test( params ) since compiler has no
knowledge of how to resolve this call ( any delegate could be passed )
so I have to pay price for late binding. ( Invoke is not even exposed
on the System::Delegate ). Please correct me if I'm wrong here.
>>
Quote:
That's why I was trying to figure out if there is a better way to
handle this situation ( have common code that invokes delegates with
the same signature - e.g., I can pass the whole object on which I
invoke a delegate and some param that I would switch on and do: obj-
>Del1( ); or obj->Del2( ) ), but the switch statement does not really
help in making code clean. I guess there is no magic - the above 2
choices is all I have...
>>
>But the functions all have the same signature, you why are you using
>System::Delegate? Use an exact delegate type and then early binding will
>work.
>>
>>
>>
>>
>>
Quote:
Thanks!
>>
Quote:
On Nov 17, 8:21 am, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:
>vcquestions wrote:
>>Ben, thanks for replying. I was trying to figure out a way to pass a
>>delegate as a parameter so that I could pass delegates with the same
>>signatures to a method that handles common code. This is straight
>>forward ( and does not require function pointers which I thought I'd
>>use originally ). When I run code below, I see that DynamicInvoke
>>walks the chain of delegates while Method->Invoke just invokes one
>>method.
>>
Quote:
>>I suspect that implementation of DynamicInvoke just walks the chain
>>and calls Method->Invoke for each. Is that a correct assumption? If
>>DynamicInvoke does not incur any additional cost, it's probably the
>>way to go.
>>
Quote:
>Yes, but... dynamic invocation is much more expensive.
>>
Quote:
>The intended way to invoke a delegate is just
>>
Quote:
>dl->Invoke(the parameters are typesafe here);
>>
Quote:
>or even simpler,
>>
Quote:
>dl(the parameters go here);
>>
Quote:
>>Thanks!
>>vcq
>>
Quote:
>>public delegate void FirstDelegate( int i );
>>public delegate void SecondDelegate( int i );
>>
Quote:
>>void CommonMethod(System::Delegate^ test)
>>{
>>//!CommonCode goes here
>>
Quote:
>>//argumest to the invoked method (should be
>>passed in as well)
>>array<Object^>^ pArgs = gcnew array<Object^>(1);
>>pArgs[0] = 3;
>>
Quote:
>>//dynamic invoke
>>test->DynamicInvoke( pArgs );
>>
Quote:
>>//method->invoke
>>test->Method->Invoke( test->Target, pArgs );
>>
Quote:
>>//walk the chain of delegates - same as DynamicInvoke ?
>>array<Delegate^>^ tests = test->GetInvocationList( );
>>for ( int i = 0; i < tests->Length; ++i )
>>{
>>tests[i]->Method->Invoke( test->Target, pArgs );
>>}
>>}- Hide quoted text -
>>
Quote:
>- Show quoted text -- Hide quoted text -
>>
>- Show quoted text -
>
  #11  
Old November 19th, 2008, 02:25 AM
vcquestions
Guest
 
Posts: n/a

re: function pointer to a delegate


Why? Of course you will have many functions (methods) with the same
Quote:
signature, but why would you need or want more than one delegate type with
that signature.
purely out of curiousity :) - originally trying to figure out how to
pass delegates as parameters and then trying to figure out why I could
not pass 2 delegates with the same signature to the common handling
functions. As it turns out ( from this thread ), I can & in a few
different ways.
Quote:
In this case, it would be best to simply use the Action<Tdelegate
understood.

Thanks!


On Nov 18, 4:08*pm, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:
Quote:
"vcquestions" <vcquesti...@gmail.comwrote in message
>
news:a118bc29-cff6-47eb-a2dc-958dd30778c8@q26g2000prq.googlegroups.com...
>
Quote:
I posted the answer yesterday - not sure where it is ( maybe I did a
reply to author )...
>
Quote:
We do have delegates with the same signature:
public delegate void OnSecondEvent( int i );
public delegate void OnCommonEvent( int i );
>
Why? *Of course you will have many functions (methods) with the same
signature, but why would you need or want more than one delegate type with
that signature.
>
In this case, it would be best to simply use the Action<Tdelegate which
"Encapsulates a method that takes a single parameter and does not return a
value." and not create any delegate types at all.
>
http://msdn.microsoft.com/en-us/library/018hxwa8.aspx
>
>
>
>
>
Quote:
void SameSignatureMethod( OnCommonEvent^ test)
{
//!CommonCode goes here
test->Invoke( 55 );
}
>
Quote:
I incorrectly used c-style cast to pass an instance of delegate
( which causes an exception ):
SameSignatureMethod( OnCommonEvent^ )( m_pA->SecondHandler );
>
Quote:
Using reinterpret_cast works fine.
SameSignatureMethod( reinterpret_cast<OnCommonEvent^>( m_pA-
Quote:
>SecondHandler ) );
>
Quote:
Need to review my casting...
>
Quote:
Thanks Ben!
vcq
>
Quote:
On Nov 17, 6:20 pm, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:
Quote:
vcquestions wrote:
in my example ( previous post ), where I pass delegates to
CommonMethod ( System::Delegate^ test), I can't just
call test->Invoke( params ) or test( params ) since compiler has no
knowledge of how to resolve this call ( any delegate could be passed)
so I have to pay price for late binding. *( Invoke is not even exposed
on the System::Delegate ). *Please correct me if I'm wrong here.
>
Quote:
Quote:
That's why I was trying to figure out if there is a better way to
handle this situation ( have common code that invokes delegates with
the same signature - e.g., I can pass the whole object on which I
invoke a delegate and some param that I would switch on and do: obj-
Del1( ); or obj->Del2( ) ), but the switch statement does not really
help in making code clean. *I guess there is no magic - the above 2
choices is all I have...
>
Quote:
Quote:
But the functions all have the same signature, you why are you using
System::Delegate? *Use an exact delegate type and then early bindingwill
work.
>
Quote:
Quote:
Thanks!
>
Quote:
Quote:
On Nov 17, 8:21 am, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:
vcquestions wrote:
>Ben, thanks for replying. I was trying to figure out a way to passa
>delegate as a parameter so that I could pass delegates with the same
>signatures to a method that handles common code. This is straight
>forward ( and does not require function pointers which I thought I'd
>use originally ). When I run code below, I see that DynamicInvoke
>walks the chain of delegates while Method->Invoke just invokes one
>method.
>
Quote:
Quote:
>I suspect that implementation of DynamicInvoke just walks the chain
>and calls Method->Invoke for each. Is that a correct assumption? If
>DynamicInvoke does not incur any additional cost, it's probably the
>way to go.
>
Quote:
Quote:
Yes, but... dynamic invocation is much more expensive.
>
Quote:
Quote:
The intended way to invoke a delegate is just
>
Quote:
Quote:
dl->Invoke(the parameters are typesafe here);
>
Quote:
Quote:
or even simpler,
>
Quote:
Quote:
dl(the parameters go here);
>
Quote:
Quote:
>Thanks!
>vcq
>
Quote:
Quote:
>public delegate void FirstDelegate( int i );
>public delegate void SecondDelegate( int i );
>
Quote:
Quote:
>void CommonMethod(System::Delegate^ test)
>{
>//!CommonCode goes here
>
Quote:
Quote:
>//argumest to the invoked method (should be
>passed in as well)
>array<Object^>^ pArgs = gcnew array<Object^>(1);
>pArgs[0] = 3;
>
Quote:
Quote:
>//dynamic invoke
>test->DynamicInvoke( pArgs );
>
Quote:
Quote:
>//method->invoke
>test->Method->Invoke( test->Target, pArgs );
>
Quote:
Quote:
>//walk the chain of delegates - same as DynamicInvoke ?
>array<Delegate^>^ tests = test->GetInvocationList( );
>for ( int i = 0; i < tests->Length; ++i )
>{
>tests[i]->Method->Invoke( test->Target, pArgs );
>}
>}- Hide quoted text -
>
Quote:
Quote:
- Show quoted text -- Hide quoted text -
>
Quote:
Quote:
- Show quoted text -- Hide quoted text -
>
- Show quoted text -
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with construction via a function pointer to a local method Peter Oliphant answers 6 November 17th, 2005 05:35 PM
function pointer to delegate Abubakar answers 0 November 17th, 2005 04:04 PM
Marshalling a function pointer to a delegate without .NET 2.0 Jason Bell answers 2 November 17th, 2005 12:04 AM