0

I have several extensions methods over "IMyInterface builder" that do pretty much the same, but I would like to simplify code.

builder.Print(...) have 4 overloads.Here I just put 2 to simplifly

    public static void ProcessOne(this IMyInterface builder, Source sourceid, Exception exception, string message, IList<string> myList)
    {
        DoStuff(myList);
        builder.Print(sourceid, exception, message);
        DoAnotherStuff(myList);
    }

    public static void ProcessTwo(this IMyInterface builder, Source sourceid, string message, IList<string> myList)
    {
        DoStuff(myList);
        builder.Print(sourceid, message);
        DoAnotherStuff(myList);
    }

Is possible to write something like this pseudo code, to simplify ProcessOne and ProcessTwo?

    public static void ProcessOne(this IMyInterface builder, Source sourceid, Exception exception, string message, IList<string> myList)
    {
        SpecialMethod(builder.Print(sourceid, exception, message), myList)
    }
    public static void ProcessTwo(this IMyInterface builder, Source sourceid, string message, IList<string> myList)
    {
        SpecialMethod(builder.Print(sourceid, message), myList)
    }       

    private static void SpecialMethod(???builder.Print???, IList<string> myList)
    {
            DoStuffOne(myList);
            ???builder.Print???(with specific overloads)
            DoAnotherStuff(myList);
    }

Basically I want to be able to pass different overloads (builder.Print) as a parameter

What is the best technique we can apply here?

EDIT: question marked as duplicated, but different overloads is not addressed.

inesmar
  • 99
  • 2
  • 7
  • 1
    One thing the flagged duplicate won't address in OP's question is the fact that OP wants to be able to pass different overloads. This is more complicated as using delegates or the .Net wrappers of Func, Action and Predicate. Mean you have to have a matching method signature. Ie same return type and same number of type of parameters, meaning you can't pass overloads – Dave Oct 19 '18 at 16:07
  • 1
    OP because I can't answer this question now it has been flagged a duplicate as long as you know the arguments you want to pass to your function at the time of called the 'Special Method' you can take advantage of something called a closure and do something like this `SpecialMethod(() => build.Printer(arg1, arg2, etc...), mylist)` and the signature of special method would be `SpecialMethod(Action print, IListmy list)` and then inbetween DoStuffOne and DoAnother stuff you called your print action like this `Print()` – Dave Oct 19 '18 at 16:15
  • Thanks for your time @Dave. Yes I need to pass differents overloads. will dig and check the suggested 'closure'. Thanks – inesmar Oct 19 '18 at 22:53
  • 1
    @Dave, Thanks a lot. it worked! – inesmar Oct 21 '18 at 18:07

0 Answers0