UPDATE . I was a bit obscure from your question, if the arg argument was used elsewhere in the method, it doesn't look like it, so you can just take Action and use lambda to specify the delegate to call with the argument captured:
protected void SaveChanges<T, TArg>(T mlaObject, TArg arg, Action undoFunction) where T : WebObject { try { this._db.SaveChanges(); } catch (Exception e) { Console.WriteLine("Error: " + e); undoFunction(); } }
To which you can pass:
SaveChanges(article, () => article.Authors.Remove(person));
Or, if he himself is myObj, and in this case (as six variable variables have already been indicated), you can simply pass it back to the delegate according to its code.
Or, if arg is different from mlaObject, and you want to do other things on it in the code too, in which case you could do:
protected void SaveChanges<T, TArg>(T mlaObject, TArg arg, Action undoFunction) where T : WebObject { try { this._db.SaveChanges(); } catch (Exception e) { Console.WriteLine("Error: " + e); undoFunction(arg); } }
And then:
SaveChanges(article, person, article.Authors.Remove);
James Michael Hare
source share