重构方法使用不同的参数签名调用委托

时间:2009-08-20 21:24:34

标签: c# refactoring delegates

我是否有可以应用于重构此代码的模式?这两种方法的唯一区别是,一个方法需要一个额外的参数并将其传递给委托?

我发现代理人不能接受重载方法签名。我怎么能再增加一个间接级别? :)

public static void ProcessFolder(
    ProcessFolderDelegate processFolderDelegate
)
{
    using (var esb = ExchangeService.GetExchangeServiceBinding())
    {
        var contactFolder = FolderService.GetPublicFolder(esb,  
            Properties.Settings.Default.ExchangePublicFolderName);
        processFolderDelegate(esb, contactFolder);
    }
}

public static void ProcessContact(  
    ProcessContactDelegate processContactDelegate,  
    Contact contact  //extra param
)
{
    using (var esb = ExchangeService.GetExchangeServiceBinding())
    {
        var contactFolder = FolderService.GetPublicFolder(esb,  
            Properties.Settings.Default.ExchangePublicFolderName);
        processContactDelegate(esb, contactFolder, contact); //extra param
    }
}

4 个答案:

答案 0 :(得分:3)

    public delegate void Action(TYPE_OF_ESB esb, TYPE_OF_CONTACT_FOLDER contact folder);
    private static void Process(Action action)
    {
        using (var esb = ExchangeService.GetExchangeServiceBinding())
        {
            var contactFolder = FolderService.GetPublicFolder(esb, Properties.Settings.Default.ExchangePublicFolderName);
            action(esb, contactfolder);
        }
    }

Process((esb, contactfolder)=>processFolderDelegate(esb, contactFolder));
Process((esb, contactfolder)=>processContactDelegate(esb, contactFolder, contact));

答案 1 :(得分:1)

不,你发布的代码实际上是应用这种模式的一种非常简洁的方法。除非有某种方法可以抽象所调用的方法类型,或者删除所有类型的安全性,否则你不会找到一种方法。我不建议这样做

答案 2 :(得分:1)

public static void ProcessFolder(ProcessFolderDelegate del)
{
    Process((b, f) => del(b, f));
}

public static void ProcessContact(ProcessContactDelegate del, Contact contact)
{
    Process((b, f) => del(b, f, contact));
}

private static void Process(
    Action<ExchangeServiceBinding, ContactsFolderType> action)
{
    // i've guessed that esb is of type ExchangeServiceBinding
    // and that contactFolder is of type ContactsFolderType
    // if that's not the case then change the type parameters
    // of the Action delegate in the method signature above

    using (var esb = ExchangeService.GetExchangeServiceBinding())
    {
        var contactFolder = FolderService.GetPublicFolder(
            esb, Properties.Settings.Default.ExchangePublicFolderName);
        action(esb, contactFolder);
    }
}

答案 3 :(得分:0)

你可以使用这样的匿名方法或lambdas:

delegate void ProcessDelegate<T>(T param);
.
.
public static void Process<T>(ProcessDelegate<T> processDelegate)
{
    using (var esb = ExchangeService.GetExchangeServiceBinding())
    {
        var contactFolder = FolderService.GetPublicFolder(esb,  
            Properties.Settings.Default.ExchangePublicFolderName);
        processDelegate(contactFolder);
    }
}

然后调用这样的方法

Process(contactFolder => MyMethod(esb, contactFolder));
Process(contactFolder => MyMethod(esb, contactFolder, contact));

MyMethod是你正在调用的实际方法,所以你在lambda表达式而不是委托中包含它。想想这样的事可能有用吗?