(请原谅我对“DoListAction”等名称的错误选择):
readonly Action<Process> removeBreakpoint = p => p.RemoveBreakpoint();
readonly Action<Process> addBreakpoint = p => p.AddBreakpoint();
void DoSingleAction(Action<Process> action)
{
var p = GetProcess(viewer);
if(p != null)
{
action(p); //invokes the action
BeginRefresh(null,null);
}
}
void DoListAction(Action<Process> action)
{
lvwProcessList.ForEach(action);
BeginRefresh(false, false);
}
private void ctxgrphAddBreakpoint_Click(object sender, EventArgs e)
{
DoSingleAction(addBreakpoint);
}
private void ctxgrphRemoveBreakpoint_Click(object sender, EventArgs e)
{
DoSingleAction(removeBreakpoint);
}
private void ctxlistAddBreakpoint_Click(object sender, EventArgs e)
{
DoListAction(addBreakpoint);
}
private void ctxlistRemoveBreakpoint_Click(object sender, EventArgs e)
{
DoListAction(removeBreakpoint);
}
void DoAction(object sender, Action<Process>)
{
if(sender is ListView)
{
lvwProcessList.ForEach(action);
BeginRefresh(false, false);
}
else if (sender is GraphView)
{
var p = GetProcess(viewer);
if(p != null)
{
action(p); //invokes the action
BeginRefresh(null,null);
}
}
else {throw new Exception("sender is not ListView or GraphView");}
}
//and update all the handlers to use this, and delete DoSingleAction and DoListAction:
private void ctxgrphAddBreakpoint_Click(object sender, EventArgs e)
{
DoAction(sender, addBreakpoint);
}
//etc.
无论如何,在每个事件处理程序中,我认为你必须指定要采取的行动。我不确定在这种情况下继承或扩展方法是否真的是你的朋友,但使用扩展方法可能会是这样:
//these two are in a separate static class
public static InvokeAction(this ListView listView, Action<Process> action)
{ ... }
public static InvokeAction(this GraphView listView, Action<Process> action)
{ ... }
private void handler(object sender, Action<Process> action)
{
var lv = sender as ListView;
var gv = sender as GraphVeiw;
lv.InvokeAction(action); //(need to check for null first)
gv.InvokeAction(action);
if(lv == null && gv == null) {throw new Exception("sender is not ListView or GraphView");}
}
//then update all the handlers:
private void ctxgrphAddBreakpoint_Click(object sender, EventArgs e)
{
handler(sender, addBreakpoint);
}
C#2/。NET 2.0
在C#2中(VS2005),使用委托的第一种方式仍然有效,只是不能使用lambdas。(2.0有
Action<T>
void removeBreakpoint(Process p) { p.RemoveBreakpoint(); }
void addBreakpoint(Process p) { p.AddBreakpoint(); }
//you could also do this, but it's overkill:
readonly Action<Process> removeBreakpoint = delegate(Process p) { p.RemoveBreakpoint(); };
readonly Action<Process> addBreakpoint = delegate(Process p) { p.AddBreakpoint(); };