_handler.Invoke ???

_handler.Invoke ???

Old forum URL: forums.lhotka.net/forums/t/2790.aspx


csm101 posted on Thursday, April 26, 2007

I'm not clear about something.  In the RuleMethod class there is a method called Invoke:

 

public bool Invoke(T target)

{

    return _handler.Invoke(target, _args);

}

This supposedly calls an "Invoke" method on the _handler delegate, which is a RuleHandler delegate.  My question is: what "Invoke" method is _handler.Invoke calling?  Is this from reflection?  Is this some other way of invoking a delegate?   Not sure what's going on here.

 

 

Thanks.

 

 

ajj3085 replied on Friday, April 27, 2007

_handler is likely a reference to a MethodInfo object.  Invoke actually invokes whatever method is represented by the object on the instance target with the given args.

csm101 replied on Friday, April 27, 2007

But _handler is *not* a MethodInfo object.  It's a delegate object.  I know this code directly steps into the validation rule method defined in the target object because I've stepped through the code.  I'm just not sure what .NET machenery is going on here.  Where is this Invoke method from?

 

RockfordLhotka replied on Friday, April 27, 2007

Originally in .NET, to invoke a delegate you had to call delegate.Invoke().

Later Microsoft added some syntactic sugar in the compilers so you can do delegate() directly.

Parts of CSLA are very old, and parts are newer. The rule handling stuff is pretty new. But my habits are also often somewhat old, and one of them is to explicitly call Invoke() Smile [:)]

Personally I like the explicit Invoke() syntax, because it makes it clear that I'm invoking a delegate. Not that I'm adverse to syntactic sugar - far from it - but there are places where I prefer more explicit syntax because I think it adds clarity of intent.

Another is with interface implementations. I much prefer VB's more verbose/explicit syntax through the Implements clause, because you can look at a method and know whether it implements an interface method or not. C#'s syntax is more compact, but you don't know whether a method implements an interface method until you change it and all of a sudden your code can't compile...

csm101 replied on Friday, April 27, 2007

Thanks Rockford! 

That clears things up.  I was searching all over for delegate.Invoke() in .NET 2.0 and couldn't find any such method.  I wonder why they didn't deprecate this syntax?  I think this is the syntax in J#, though.

RockfordLhotka replied on Friday, April 27, 2007

Remember, the compiler always uses this syntax. Calling a delegate directly is merely a compiler trick. So they really can’t get rid of the Invoke() syntax, because that’s the real syntax – everything else is just illusion.

 

And if they did try to get rid of it in the compiler, they’d both break a lot of code, and they’d find people like me arguing that it should remain because it adds clarity to code J

 

Rocky

 

 

From: csm101 [mailto:cslanet@lhotka.net]
Sent: Friday, April 27, 2007 10:11 AM
To: rocky@lhotka.net
Subject: Re: [CSLA .NET] _handler.Invoke ???

 

Thanks Rockford! 

That clears things up.  I was searching all over for delegate.Invoke() in .NET 2.0 and couldn't find any such method.  I wonder why they didn't deprecate this syntax?  I think this is the syntax in J#, though.



Copyright (c) Marimer LLC