开发者

Suppress an attribute for a single method at compile time?

I'm using PostSharp, and I'd like to suppress (or change) an existing global attribute, for one method in the class.

In the example below, I want the class "thisIsLogged()" to be logged, and the class "thisIsNotLogged()" to not be logged.

However, it doesn't work: the attribute "[LogThis(false)]" simply adds to the existing class level attribute, and logging occurs anyway. Any ideas?

[LogThis(true)] // uses PostSharp + SmartInspect to switch on logging for the entire class
class doSomething
{
  void thisIsLogged(int x)
  {
     // entry/exit from this class is automatically logged
  }
  [LogThis(false)] // aim: suppress logging for this method, if [LogThis(true)] is switched on for the entire class (however, this doesn't work as attributes are additive)
  void thisIsNotLogged(int x)
  {
     // I want to suppress the entry/exit logging for this class, t开发者_如何学Pythono reduce log size
     // However, this *doesnt work*; logging occurs anyway
     // as attributes are additive - any ideas?
  }
}

Edit:

Used [LogThis(AttributeExclude=true)], this worked fine (see solution below).


Consider using a MethodPointcut, as Gael kindly suggested I use when I had a similar question. This gives you a lot of flexibility to decide which methods to augment with aspects, including inspection of Attributes.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜