c#定义方法,该方法以不同的重载方式接收方法作为参数

时间:2018-10-19 15:40:52

标签: c#

我在“ IMyInterface Builder”上有几种扩展方法,它们的功能几乎相同,但是我想简化代码。

builder.Print(...)有4个重载。在这里,我仅将2个重载为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);
    }

是否可以编写这样的伪代码来简化ProcessOne和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);
    }

基本上我希望能够将不同的重载(builder.Print)作为参数传递

在这里可以应用的最佳技术是什么?

编辑:标记为重复的问题,但不同的重载未解决。

0 个答案:

没有答案