I have implemented the following functionality to determine if two shapes are intersecting. I use the following IsOverlapping extension method, which uses dynamic to call the appropriate overloaded IsOverlappingSpecialisation method at runtime. I believe this is called double dispatching.

This is redundant (as I will need to do this for every new shape created). Is there a way I could possibly get around this? I thought of passing in a Tuple parameter into IsOverlapping, but I still have problems. Essentially I want overloading to occur based on unique unordered parameter sets (I know this is not possible, so looking for a workaround).

This just stores delegates in a Dictionary and tries to get a matching one when you call IsOverlapping on a Shape.

You use it like this:

public class Program
{
public static void Main()
{
// Add the calculation algorithm defined below.
OverlapCalculator.AddCalculation<Rectangle, Triangle>(IsOverlapping);
var rect = new Rectangle();
var triangle = new Triangle();
var circle = new Circle();
// These will work since we have a two way calculation for Rectangle and Triangle
rect.IsOverlapping(triangle);
triangle.IsOverlapping(rect);
// This will throw since we have no calculation between Circle and Triangle.
circle.IsOverlapping(triangle);
}
private static bool IsOverlapping(Rectangle rectangle, Triangle triangle)
{
// Do specialised geometry
return true;
}
}

This should be a neat and fast (no reflection) solution to your problem.

One drawback with this solution is that you have to "declare" the calculation methods using the AddCalculation method.