Can I add an attribute to a function to prevent reentry?

前端 未结 9 2357
梦谈多话
梦谈多话 2021-02-15 12:26

At the moment, I have some functions which look like this:

private bool inFunction1 = false;
public void function1()
{
    if (inFunction1) return;
    inFunctio         


        
9条回答
  •  清酒与你
    2021-02-15 12:59

    This thread is a bit old but I figured it'd be worth bring it into 2012 because this problem still exists (more or less). I was able to solve this problem using proxy objects generated using Reflection.Emit (specifically using LinFu.DynamicProxy). The LinFu article is older than this article so I assume everything it discusses was relevant when it was asked (and yet somehow still today).

    I used LinFu because I was already using it for other purposes, but I'm sure some of the other DynamicProxy frameworks available would work for you (e.g. Castle.DynamicProxy) or you could roll your own based on Reflection.Emit (not for those with weak dispositions). They provide a mechanism that fills a large part of AOP's role while keeping you in control of your code.

提交回复
热议问题