Question

This should be a quick one. Is it possible to do something like this:

[Callback(funtionY)]
void functionX()
{
}

void functionY()
{
}

So that when functionX is called, functionY is called automatically too? Reason I ask is because I'm going to implement networking functionality to a small XNA based game engine, and I want to mark functions as Synched, to mark that when a function gets called it should be called across all clients.

Thanks.

Was it helpful?

Solution

Sounds like you want Aspect-Oriented Programming.

I've used PostSharp for this before now, and it works pretty well.

OTHER TIPS

You can probably do that quite easily with PostSharp, but in general: no; you'd have to call it explicitly.

Yes, you can, just create them as a delegate.

Action foo = functionX;
foo += functionY;

foo(); // both called

UPDATE: Jon (thanks) pointed out that invocation order is in fact determined. I would NEVER rely on this however. See comments.

Is there some reason you can't do this:

void functionX()
{
    functionY();
    // ... etc
}

Based on the description in the question, that's a valid answer. I assume that you've considered it already, though.

Another possibility would be to use the eventing system to your advantage, something like (uncompiled):

event Action FunctionXCalled;

// somewhere in initialization...
MyCtor()
{
    FunctionXCalled += functionY;
}

void functionY() { }

void functionX()
{
    if(FunctionXCalled != null) FunctionXCalled();
    // ... etc
}

how about:

void functionX() {
   functionY();
   // Other implementation stuff
}

void functionY() {

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top