[英]Refactor methods invoking delgates with different parameter signatures
Is there a pattern that I could apply to refactor this code? 是否可以使用某种模式来重构此代码? The only difference between the two methods is that that one method takes an extra parameter and passes it to the delegate?
两种方法之间的唯一区别是,一个方法需要一个额外的参数并将其传递给委托?
I found out that delegates cannot take overloaded method signatures. 我发现委托不能接受重载的方法签名。 How could I add one more level of indirection?
我如何再添加一个间接级别? :)
:)
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
}
}
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));
No, the code you posted is actually a very terse method of applying this patter. 不,您发布的代码实际上是应用此模式的一种非常简洁的方法。 You're not going to find a terser way unless there is some way of abstracting the type of method being called, or by removing all type safety, which I would not recommend
除非有某种方法可以抽象化被调用方法的类型,或者通过消除所有类型安全性,否则您将不会找到更简单的方法,我不建议这样做
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);
}
}
You could use anonymous methods or lambdas like this: 您可以使用匿名方法或lambda:
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);
}
}
and then call the method like this 然后像这样调用方法
Process(contactFolder => MyMethod(esb, contactFolder));
Process(contactFolder => MyMethod(esb, contactFolder, contact));
Where MyMethod is the actual method you're calling, so you contain it within your lambda expression rather than a delegate. MyMethod是您要调用的实际方法,因此您将其包含在lambda表达式中,而不是委托中。 Think something like that may work?
认为类似的方法可能有效?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.