简体   繁体   中英

How to combine two methods with slightly different signatures?

Consider the following 2 methods. The body is identical - the only difference is the 2nd parameter (Rectangle vs RectangleF) to the methods. Is there a way to combine these 2 methods into 1 without needlessly complicating the parameter list:

public static void DrawRectangles(this Image thisImage, List<RectangleF> rectangles, Color color) {
  using (var g = Graphics.FromImage(thisImage)) {
    var brush = new SolidBrush(color);
    g.FillRectangles(brush, rectangles.ToArray());
  }
}

public static void DrawRectangles(this Image thisImage, List<Rectangle> rectangles, Color color) {
  using (var g = Graphics.FromImage(thisImage)) {
    var brush = new SolidBrush(color);
    g.FillRectangles(brush, rectangles.ToArray());
}

This is the best that I think you can do to reduce code duplication:

private static void DrawRectangles<T>(this Image thisImage, List<T> rectangles, Color color, Action<Graphics, Brush, T[]> fill)
{
    using (var g = Graphics.FromImage(thisImage))
    {
        var brush = new SolidBrush(color);
        fill(g, brush, rectangles.ToArray());
    }
}

public static void DrawRectangles(this Image thisImage, List<RectangleF> rectangles, Color color)
{
    thisImage.DrawRectangles(rectangles, color, (g, b, rs) => g.FillRectangles(b, rs));
}

public static void DrawRectangles(this Image thisImage, List<Rectangle> rectangles, Color color)
{
    thisImage.DrawRectangles(rectangles, color, (g, b, rs) => g.FillRectangles(b, rs));
}

You can try to use RectangleF.Implicit(Rectangle to RectangleF) Operator , let Rectangle to RectangleF then pass be the parameter.

https://docs.microsoft.com/en-us/dotnet/api/system.drawing.rectanglef.op_implicit?redirectedfrom=MSDN&view=netframework-4.7.2

public static void DrawRectangles(this Image thisImage, List<RectangleF> rectangles, Color color)
{
    using (var g = Graphics.FromImage(thisImage))
    {
        var brush = new SolidBrush(color);
        g.FillRectangles(brush, rectangles.ToArray());
    }
}

public static void DrawRectangles(this Image thisImage, List<Rectangle> rectangles, Color color)
{
    var rectangleFs = rectangles.Select(x => (RectangleF) x).ToList();
    DrawRectangles(thisImage, rectangleFs, color);
}

You could define a generic method which takes a generic parameters as a IEnumerable.

We then cast is as an object and gets his type either Rectangle or RectangleF. Based on that logic we then Draw the Rectangle.

    public static void DrawRectangles<T>(this Image thisImage, T rectangles, Color color) where T : IEnumerable
    {
        using (var g = Graphics.FromImage(thisImage))
        {
            var brush = new SolidBrush(color);
            if (rectangles.Cast<object>().FirstOrDefault().GetType() == typeof(Rectangle))
            {
                g.FillRectangles(brush, rectangles.Cast<Rectangle>().ToArray());
            }
            else
            {
                g.FillRectangles(brush, rectangles.Cast<RectangleF>().ToArray());
            }
        }
    }

    public static void Main(string[] args)
    {
        List<Rectangle> r = new List<Rectangle>();
        List<RectangleF> rf = new List<RectangleF>();

        r.Add(new Rectangle(10, 10, 10, 10));
        rf.Add(new RectangleF(10.4f, 10.4f, 10.4f, 10.4f));
        DrawRectangles(new Bitmap(10, 10), rf, Color.Aqua);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM