How to do something automatic before call a method?

  • Thread starter Thread starter ³ÂÏ£ÕÂ
  • Start date Start date
³

³ÂÏ£ÕÂ

Hi ,All

I have a simple class that contain few method, like this

public class TestClass{

public void A(){}
public void B(){}

}

I want to do something automatic before call the method ( for
example ,A() or B() etc)$B!#(BIs possible to do this through pass an
attribute or other something, except write the code like below:

public void A(){
DoSomethingFirst();
//continue to do method logic
}
public void B(){
DoSomethingFirst();
//continue to do method logic
}

I think the attribute maybe do this , or the event is the better
choice, but how can we handler the event when the Method(A() or B())
be invoked.

Any advise is welcome, thank you!

best regard,
Ares
 
I have a simple class that contain few method, like this

public class TestClass{

public void A(){}
public void B(){}

}

I want to do something automatic before call the method ( for
example ,A() or B() etc)¡£Is possible to do this through pass an
attribute or other something, except write the code like below:

public void A(){
DoSomethingFirst();
//continue to do method logic}

public void B(){
DoSomethingFirst();
//continue to do method logic

}

I think the attribute maybe do this , or the event is the better
choice, but how can we handler the event when the Method(A() or B())
be invoked.

What you're describing here is precisely a kind of thing AOP (aspect-
oriented programming) provides. I suggest you read a bit about it in
general, say, on Wikipedia, and then have a look at PostSharp, which
is probably the most well-known AOP implementation for .NET:

http://www.postsharp.org/
 
What you're describing here is precisely a kind of thing AOP (aspect-
oriented programming) provides. I suggest you read a bit about it in
general, say, on Wikipedia, and then have a look at PostSharp, which
is probably the most well-known AOP implementation for .NET:

http://www.postsharp.org/- Òþ²Ø±»ÒýÓÃÎÄ×Ö -

- ÏÔʾÒýÓõÄÎÄ×Ö -

It's great, Pavel ! thank you very much!

best regard,
Ares
 
Back
Top