C# 基于无序参数集的重载方法
我有以下形状层次结构:C# 基于无序参数集的重载方法,c#,.net,overloading,dynamictype,C#,.net,Overloading,Dynamictype,我有以下形状层次结构: public abstract class Shape { ... } public class Rectangle : Shape { ... } public class Circle : Shape { ... } public class Triangle : Shape { ... } 我实现了以下功能来确定两个形状是否相交。我使用以下IsOverlapping扩展方法,它使用dynamic在运行时调用适当的重载isoverlappingspeciali
public abstract class Shape
{ ... }
public class Rectangle : Shape
{ ... }
public class Circle : Shape
{ ... }
public class Triangle : Shape
{ ... }
我实现了以下功能来确定两个形状是否相交。我使用以下IsOverlapping
扩展方法,它使用dynamic
在运行时调用适当的重载isoverlappingspecialization
方法。我相信这就是所谓的双重派遣
static class ShapeActions
{
public static bool IsOverlapping(this Shape shape1, Shape shape2)
{
return IsOverlappingSpecialisation(shape1 as dynamic, shape2 as dynamic);
}
private static bool IsOverlappingSpecialisation(Rectangle rect, Circle circle)
{
// Do specialised geometry
return true;
}
private static bool IsOverlappingSpecialisation(Rectangle rect, Triangle triangle)
{
// Do specialised geometry
return true;
}
这意味着我可以执行以下操作:
Shape rect = new Rectangle();
Shape circle = new Circle();
bool isOverlap = rect.IsOverlapping(circle);
我现在面临的问题是,我还必须在ShapeActions
中为circle.IsOverlapping(rect)
实现以下功能:
private static bool IsOverlappingSpecialisation(Circle circle, Rectangle rect)
{
// The same geometry maths is used here
return IsOverlappingSpecialisation(rect, circle);
}
这是多余的(因为我需要为创建的每个新形状执行此操作)。有什么办法可以让我绕过这件事吗?我曾想过将
Tuple
参数传递到IsOverlapping
,但仍然存在问题。本质上,我希望基于唯一的无序参数集发生重载(我知道这是不可能的,所以正在寻找解决方法)。我可能在这里将事情过度复杂化,但它可以
public static class OverlapCalculator
{
private static readonly Dictionary<Tuple<Type, Type>, Delegate> Calculations = new Dictionary<Tuple<Type, Type>, Delegate>();
public static bool IsOverlapping<TShape, TOtherShape>(this TShape shape, TOtherShape otherShape)
where TShape : Shape
where TOtherShape : Shape
{
var calculation = GetCalculationDelegate<TShape, TOtherShape>();
if (calculation != null)
{
return calculation(shape, otherShape);
}
throw new InvalidOperationException(string.Format("Could not find calculation for {0} and {1}", typeof(TShape).Name, typeof(TOtherShape).Name));
}
public static void AddCalculation<TShape, TOtherShape>(Func<TShape, TOtherShape, bool> calculation)
where TShape : Shape
where TOtherShape : Shape
{
var key = new Tuple<Type, Type>(typeof(TShape), typeof(TOtherShape));
Calculations[key] = calculation;
var reverseKey = new Tuple<Type, Type>(typeof(TOtherShape), typeof(TShape));
var reverseCalculation = new Func<TOtherShape, TShape, bool>((otherShape, shape) => calculation(shape, otherShape));
Calculations[reverseKey] = reverseCalculation;
}
private static Func<TShape, TOtherShape, bool> GetCalculationDelegate<TShape, TOtherShape>()
{
var key = new Tuple<Type, Type>(typeof(TShape), typeof(TOtherShape));
Delegate calculationDelegate;
if (Calculations.TryGetValue(key, out calculationDelegate))
{
return (Func<TShape, TOtherShape, bool>) calculationDelegate;
}
return null;
}
}
这应该是一个简洁、快速(无反射)的解决方案
此解决方案的一个缺点是,您必须使用
AddCalculation
方法“声明”计算方法。您看到了吗?只是出于兴趣,为什么您在这里使用dynamic
而不只是打开类型?(或提供方法重写?)因为我的类型被引用为Shape
。我需要运行时对象类型来分派正确的方法。
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;
}
}