camelCase
camelCase

Reputation: 1780

Method parameter type to pass a lambda expression prior to execute

I am trying to retrofit some interface based abstraction to legacy code as a preliminary step for Dependency Injection. The legacy code contains lambda usage that I am struggling to encapsulate. Here is the existing lambda usage:

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));

I wish to route the lamda execution via a common base class method as follows:

base.CommonExecute( () => MethodAaa(a, b, c) );
base.CommonExecute( () => MethodBbb(d, e) );

base.CommonExecute( Action<???> lamdaExpression )
{
    _commandObjectFromThirdPartyLibrary.Execute( lamdaExpression );
}

Can someone provide an example of how to declare base.CommonExecute(?) properly?

Upvotes: 4

Views: 1279

Answers (2)

Alexei Levenkov
Alexei Levenkov

Reputation: 100547

Non generic version of Action is void-void one:

public delegate void Action();

Upvotes: 0

Steve Danner
Steve Danner

Reputation: 22158

I don't see anything wrong with using the non-generic version of the Action delegate:

base.CommonExecute(Action lambdaExpression )
{
    _commandObjectFromThirdPartyLibrary.Execute( lambdaExpression );
}

Upvotes: 5

Related Questions