我正在尝试将一些基于接口的抽象改进为遗留代码,作为依赖注入的初步步骤。遗留代码包含我正在努力封装的lambda用法。这是现有的lambda用法:
private void MethodAaa(EntityA a, EntityB a, int someInt) {...}
private void MethodBbb(DateTime date, EntityA e) {...}
_commandObjectFromThirdPartyLibrary.Execute(() => MethodAaa(a, b, c));
_commandObjectFromThirdPartyLibrary.Execute(() => MethodBbb(d, e));
我希望通过公共基类方法路由lamda执行,如下所示:
base.CommonExecute( () => MethodAaa(a, b, c) );
base.CommonExecute( () => MethodBbb(d, e) );
base.CommonExecute( Action<???> lamdaExpression )
{
_commandObjectFromThirdPartyLibrary.Execute( lamdaExpression );
}
有人可以提供一个如何正确声明base.CommonExecute(?)的示例吗?
答案 0 :(得分:5)
我认为使用non-generic version of the Action delegate:
没有任何问题base.CommonExecute(Action lambdaExpression )
{
_commandObjectFromThirdPartyLibrary.Execute( lambdaExpression );
}
答案 1 :(得分:0)
Action的非通用版本是void-void one:
public delegate void Action();