Does the infrastructure have special api for detecting reentration?

I want to prevent re-installation for a large set of methods.

for one method, this code works:

bool _isInMyMethod; void MyMethod() { if (_isInMethod) throw new ReentrancyException(); _isInMethod = true; try { ...do something... } finally { _isInMethod = false; } } 

It is tiring to do this for each method.
Therefore, I used the StackTrace class:

  public static void ThrowIfReentrant() { var stackTrace = new StackTrace(false); var frames = stackTrace.GetFrames(); var callingMethod = frames[1].GetMethod(); if (frames.Skip(2).Any( frame => EqualityComparer<MethodBase>.Default.Equals(callingMethod,frame.GetMethod()))) throw new ReentrancyException(); } 

It works great, but looks more like a hack.

Is there a special API in the .NET Framework for detecting redeployment?

+6
source share
2 answers

I recommend using PostSharp to solve your problem. Although it might be expensive to use a commercial tool only for a specific reason, I suggest you take a look, as this tool can solve other problems that are better suited for solving AOP (logging, transaction management, security, etc.). The company website is here , and you can take a look at examples here . Pluralsight has a good course on AOP methodology with examples at PostSharp here . Good luck

+3
source

The usual .Net approach is to use some for synchronization to block other threads, instead of causing them to throw an exception.

+1
source

All Articles