Without building and rewriting IL, there is no way to create a custom attribute that modifies the code as you describe.
I suggest you use the delegate-based approach instead, for example. for functions of one argument:
static Func<TArg,T> WrapAgainstReentry<TArg,T>(Func<TArg,T> code, Func<TArg,T> onReentry) { bool entered = false; return x => { if (entered) return onReentry(x); entered = true; try { return code(x); } finally { entered = false; } }; }
This method transfers the function for wrapping (provided that it corresponds to Func <TArg, T> - you can write other options or a completely general version with great effort) and an alternative function for calling in cases of re-entry. (An alternative function may throw an exception or return immediately, etc.). Then, in all the code where you usually call the passed method, you call the delegate returned by WrapAgainstReentry ().
Barry kelly
source share