I’m writing a simple game in XNA and I’ve faced a problem with delegates. I use them to represent physics in the game, e.g.:
public delegate void PhysicsLaw(World world);
//for gravitation
static public void BallLawForGravity(World world)
{
if (world.ball.position.Y != world.zeroLevel)
//v(t) = v0 + sqrt(c * (h - zeroLevel))
world.ball.speed.Y += (float)Math.Sqrt(0.019 * (world.zeroLevel - world.ball.position.Y));
}
And I want to create multicast delegates for different objects/circumstances consisting from many methods such as BallLawForGravity(), but I can do it only like this:
processingList = BallLawForBounce;
processingList += BallLawForAirFriction;
...
processingList += BallLawForGravity;
Obviously, it doesn’t look good. Is there any standard way to create a multicast delegate from collection of method names?
Use the static method
Delegate.Combine Method (Delegate[])for such tasks.More examples.
Update
You can use the creating delegate via Reflection for this but I don’t recommend it, because it’s very slow technic.