Please ignore whether a pattern like this is actually a good idea or not. The rationale was that I wanted to catch an error that was thrown, or a mismatch in the number of records inserted / updated / deleted. I didn't want to repeat this logic, and at the time this customisation felt like it was going to apply to no more than about four long "script" methods.
My first step was to use an anonymous function.
public void DoSqlAction(Func<bool> f, string task, string ctx, ref bool cont, List<ResultInfo> resultInfo) {
if (cont) {
bool ret = false;
try {
if (f.Invoke()) {
resultInfo.Add(new ResultInfo(seq, task, "Success", ctx, true));
cont = true;
} else {
resultInfo.Add(new ResultInfo(seq, task, "Fail", ctx, false));
cont = false;
}
} catch (Exception ex) {
resultInfo.Add(new ResultInfo(seq, task, "Error: " + ex.Message, ctx, false));
cont = false;
}
}
}
if I try to use this:
DoSqlAction(() => 1 == cx.Execute(someSql, anonymousTypeWithClassInstanceInside), "add item", refinfo.ToString(),ref cont, resultInfo);
anonymousTypeWithClassInstanceInside <-- source of the error
The error comes up:
Cannot use ref or out parameter 'abc' inside an anonymous method, lambda expression, or query expression
The solution is to get rid of the delegate Func<bool> f
. I'm writing this entry (perhaps it should be a blog post?) because it felt that the compile-time error gets generated is a bit of a road block.
In this post, I discovered a link to Eric's article:
C# Cannot use ref or out parameter inside an anonymous method body
here
After seeing how foreach gets implemented, I was lead to think... hmmm... maybe I'm reaching for customisable syntactic sugar.
Is this possible now in C#4? Will it be possible in C#5? It makes me consider looking into http://nemerle.org at some point in the future, but I'd really like to stay within C#.